Skip to content

Commit 87de1a0

Browse files
committed
fix: Add the TokenIssueAtFunc and TokenExpireAtFunc to prevent 401 when the default server time is abnormal
1 parent 694d3e0 commit 87de1a0

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

store.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ const (
3636
)
3737

3838
type StoreConfig struct {
39-
KeyContent []byte // Loads a .p8 certificate
40-
KeyID string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
41-
BundleID string // Your app’s bundle ID
42-
Issuer string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
43-
Sandbox bool // default is Production
39+
KeyContent []byte // Loads a .p8 certificate
40+
KeyID string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
41+
BundleID string // Your app’s bundle ID
42+
Issuer string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
43+
Sandbox bool // default is Production
44+
TokenIssuedAtFunc func() int64 // The token’s creation time func. Default is current timestamp.
45+
TokenExpiredAtFunc func() int64 // The token’s expiration time func. Default is one hour later.
4446
}
4547

4648
type StoreClient struct {

token.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ var (
2424
type Token struct {
2525
sync.Mutex
2626

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.
3234

3335
// internal variables
3436
AuthKey *ecdsa.PrivateKey // .p8 private key
@@ -45,14 +47,16 @@ func (t *Token) WithConfig(c *StoreConfig) {
4547
t.BundleID = c.BundleID
4648
t.Issuer = c.Issuer
4749
t.Sandbox = c.Sandbox
50+
t.IssuedAtFunc = c.TokenIssuedAtFunc
51+
t.ExpiredAtFunc = c.TokenExpiredAtFunc
4852
}
4953

5054
// GenerateIfExpired checks to see if the token is about to expire and generates a new token.
5155
func (t *Token) GenerateIfExpired() (string, error) {
5256
t.Lock()
5357
defer t.Unlock()
5458

55-
if t.Expired() {
59+
if t.Expired() || t.Bearer == "" {
5660
err := t.Generate()
5761
if err != nil {
5862
return "", err
@@ -76,7 +80,13 @@ func (t *Token) Generate() error {
7680
t.AuthKey = key
7781

7882
issuedAt := time.Now().Unix()
83+
if t.IssuedAtFunc != nil {
84+
issuedAt = t.IssuedAtFunc()
85+
}
7986
expiredAt := time.Now().Add(time.Duration(1) * time.Hour).Unix()
87+
if t.ExpiredAtFunc != nil {
88+
expiredAt = t.ExpiredAtFunc()
89+
}
8090
jwtToken := &jwt.Token{
8191
Header: map[string]interface{}{
8292
"alg": "ES256",

0 commit comments

Comments
 (0)