Skip to content

Commit 60bbc19

Browse files
authored
Merge pull request #305 from AvdLee/bugfix/add-required-iat-field-in-jwt
Add required `iat` field to JWT
2 parents 5fb7c50 + 38c137e commit 60bbc19

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

Sources/JWT/JWT.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,17 @@ private struct TeamPayload: Codable {
3434
case issuerIdentifier = "iss"
3535
case expirationTime = "exp"
3636
case audience = "aud"
37+
case issuedAtTime = "iat"
3738
}
3839

3940
/// Your issuer identifier from the API Keys page in App Store Connect (Ex: 57246542-96fe-1a63-e053-0824d011072a)
4041
let issuerIdentifier: String
4142

4243
/// The token's expiration time, in Unix epoch time; tokens that expire more than 20 minutes in the future are not valid (Ex: 1528408800)
4344
let expirationTime: TimeInterval
45+
46+
/// The token’s creation time, in UNIX epoch time (Ex: 1528407600)
47+
let issuedAtTime: TimeInterval
4448

4549
/// The required audience which is set to the App Store Connect version.
4650
let audience: String = "appstoreconnect-v1"
@@ -52,13 +56,17 @@ private struct IndividualPayload: Codable {
5256
case subject = "sub"
5357
case expirationTime = "exp"
5458
case audience = "aud"
59+
case issuedAtTime = "iat"
5560
}
5661

5762
/// The subject to pass to the payload when using individual keys
5863
let subject: String = "user"
5964

6065
/// The token's expiration time, in Unix epoch time; tokens that expire more than 20 minutes in the future are not valid (Ex: 1528408800)
6166
let expirationTime: TimeInterval
67+
68+
/// The token’s creation time, in UNIX epoch time (Ex: 1528407600)
69+
let issuedAtTime: TimeInterval
6270

6371
/// The required audience which is set to the App Store Connect version.
6472
let audience: String = "appstoreconnect-v1"
@@ -122,12 +130,20 @@ public struct JWT: Codable, JWTCreatable {
122130

123131
/// Combine the header and the payload as a digest for signing.
124132
private func digest(dateProvider: DateProvider) throws -> String {
125-
let expirationTime = dateProvider().addingTimeInterval(expireDuration).timeIntervalSince1970
133+
let now = dateProvider()
134+
let expirationTime = now.addingTimeInterval(expireDuration).timeIntervalSince1970
126135
let payload: Codable
127136
if let issuerIdentifier {
128-
payload = TeamPayload(issuerIdentifier: issuerIdentifier, expirationTime: expirationTime)
137+
payload = TeamPayload(
138+
issuerIdentifier: issuerIdentifier,
139+
expirationTime: expirationTime,
140+
issuedAtTime: now.timeIntervalSince1970
141+
)
129142
} else {
130-
payload = IndividualPayload(expirationTime: expirationTime)
143+
payload = IndividualPayload(
144+
expirationTime: expirationTime,
145+
issuedAtTime: now.timeIntervalSince1970
146+
)
131147
}
132148

133149
let headerString = try JSONEncoder().encode(header.self).base64URLEncoded()

0 commit comments

Comments
 (0)