Skip to content

Commit 5b68f34

Browse files
committed
Add authenticateWithApple to apple oauth
1 parent 384f394 commit 5b68f34

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Generated using Sourcery 2.0.2 — https://github.com/krzysztofzablocki/Sourcery
2+
// DO NOT EDIT
3+
import Combine
4+
import Foundation
5+
6+
public extension StytchClient.OAuth.Apple {
7+
/// This function is intended for rare cases where you must authenticate with Apple only and collect the JWT.
8+
/// This function will not create a Stytch user.
9+
func authenticateWithApple(completion: @escaping Completion<String>) {
10+
Task {
11+
do {
12+
completion(.success(try await authenticateWithApple()))
13+
} catch {
14+
completion(.failure(error))
15+
}
16+
}
17+
}
18+
19+
/// This function is intended for rare cases where you must authenticate with Apple only and collect the JWT.
20+
/// This function will not create a Stytch user.
21+
func authenticateWithApple() -> AnyPublisher<String, Error> {
22+
return Deferred {
23+
Future({ promise in
24+
Task {
25+
do {
26+
promise(.success(try await authenticateWithApple()))
27+
} catch {
28+
promise(.failure(error))
29+
}
30+
}
31+
})
32+
}
33+
.eraseToAnyPublisher()
34+
}
35+
}

Sources/StytchCore/StytchClient/OAuth/OAuth+Apple.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ public extension StytchClient.OAuth {
5959

6060
return authenticateResponse
6161
}
62+
63+
// sourcery: AsyncVariants, (NOTE: - must use /// doc comment styling)
64+
/// This function is intended for rare cases where you must authenticate with Apple only and collect the JWT.
65+
/// This function will not create a Stytch user.
66+
public func authenticateWithApple() async throws -> String {
67+
let rawNonce = try cryptoClient.dataWithRandomBytesOfCount(32).toHexString()
68+
69+
let authenticateResult = try await appleOAuthClient.authenticate(
70+
configureController: { _ in },
71+
nonce: cryptoClient.sha256(Data(rawNonce.utf8)).base64EncodedString()
72+
)
73+
74+
// the idToken is the JWT
75+
return authenticateResult.idToken
76+
}
6277
}
6378
}
6479

0 commit comments

Comments
 (0)