-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add Apple JWT Token based authentication
- Loading branch information
Showing
5 changed files
with
165 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
apns "github.com/sideshow/apns2" | ||
"github.com/sideshow/apns2/token" | ||
) | ||
|
||
func main() { | ||
|
||
authKey, err := token.AuthKeyFromFile("../APNSAuthKey_T64N7W47U9.p8") | ||
if err != nil { | ||
log.Fatal("token error:", err) | ||
} | ||
|
||
token := &token.Token{ | ||
AuthKey: authKey, | ||
KeyID: "T64N7W47U9", | ||
TeamID: "264H7447N5", | ||
} | ||
|
||
notification := &apns.Notification{} | ||
notification.DeviceToken = "cb7262544176c3f15efdcdcf9dd03418dfca82ba710c54ab6b1352350d442cb4" | ||
notification.Topic = "com.Apns2" | ||
notification.Payload = []byte(`{ | ||
"aps" : { | ||
"alert" : "Hello!" | ||
} | ||
} | ||
`) | ||
|
||
client := apns.NewTokenClient(token) | ||
res, err := client.Push(notification) | ||
|
||
if err != nil { | ||
log.Fatal("error: ", err) | ||
} else { | ||
fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason) | ||
} | ||
} |
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,82 @@ | ||
package token | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"errors" | ||
"io/ioutil" | ||
"sync" | ||
"time" | ||
|
||
jwt "github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
const ( | ||
TokenTimeout = 3000 // 50 minutes | ||
) | ||
|
||
type Token struct { | ||
AuthKey *ecdsa.PrivateKey | ||
KeyID string | ||
TeamID string | ||
IssuedAt int64 | ||
Bearer string | ||
m sync.Mutex | ||
} | ||
|
||
func AuthKeyFromFile(filename string) (*ecdsa.PrivateKey, error) { | ||
bytes, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return AuthKeyFromBytes(bytes) | ||
} | ||
|
||
func AuthKeyFromBytes(bytes []byte) (*ecdsa.PrivateKey, error) { | ||
block, _ := pem.Decode(bytes) | ||
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, errors.New("token: AuthKey must be of type ecdsa.PrivateKey") | ||
} | ||
} | ||
|
||
func (t *Token) GenerateIfExpired() { | ||
t.m.Lock() | ||
defer t.m.Unlock() | ||
if t.Expired() { | ||
t.Generate() | ||
} | ||
} | ||
|
||
func (t *Token) Expired() bool { | ||
return time.Now().Unix() >= (t.IssuedAt + TokenTimeout) | ||
} | ||
|
||
func (t *Token) Generate() (bool, error) { | ||
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 | ||
} |