Skip to content

Commit

Permalink
Release/1.3.0 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
borut-t authored Apr 25, 2024
2 parents 980741e + 382fac2 commit 7bca192
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
12 changes: 12 additions & 0 deletions Sources/Apple/AppleAuthenticator+Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Foundation
public extension AppleAuthenticator {
enum Nonce {
case random(length: UInt)
case custom(value: String)
}

struct Response {
Expand All @@ -22,6 +23,17 @@ public extension AppleAuthenticator {
public let email: Email
public let expiresAt: Date
}

enum Error: Swift.Error {
case system(_ error: Swift.Error)
case cancelled
case invalidNonceLength
case invalidIdentityToken
case unhandledAuthorization
case credentialsRevoked
case missingExpiration
case missingEmail
}
}

public extension AppleAuthenticator.Response {
Expand Down
33 changes: 11 additions & 22 deletions Sources/Apple/AppleAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ extension AppleAuthenticator: Authenticator {
public func canOpenUrl(_ url: URL, application: UIApplication, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
false
}

/// Generate random nonce string
public func generateNonceString(length: UInt = 32) -> String {
let charset: [Character] = Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
let result = (0..<length).compactMap { _ in charset.randomElement() }
guard result.count == length else { fatalError("Unable to generate nonce!") }
return String(result)
}
}

// MARK: - ASAuthorizationControllerDelegate
Expand Down Expand Up @@ -145,20 +153,6 @@ extension AppleAuthenticator: ASAuthorizationControllerDelegate {
}
}

// MARK: - Error
public extension AppleAuthenticator {
enum Error: Swift.Error {
case system(_ error: Swift.Error)
case cancelled
case invalidNonceLength
case invalidIdentityToken
case unhandledAuthorization
case credentialsRevoked
case missingExpiration
case missingEmail
}
}

// MARK: - Private Methods
private extension AppleAuthenticator {
func appleSignIn(on presentingViewController: UIViewController, with nonce: Nonce?) {
Expand All @@ -171,7 +165,9 @@ private extension AppleAuthenticator {
rejectSignIn(with: .invalidNonceLength)
return
}
request.nonce = generateRandomNonceString(length: length).sha256
request.nonce = generateNonceString(length: length).sha256
case .custom(let value):
request.nonce = value
case .none:
break
}
Expand All @@ -182,13 +178,6 @@ private extension AppleAuthenticator {
controller.performRequests()
}

func generateRandomNonceString(length: UInt = 32) -> String {
let charset: [Character] = Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
let result = (0..<length).compactMap { _ in charset.randomElement() }
guard result.count == length else { fatalError("Unable to generate nonce!") }
return String(result)
}

func setupCredentialsRevokeListener() {
NotificationCenter.default.addObserver(self,
selector: #selector(appleCredentialRevoked),
Expand Down

0 comments on commit 7bca192

Please sign in to comment.