@@ -457,6 +457,76 @@ public actor AuthClient {
457457 )
458458 }
459459
460+ /// Attempts a single-sign on using an enterprise Identity Provider.
461+ /// - Parameters:
462+ /// - domain: The email domain to use for signing in.
463+ /// - redirectTo: The URL to redirect the user to after they sign in with the third-party
464+ /// provider.
465+ /// - captchaToken: The captcha token to be used for captcha verification.
466+ /// - Returns: A URL that you can use to initiate the provider's authentication flow.
467+ public func signInWithSSO(
468+ domain: String ,
469+ redirectTo: URL ? = nil ,
470+ captchaToken: String ? = nil
471+ ) async throws -> SSOResponse {
472+ await sessionManager. remove ( )
473+
474+ let ( codeChallenge, codeChallengeMethod) = prepareForPKCE ( )
475+
476+ return try await api. execute (
477+ Request (
478+ path: " /sso " ,
479+ method: . post,
480+ body: configuration. encoder. encode (
481+ SignInWithSSORequest (
482+ providerId: nil ,
483+ domain: domain,
484+ redirectTo: redirectTo,
485+ gotrueMetaSecurity: captchaToken. map { AuthMetaSecurity ( captchaToken: $0) } ,
486+ codeChallenge: codeChallenge,
487+ codeChallengeMethod: codeChallengeMethod
488+ )
489+ )
490+ )
491+ )
492+ . decoded ( decoder: configuration. decoder)
493+ }
494+
495+ /// Attempts a single-sign on using an enterprise Identity Provider.
496+ /// - Parameters:
497+ /// - providerId: The ID of the SSO provider to use for signing in.
498+ /// - redirectTo: The URL to redirect the user to after they sign in with the third-party
499+ /// provider.
500+ /// - captchaToken: The captcha token to be used for captcha verification.
501+ /// - Returns: A URL that you can use to initiate the provider's authentication flow.
502+ public func signInWithSSO(
503+ providerId: String ,
504+ redirectTo: URL ? = nil ,
505+ captchaToken: String ? = nil
506+ ) async throws -> SSOResponse {
507+ await sessionManager. remove ( )
508+
509+ let ( codeChallenge, codeChallengeMethod) = prepareForPKCE ( )
510+
511+ return try await api. execute (
512+ Request (
513+ path: " /sso " ,
514+ method: . post,
515+ body: configuration. encoder. encode (
516+ SignInWithSSORequest (
517+ providerId: providerId,
518+ domain: nil ,
519+ redirectTo: redirectTo,
520+ gotrueMetaSecurity: captchaToken. map { AuthMetaSecurity ( captchaToken: $0) } ,
521+ codeChallenge: codeChallenge,
522+ codeChallengeMethod: codeChallengeMethod
523+ )
524+ )
525+ )
526+ )
527+ . decoded ( decoder: configuration. decoder)
528+ }
529+
460530 /// Log in an existing user by exchanging an Auth Code issued during the PKCE flow.
461531 public func exchangeCodeForSession( authCode: String ) async throws -> Session {
462532 guard let codeVerifier = try codeVerifierStorage. getCodeVerifier ( ) else {
@@ -945,29 +1015,29 @@ public actor AuthClient {
9451015 }
9461016
9471017 private func prepareForPKCE( ) -> ( codeChallenge: String ? , codeChallengeMethod: String ? ) {
948- if configuration. flowType == . pkce {
949- let codeVerifier = PKCE . generateCodeVerifier ( )
950-
951- do {
952- try codeVerifierStorage. storeCodeVerifier ( codeVerifier)
953- } catch {
954- assertionFailure (
955- """
956- An error occurred while storing the code verifier,
957- PKCE flow may not work as expected.
958-
959- Error: \( error. localizedDescription)
960- """
961- )
962- }
1018+ guard configuration. flowType == . pkce else {
1019+ return ( nil , nil )
1020+ }
1021+
1022+ let codeVerifier = PKCE . generateCodeVerifier ( )
9631023
964- let codeChallenge = PKCE . generateCodeChallenge ( from: codeVerifier)
965- let codeChallengeMethod = codeVerifier == codeChallenge ? " plain " : " s256 "
1024+ do {
1025+ try codeVerifierStorage. storeCodeVerifier ( codeVerifier)
1026+ } catch {
1027+ assertionFailure (
1028+ """
1029+ An error occurred while storing the code verifier,
1030+ PKCE flow may not work as expected.
9661031
967- return ( codeChallenge, codeChallengeMethod)
1032+ Error: \( error. localizedDescription)
1033+ """
1034+ )
9681035 }
9691036
970- return ( nil , nil )
1037+ let codeChallenge = PKCE . generateCodeChallenge ( from: codeVerifier)
1038+ let codeChallengeMethod = codeVerifier == codeChallenge ? " plain " : " s256 "
1039+
1040+ return ( codeChallenge, codeChallengeMethod)
9711041 }
9721042
9731043 private func isImplicitGrantFlow( url: URL ) -> Bool {
0 commit comments