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

initial support for Token Authentication (JWT) #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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