Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(appstore): replace issuedAt/expiredAt with issuedAtFunc/expiredAtFunc #281

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions appstore/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const (
)

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

type (
Expand Down
31 changes: 16 additions & 15 deletions appstore/api/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ var (
type Token struct {
sync.Mutex

KeyContent []byte // Loads a .p8 certificate
KeyID string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
BundleID string // Your app’s bundle ID
Issuer string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
Sandbox bool // default is Production
IssueAt int64 // The token’s creation time, in UNIX time. Default is current timestamp.
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)
KeyContent []byte // Loads a .p8 certificate
KeyID string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
BundleID string // Your app’s bundle ID
Issuer string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
Sandbox bool // default is Production
IssuedAtFunc func() int64 // The token’s creation time func. Default is current timestamp.
ExpiredAtFunc func() int64 // The token’s expiration time func.

// internal variables
AuthKey *ecdsa.PrivateKey // .p8 private key
Bearer string // Authorized bearer token
AuthKey *ecdsa.PrivateKey // .p8 private key
Bearer string // Authorized bearer token
ExpiredAt int64 // The token’s expiration time, in UNIX time
}

func (t *Token) WithConfig(c *StoreConfig) {
Expand All @@ -43,8 +44,8 @@ func (t *Token) WithConfig(c *StoreConfig) {
t.BundleID = c.BundleID
t.Issuer = c.Issuer
t.Sandbox = c.Sandbox
t.IssueAt = c.TokenIssueAt
t.ExpiredAt = c.TokenExpiredAt
t.IssuedAtFunc = c.TokenIssuedAtFunc
t.ExpiredAtFunc = c.TokenExpiredAtFunc
}

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

issuedAt := time.Now().Unix()
if t.IssueAt > 0 {
issuedAt = t.IssueAt
if t.IssuedAtFunc != nil {
issuedAt = t.IssuedAtFunc()
}
expiredAt := time.Now().Add(time.Duration(1) * time.Hour).Unix()
if t.ExpiredAt > 0 {
expiredAt = t.ExpiredAt
if t.ExpiredAtFunc != nil {
expiredAt = t.ExpiredAtFunc()
}
jwtToken := &jwt.Token{
Header: map[string]interface{}{
Expand Down
Loading