Skip to content

Commit

Permalink
Fix authentication refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee committed Jul 23, 2022
1 parent 46cca57 commit 1a66950
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Example/Shared/AppsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ struct AppsListView: View {
ProgressView()
.opacity(viewModel.apps.isEmpty ? 1.0 : 0.0)
}.navigationTitle("List of Apps")
.toolbar {
Button("Refresh") {
viewModel.loadApps()
}
}
}.onAppear {
viewModel.loadApps()
}
Expand Down
26 changes: 20 additions & 6 deletions Sources/JWT/JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,35 @@ struct JWT: Codable, JWTCreatable {
typealias Token = String
typealias P8PrivateKey = String

typealias DateProvider = () -> Date
static let defaultDateProvider: DateProvider = {
Date()
}

/// The JWT Header contains information specific to the App Store Connect API Keys, such as algorithm and keys.
private let header: Header

/// The JWT Payload contains information specific to the App Store Connect APIs, such as issuer ID and expiration time.
private let payload: Payload
/// Your issuer identifier from the API Keys page in App Store Connect (Ex: 57246542-96fe-1a63-e053-0824d011072a)
private let issuerIdentifier: String

/// The token's expiration duration. Tokens that expire more than 20 minutes in the future are not valid, so set it to a max of 20 minutes.
private let expireDuration: TimeInterval

/// Creates a new JWT Factory to create signed requests for the App Store Connect API.
///
/// - Parameters:
/// - keyIdentifier: Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
/// - issuerIdentifier: Your issuer identifier from the API Keys page in App Store Connect (Ex: 57246542-96fe-1a63-e053-0824d011072a)
/// - expireDuration: The token's expiration duration. Tokens that expire more than 20 minutes in the future are not valid, so set it to a max of 20 minutes.
public init(keyIdentifier: String, issuerIdentifier: String, expireDuration: TimeInterval, baseDate: Date = Date()) {
public init(keyIdentifier: String, issuerIdentifier: String, expireDuration: TimeInterval) {
header = Header(keyIdentifier: keyIdentifier)
payload = Payload(issuerIdentifier: issuerIdentifier, expirationTime: baseDate.addingTimeInterval(expireDuration).timeIntervalSince1970)
self.issuerIdentifier = issuerIdentifier
self.expireDuration = expireDuration
}

/// Combine the header and the payload as a digest for signing.
private func digest() throws -> String {
private func digest(dateProvider: DateProvider) throws -> String {
let payload = Payload(issuerIdentifier: issuerIdentifier, expirationTime: dateProvider().addingTimeInterval(expireDuration).timeIntervalSince1970)
let headerString = try JSONEncoder().encode(header.self).base64URLEncoded()
let payloadString = try JSONEncoder().encode(payload.self).base64URLEncoded()
return "\(headerString).\(payloadString)"
Expand All @@ -112,7 +122,11 @@ struct JWT: Codable, JWTCreatable {
/// - Returns: A signed JWT.Token value which can be used as a value for the Bearer Authentication header.
/// - Throws: An error if something went wrong, like a JWT.Error.
public func signedToken(using privateKey: P8PrivateKey) throws -> JWT.Token {
let digest = try self.digest()
try signedToken(using: privateKey, dateProvider: Self.defaultDateProvider)
}

func signedToken(using privateKey: P8PrivateKey, dateProvider: DateProvider) throws -> JWT.Token {
let digest = try self.digest(dateProvider: dateProvider)

let signature = try privateKey.toASN1()
.toECKeyData()
Expand Down

0 comments on commit 1a66950

Please sign in to comment.