diff --git a/push/errors.go b/push/errors.go index 59ae342..2c17e9f 100644 --- a/push/errors.go +++ b/push/errors.go @@ -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") @@ -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": @@ -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: diff --git a/push/header.go b/push/header.go index cbfe1f0..5851b08 100644 --- a/push/header.go +++ b/push/header.go @@ -25,6 +25,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 @@ -54,4 +57,8 @@ func (h *Headers) set(reqHeader http.Header) { reqHeader.Set("apns-topic", h.Topic) } + if h.Authorization != "" { + reqHeader.Set("authorization", "bearer "+h.Authorization) + } + } diff --git a/push/header_test.go b/push/header_test.go index 330e9da..b33fa6d 100644 --- a/push/header_test.go +++ b/push/header_test.go @@ -8,11 +8,12 @@ import ( func TestHeaders(t *testing.T) { headers := Headers{ - ID: "uuid", - CollapseID: "game1.score.identifier", - Expiration: time.Unix(12622780800, 0), - LowPriority: true, - Topic: "bundle-id", + ID: "uuid", + CollapseID: "game1.score.identifier", + Expiration: time.Unix(12622780800, 0), + LowPriority: true, + Topic: "bundle-id", + Authorization: "eyJhbGciOiJFUzI1N", } reqHeader := http.Header{} @@ -23,6 +24,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) { @@ -35,6 +37,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) { @@ -47,6 +50,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) {