@@ -24,11 +24,13 @@ var (
24
24
type Token struct {
25
25
sync.Mutex
26
26
27
- KeyContent []byte // Loads a .p8 certificate
28
- KeyID string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
29
- BundleID string // Your app’s bundle ID
30
- Issuer string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
31
- Sandbox bool // default is Production
27
+ KeyContent []byte // Loads a .p8 certificate
28
+ KeyID string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
29
+ BundleID string // Your app’s bundle ID
30
+ Issuer string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
31
+ Sandbox bool // default is Production
32
+ IssuedAtFunc func () int64 // The token’s creation time func. Default is current timestamp.
33
+ ExpiredAtFunc func () int64 // The token’s expiration time func.
32
34
33
35
// internal variables
34
36
AuthKey * ecdsa.PrivateKey // .p8 private key
@@ -45,14 +47,16 @@ func (t *Token) WithConfig(c *StoreConfig) {
45
47
t .BundleID = c .BundleID
46
48
t .Issuer = c .Issuer
47
49
t .Sandbox = c .Sandbox
50
+ t .IssuedAtFunc = c .TokenIssuedAtFunc
51
+ t .ExpiredAtFunc = c .TokenExpiredAtFunc
48
52
}
49
53
50
54
// GenerateIfExpired checks to see if the token is about to expire and generates a new token.
51
55
func (t * Token ) GenerateIfExpired () (string , error ) {
52
56
t .Lock ()
53
57
defer t .Unlock ()
54
58
55
- if t .Expired () {
59
+ if t .Expired () || t . Bearer == "" {
56
60
err := t .Generate ()
57
61
if err != nil {
58
62
return "" , err
@@ -76,7 +80,13 @@ func (t *Token) Generate() error {
76
80
t .AuthKey = key
77
81
78
82
issuedAt := time .Now ().Unix ()
83
+ if t .IssuedAtFunc != nil {
84
+ issuedAt = t .IssuedAtFunc ()
85
+ }
79
86
expiredAt := time .Now ().Add (time .Duration (1 ) * time .Hour ).Unix ()
87
+ if t .ExpiredAtFunc != nil {
88
+ expiredAt = t .ExpiredAtFunc ()
89
+ }
80
90
jwtToken := & jwt.Token {
81
91
Header : map [string ]interface {}{
82
92
"alg" : "ES256" ,
0 commit comments