-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
110 lines (92 loc) · 2.08 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package appstoreserverapi
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"errors"
"sync"
"time"
"github.com/golang-jwt/jwt/v5"
)
// Token JSON Web Tokens signed to authorize App Store Server API requests
type Token struct {
conf *Config
// token lock
mutex sync.RWMutex
token string
// token 过期时间
expireAt time.Time
}
func NewToken(conf *Config) *Token {
return &Token{
conf: conf,
}
}
func (t *Token) Get() (string, error) {
if token := t.get(); "" != token {
return token, nil
}
t.mutex.Lock()
defer t.mutex.Unlock()
var err error
t.token, t.expireAt, err = t.create()
if err != nil {
return "", err
}
return t.token, nil
}
// get return exist valid token
func (t *Token) get() string {
t.mutex.RLock()
defer t.mutex.RUnlock()
if "" != t.token && t.expireAt.After(time.Now()) {
return t.token
}
return ""
}
// create got new token
// 文档:https://developer.apple.com/documentation/appstoreserverapi/generating_tokens_for_api_requests
func (t *Token) create() (string, time.Time, error) {
now := time.Now()
alg := jwt.SigningMethodES256
exp := now.Add(55 * time.Minute)
token := jwt.Token{
Method: alg,
Header: map[string]interface{}{
"alg": alg.Name,
"kid": t.conf.KeyID,
"typ": "JWT",
},
Claims: jwt.MapClaims{
"iss": t.conf.Issuer,
"iat": now.Unix(),
"exp": exp.Unix(),
"aud": "appstoreconnect-v1",
"bid": t.conf.BundleID,
},
}
key, err := t.loadPrivateKey()
if err != nil {
return "", time.Time{}, err
}
s, err := token.SignedString(key)
if err != nil {
return "", time.Time{}, err
}
return s, exp, nil
}
func (t *Token) loadPrivateKey() (*ecdsa.PrivateKey, error) {
block, _ := pem.Decode(t.conf.PrivateKey)
if block == nil {
return nil, errors.New("appstore.appstoreserverapi.Token: private api key must be a PEM encoded PKCS8 key")
}
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
pk, ok := key.(*ecdsa.PrivateKey)
if !ok {
return nil, errors.New("appstore.appstoreserverapi.Token: key is not a valid ECDSA private key")
}
return pk, nil
}