Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
initial support for JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
nathany committed Jun 15, 2016
1 parent 208173b commit e9c2474
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
12 changes: 12 additions & 0 deletions push/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var (
ErrBadPriority = errors.New("BadPriority")
ErrBadTopic = errors.New("BadTopic")

// Token authentication errors.
ErrInvalidProviderToken = errors.New("InvalidProviderToken")
ErrExpiredProviderToken = errors.New("ExpiredProviderToken")

// Certificate and topic errors.
ErrBadCertificate = errors.New("BadCertificate")
ErrBadCertificateEnvironment = errors.New("BadCertificateEnvironment")
Expand Down Expand Up @@ -80,6 +84,10 @@ func mapErrorReason(reason string) error {
e = ErrUnregistered
case "DuplicateHeaders":
e = ErrDuplicateHeaders
case "InvalidProviderToken":
e = ErrInvalidProviderToken
case "ExpiredProviderToken":
e = ErrExpiredProviderToken
case "BadCertificateEnvironment":
e = ErrBadCertificateEnvironment
case "BadCertificate":
Expand Down Expand Up @@ -128,6 +136,10 @@ func (e *Error) Error() string {
return "the apns-priority value is bad"
case ErrBadTopic:
return "the Topic header was invalid"
case ErrInvalidProviderToken:
return "JWT authentication token is invalid"
case ErrExpiredProviderToken:
return "JWT authentication token expired"
case ErrBadCertificate:
return "the certificate was bad"
case ErrBadCertificateEnvironment:
Expand Down
7 changes: 7 additions & 0 deletions push/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type Headers struct {

// Topic for certificates with multiple topics.
Topic string

// Authorization for Token Authentication (JWT)
Authorization string
}

// set headers for an HTTP request
Expand All @@ -46,4 +49,8 @@ func (h *Headers) set(reqHeader http.Header) {
reqHeader.Set("apns-topic", h.Topic)
}

if h.Authorization != "" {
reqHeader.Set("authorization", "bearer "+h.Authorization)
}

}
12 changes: 8 additions & 4 deletions push/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (

func TestHeaders(t *testing.T) {
headers := Headers{
ID: "uuid",
Expiration: time.Unix(12622780800, 0),
LowPriority: true,
Topic: "bundle-id",
ID: "uuid",
Expiration: time.Unix(12622780800, 0),
LowPriority: true,
Topic: "bundle-id",
Authorization: "eyJhbGciOiJFUzI1N",
}

reqHeader := http.Header{}
Expand All @@ -21,6 +22,7 @@ func TestHeaders(t *testing.T) {
testHeader(t, reqHeader, "apns-expiration", "12622780800")
testHeader(t, reqHeader, "apns-priority", "5")
testHeader(t, reqHeader, "apns-topic", "bundle-id")
testHeader(t, reqHeader, "authorization", "bearer eyJhbGciOiJFUzI1N")
}

func TestNilHeader(t *testing.T) {
Expand All @@ -32,6 +34,7 @@ func TestNilHeader(t *testing.T) {
testHeader(t, reqHeader, "apns-expiration", "")
testHeader(t, reqHeader, "apns-priority", "")
testHeader(t, reqHeader, "apns-topic", "")
testHeader(t, reqHeader, "authorization", "")
}

func TestEmptyHeaders(t *testing.T) {
Expand All @@ -43,6 +46,7 @@ func TestEmptyHeaders(t *testing.T) {
testHeader(t, reqHeader, "apns-expiration", "")
testHeader(t, reqHeader, "apns-priority", "")
testHeader(t, reqHeader, "apns-topic", "")
testHeader(t, reqHeader, "authorization", "")
}

func testHeader(t *testing.T, reqHeader http.Header, key, expected string) {
Expand Down

0 comments on commit e9c2474

Please sign in to comment.