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 Sep 19, 2016
1 parent d6d71af commit 50f3f9a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 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 @@ -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
Expand Down Expand Up @@ -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)
}

}
14 changes: 9 additions & 5 deletions push/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 50f3f9a

Please sign in to comment.