Skip to content

Commit 1593518

Browse files
authored
Merge pull request #281 from richzw/master
feat(appstore): replace issuedAt/expiredAt with issuedAtFunc/expiredAtFunc
2 parents 63f7061 + 854933c commit 1593518

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

appstore/api/store.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ const (
3838
)
3939

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

5050
type (

appstore/api/token.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@ 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
32-
IssueAt int64 // The token’s creation time, in UNIX time. Default is current timestamp.
33-
ExpiredAt int64 // The token’s expiration time, in UNIX time. Tokens that expire more than 60 minutes after the time in iat are not valid (Ex: 1623086400)
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.
3434

3535
// internal variables
36-
AuthKey *ecdsa.PrivateKey // .p8 private key
37-
Bearer string // Authorized bearer token
36+
AuthKey *ecdsa.PrivateKey // .p8 private key
37+
Bearer string // Authorized bearer token
38+
ExpiredAt int64 // The token’s expiration time, in UNIX time
3839
}
3940

4041
func (t *Token) WithConfig(c *StoreConfig) {
@@ -43,8 +44,8 @@ func (t *Token) WithConfig(c *StoreConfig) {
4344
t.BundleID = c.BundleID
4445
t.Issuer = c.Issuer
4546
t.Sandbox = c.Sandbox
46-
t.IssueAt = c.TokenIssueAt
47-
t.ExpiredAt = c.TokenExpiredAt
47+
t.IssuedAtFunc = c.TokenIssuedAtFunc
48+
t.ExpiredAtFunc = c.TokenExpiredAtFunc
4849
}
4950

5051
// GenerateIfExpired checks to see if the token is about to expire and generates a new token.
@@ -76,12 +77,12 @@ func (t *Token) Generate() error {
7677
t.AuthKey = key
7778

7879
issuedAt := time.Now().Unix()
79-
if t.IssueAt > 0 {
80-
issuedAt = t.IssueAt
80+
if t.IssuedAtFunc != nil {
81+
issuedAt = t.IssuedAtFunc()
8182
}
8283
expiredAt := time.Now().Add(time.Duration(1) * time.Hour).Unix()
83-
if t.ExpiredAt > 0 {
84-
expiredAt = t.ExpiredAt
84+
if t.ExpiredAtFunc != nil {
85+
expiredAt = t.ExpiredAtFunc()
8586
}
8687
jwtToken := &jwt.Token{
8788
Header: map[string]interface{}{

0 commit comments

Comments
 (0)