This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from mercari/feature_support_token_based_prov…
…ider support token based provider for APNs
- Loading branch information
Showing
20 changed files
with
507 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEbVzfPnZPxfAyxqE | ||
ZV05laAoJAl+/6Xt2O4mOB611sOhRANCAASgFTKjwJAAU95g++/vzKWHkzAVmNMI | ||
tB5vTjZOOIwnEb70MsWZFIyUFD1P9Gwstz4+akHX7vI8BH6hHmBmfeQl | ||
-----END PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDfdOqotHd55SYO | ||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HXrO+CCw9BTJAF+c3c3xeUOLXafzyW29bASGfUtA7Ax | ||
QAsR+Rr3+wKBgAwfZxrh6ZWP+17+WuVArOWIMZFj7SRX2yGdWa/lxwgmNPSSFkXj | ||
hkBttujoY8IsSrTivzqpgCrTCjPTpir4iURzWw4W08bpjd7u3C/HX7Y16Uq8ohEJ | ||
T5lslveDJ3iNljSK74eMK7kLg7fBM7YDogxccHJ1IHsvInp3e1pmZxOxAoGAO+bS | ||
TUQ4N/UuQezgkF3TDrnBraO67leDGwRbfiE/U0ghQvqh5DA0QSPVzlWDZc9KUitv | ||
j8vxsR9o1PW9GS0an17GJEYuetLnkShKK3NWOhBBX6d1yP9rVdH6JhgIJEy/g0Su | ||
z7TAFiFc8i7JF8u4QJ05C8bZAMhOLotqftQeVOMCgYAid8aaRvaM2Q8a42Jn6ZTT | ||
5ms6AvNr98sv0StnfmNQ+EYXN0bEk2huSW+w2hN34TYYBTjViQmHbhudwwu8lVjE | ||
ccDmIXsUFbHVK+kTIpWGGchy5cYPs3k9s1nMR2av0Lojtw9WRY76xRXvN8W6R7Eh | ||
wA2ax3+gEEYpGhjM/lO2Lg== | ||
-----END PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEogIBAAKCAQEA33TqqLR3eeUmDtHS89qF3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9 | ||
cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA04DDnEFGAf+kDQiNSe2ZtqC7bnIc | ||
8+KSG/qOGQIVaay4Ucr6ovDkykO5Hxn7OU7sJp9TP9H0JH8zMQA6YzijYH9LsupT | ||
errY3U6zyihVEDXXOv08vBHk50BMFJbE9iwFwnxCsU5+UZUZYw87Uu0n4LPFS9BT | ||
8tUIvA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IIUL6oeQwNEEj1c5Vg2XPSlIrb4/L8bEfaNT1vRktGp9exiRGLnrS55EoSitz | ||
VjoQQV+ndcj/a1XR+iYYCCRMv4NErs+0wBYhXPIuyRfLuECdOQvG2QDITi6Lan7U | ||
HlTjAoGAInfGmkb2jNkPGuNiZ+mU0+ZrOgLza/fLL9ErZ35jUPhGFzdGxJNobklv | ||
sNoTd+E2GAU41YkJh24bncMLvJVYxHHA5iF7FBWx1SvpEyKVhhnIcuXGD7N5PbNZ | ||
zEdmr9C6I7cPVkWO+sUV7zfFukexIcANmsd/oBBGKRoYzP5Tti4= | ||
-----END RSA PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEbVzfPnZPxfAyxqE | ||
ZV05laAoJAl+/6Xt2O4mOB611sOhRANCAASgFTKjwJAAU95g++/vzKWHkzAVmNMI | ||
tB5vTjZOOIwnEb70MsWZFIyUFD1P9Gwstz4+akHX7vI8BH6hHmBmfZZZ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgEbVzfPnZPxfAyxqE | ||
ZV05laAoJAl+/6Xt2O4mOB611sOhRANCAASgFTKjwJAAU95g++/vzKWHkzAVmNMI | ||
tB5vTjZOOIwnEb70MsWZFIyUFD1P9Gwstz4+akHX7vI8BH6hHmBmfeQl | ||
-----END PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Package token | ||
// original: https://github.com/sideshow/apns2/blob/master/token/token.go | ||
// Copyright (c) 2016 Adam Jones | ||
package token | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"io/ioutil" | ||
"sync" | ||
"time" | ||
|
||
jwt "github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
const ( | ||
// TokenTimeout is the period of time in seconds that a token is valid for. | ||
// If the timestamp for token issue is not within the last hour, APNs | ||
// rejects subsequent push messages. This is set to under an hour so that | ||
// we generate a new token before the existing one expires. | ||
TokenTimeout = 3000 | ||
) | ||
|
||
// Possible errors when parsing a .p8 file. | ||
var ( | ||
ErrAuthKeyNotPem = errors.New("token: AuthKey must be a valid .p8 PEM file") | ||
ErrAuthKeyNotECDSA = errors.New("token: AuthKey must be of type ecdsa.PrivateKey") | ||
ErrAuthKeyNil = errors.New("token: AuthKey was nil") | ||
) | ||
|
||
// Token represents an Apple Provider Authentication Token (JSON Web Token). | ||
type Token struct { | ||
sync.Mutex | ||
AuthKey *ecdsa.PrivateKey | ||
KeyID string | ||
TeamID string | ||
IssuedAt int64 | ||
Bearer string | ||
} | ||
|
||
// AuthKeyFromFile loads a .p8 certificate from a local file and returns a | ||
func AuthKeyFromFile(filename string) (*ecdsa.PrivateKey, error) { | ||
bytes, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return AuthKeyFromBytes(bytes) | ||
} | ||
|
||
// AuthKeyFromBytes loads a .p8 certificate from an in memory byte array and | ||
func AuthKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error) { | ||
block, _ := pem.Decode(bytes) | ||
if block == nil { | ||
return nil, ErrAuthKeyNotPem | ||
} | ||
key, err := x509.ParsePKCS8PrivateKey(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch pk := key.(type) { | ||
case *ecdsa.PrivateKey: | ||
return pk, nil | ||
default: | ||
return nil, ErrAuthKeyNotECDSA | ||
} | ||
} | ||
|
||
// GenerateBearerIfExpired checks to see if the token is about to expire and | ||
// generates a new token. | ||
func (t *Token) GenerateBearerIfExpired() (bearer string) { | ||
t.Lock() | ||
defer t.Unlock() | ||
if t.Expired() { | ||
// TODO: error handling | ||
t.Generate() | ||
} | ||
return t.Bearer | ||
} | ||
|
||
// Expired checks to see if the token has expired. | ||
func (t *Token) Expired() bool { | ||
return time.Now().Unix() >= (t.IssuedAt + TokenTimeout) | ||
} | ||
|
||
// Generate creates a new token. | ||
func (t *Token) Generate() (bool, error) { | ||
if t.AuthKey == nil { | ||
return false, ErrAuthKeyNil | ||
} | ||
issuedAt := time.Now().Unix() | ||
jwtToken := &jwt.Token{ | ||
Header: map[string]interface{}{ | ||
"alg": "ES256", | ||
"kid": t.KeyID, | ||
}, | ||
Claims: jwt.MapClaims{ | ||
"iss": t.TeamID, | ||
"iat": issuedAt, | ||
}, | ||
Method: jwt.SigningMethodES256, | ||
} | ||
bearer, err := jwtToken.SignedString(t.AuthKey) | ||
if err != nil { | ||
return false, err | ||
} | ||
t.IssuedAt = issuedAt | ||
t.Bearer = bearer | ||
return true, nil | ||
} |
Oops, something went wrong.