Skip to content

Commit

Permalink
Implemented PKCE for signInWithOTP
Browse files Browse the repository at this point in the history
  • Loading branch information
ljubiknenad committed Jul 31, 2023
1 parent ce08fd5 commit efd14c3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion Sources/GoTrue/GoTrueClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,25 @@ public final class GoTrueClient {
captchaToken: String? = nil
) async throws {
await env.sessionManager.remove()
var codeChallenge: String? = nil
var codeChallengeMethod: String? = nil

if flowType == .pkce {
codeVerifier = PKCE.generateCodeVerifier()
codeChallenge = PKCE.generateCodeChallenge(from: codeVerifier)
codeChallengeMethod = "S256"
}

try await env.client.send(
Paths.otp.post(
redirectTo: redirectTo,
.init(
email: email,
createUser: shouldCreateUser,
data: data,
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:))
gotrueMetaSecurity: captchaToken.map(GoTrueMetaSecurity.init(captchaToken:)),
codeChallenge: codeChallenge,
codeChallengeMethod: codeChallengeMethod
)
)
)
Expand Down
10 changes: 9 additions & 1 deletion Sources/GoTrue/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,25 @@ public struct OTPParams: Codable, Hashable, Sendable {
public var createUser: Bool
public var data: [String: AnyJSON]?
public var gotrueMetaSecurity: GoTrueMetaSecurity?
public var codeChallenge: String?
public var codeChallengeMethod: String?

public init(
email: String? = nil,
phone: String? = nil,
createUser: Bool? = nil,
data: [String: AnyJSON]? = nil,
gotrueMetaSecurity: GoTrueMetaSecurity? = nil
gotrueMetaSecurity: GoTrueMetaSecurity? = nil,
codeChallenge: String? = nil,
codeChallengeMethod: String? = nil
) {
self.email = email
self.phone = phone
self.createUser = createUser ?? true
self.data = data
self.gotrueMetaSecurity = gotrueMetaSecurity
self.codeChallenge = codeChallenge
self.codeChallengeMethod = codeChallengeMethod
}

public enum CodingKeys: String, CodingKey {
Expand All @@ -406,6 +412,8 @@ public struct OTPParams: Codable, Hashable, Sendable {
case createUser = "create_user"
case data
case gotrueMetaSecurity = "gotrue_meta_security"
case codeChallenge = "code_challenge"
case codeChallengeMethod = "code_challenge_method"
}
}

Expand Down

0 comments on commit efd14c3

Please sign in to comment.