Skip to content

Commit 9390a0b

Browse files
authored
Merge pull request #577 from stytchauth/oauth_attach_token
Add oauth_attach_token to 3rd party oauth requests
2 parents 5ebc480 + 911a126 commit 9390a0b

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public extension StytchClient.OAuth {
1818
/// Initiates the OAuth flow by using the included parameters to start a Sign In With Apple request. If the authentication is successful this method will return a new session object.
1919
public func start(parameters: StartParameters) async throws -> AuthenticateResponse {
2020
let rawNonce = try cryptoClient.dataWithRandomBytesOfCount(32).toHexString()
21+
2122
let authenticateResult = try await appleOAuthClient.authenticate(
2223
configureController: { controller in
2324
#if !os(watchOS)
@@ -26,6 +27,7 @@ public extension StytchClient.OAuth {
2627
},
2728
nonce: cryptoClient.sha256(Data(rawNonce.utf8)).base64EncodedString()
2829
)
30+
2931
let authenticateResponse: AuthenticateResponse = try await router.post(
3032
to: .authenticate,
3133
parameters: AuthenticateParameters(
@@ -34,6 +36,7 @@ public extension StytchClient.OAuth {
3436
sessionDurationMinutes: parameters.sessionDurationMinutes
3537
)
3638
)
39+
3740
if authenticateResult.name != nil {
3841
let _: BasicResponse = try await userRouter.put(
3942
to: .index,
@@ -42,6 +45,7 @@ public extension StytchClient.OAuth {
4245
)
4346
)
4447
}
48+
4549
_ = try await EventsClient.logEvent(
4650
parameters: .init(
4751
eventName: "apple_oauth_name_found",
@@ -97,7 +101,9 @@ public extension StytchClient.OAuth.Apple {
97101
self.sessionDurationMinutes = sessionDurationMinutes
98102
self.presentationContextProvider = presentationContextProvider
99103
}
104+
100105
#else
106+
101107
/// - Parameters:
102108
/// - sessionDurationMinutes: The duration, in minutes, of the requested session. Defaults to 5 minutes.
103109
public init(sessionDurationMinutes: Minutes = StytchClient.defaultSessionDuration) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public extension StytchClient.OAuth.ThirdParty {
3737
struct WebAuthenticationConfiguration: WebAuthenticationSessionClientConfiguration {
3838
let loginRedirectUrl: URL?
3939
let signupRedirectUrl: URL?
40+
let oauthAttachToken: String?
4041
let customScopes: [String]?
4142
let providerParams: [String: String]?
4243
public let clientType: ClientType = .consumer
@@ -50,16 +51,19 @@ public extension StytchClient.OAuth.ThirdParty {
5051
/// - Parameters:
5152
/// - loginRedirectUrl: The url an existing user is redirected to after authenticating with the identity provider. This url **must** use a custom scheme and be added to your Stytch Dashboard.
5253
/// - signupRedirectUrl: The url a new user is redirected to after authenticating with the identity provider. This url **must** use a custom scheme and be added to your Stytch Dashboard.
54+
/// - oauthAttachToken: A single-use token for connecting the Stytch User selection from an OAuth Attach request to the corresponding OAuth Start request.
5355
/// - customScopes: Any additional scopes to be requested from the identity provider.
5456
/// - providerParams: An optional mapping of provider specific values to pass through to the OAuth provider
5557
public init(
5658
loginRedirectUrl: URL? = nil,
5759
signupRedirectUrl: URL? = nil,
60+
oauthAttachToken: String? = nil,
5861
customScopes: [String]? = nil,
5962
providerParams: [String: String]? = nil
6063
) {
6164
self.loginRedirectUrl = loginRedirectUrl
6265
self.signupRedirectUrl = signupRedirectUrl
66+
self.oauthAttachToken = oauthAttachToken
6367
self.customScopes = customScopes
6468
self.providerParams = providerParams
6569
}
@@ -91,6 +95,10 @@ public extension StytchClient.OAuth.ThirdParty {
9195
queryParameters["signup_redirect_url"] = signupRedirectUrl
9296
}
9397

98+
if let oauthAttachToken = oauthAttachToken, oauthAttachToken.isEmpty == false {
99+
queryParameters["oauth_attach_token"] = oauthAttachToken
100+
}
101+
94102
let domain = Current.localStorage.stytchDomain(publicToken)
95103
guard let url = URL(string: "https://\(domain)/v1/public/oauth/\(providerName)/start?\(queryParameters.toURLParameters())") else {
96104
throw StytchSDKError.invalidStartURL

0 commit comments

Comments
 (0)