From 7ec85a8c65916e9b9a366340455ee9a0aa683bd0 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Wed, 12 Apr 2023 21:12:15 +0800 Subject: [PATCH 1/7] feat: add pkce option to signup --- src/GoTrueClient.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index ecec5fde5..778dc8aef 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -271,6 +271,13 @@ export default class GoTrueClient { let res: AuthResponse if ('email' in credentials) { const { email, password, options } = credentials + // TODO(joel): Refactor pkce check into helper + let codeChallenge: string | null = null + if (this.flowType === 'pkce') { + const codeVerifier = generatePKCEVerifier() + await setItemAsync(this.storage, `${this.storageKey}-code-verifier`, codeVerifier) + const codeChallenge = await generatePKCEChallenge(codeVerifier) + } res = await _request(this.fetch, 'POST', `${this.url}/signup`, { headers: this.headers, redirectTo: options?.emailRedirectTo, @@ -279,6 +286,8 @@ export default class GoTrueClient { password, data: options?.data ?? {}, gotrue_meta_security: { captcha_token: options?.captchaToken }, + code_challenge: codeChallenge, + code_challenge_method: codeChallenge ? 's256' : null, }, xform: _sessionResponse, }) From 276f37d32ef2d2ccd3fe5ca96de695a8a57bb5a8 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Mon, 17 Apr 2023 18:34:55 +0800 Subject: [PATCH 2/7] feat: add PKCE methods to update user --- src/GoTrueClient.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 778dc8aef..5c9bf7e61 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -725,6 +725,14 @@ export default class GoTrueClient { if (!sessionData.session) { throw new AuthSessionMissingError() } + let codeChallenge: string | null = null + let codeChallengeMethod: string | null = null + if (this.flowType === 'pkce') { + const codeVerifier = generatePKCEVerifier() + await setItemAsync(this.storage, `${this.storageKey}-code-verifier`, codeVerifier) + codeChallenge = await generatePKCEChallenge(codeVerifier) + codeChallengeMethod = codeVerifier === codeChallenge ? 'plain' : 's256' + } const session: Session = sessionData.session const { data, error: userError } = await _request(this.fetch, 'PUT', `${this.url}/user`, { headers: this.headers, @@ -732,6 +740,8 @@ export default class GoTrueClient { body: attributes, jwt: session.access_token, xform: _userResponse, + code_challenge: codeChallenge, + code_challenge_method: codeChallengeMethod, }) if (userError) throw userError session.user = data.user as User From 03c5a54366bcfba39c78211aea191997cdd3b39d Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 18 Apr 2023 01:02:08 +0800 Subject: [PATCH 3/7] fix: move code_challenge and method into body --- src/GoTrueClient.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 5c9bf7e61..52a697571 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -737,11 +737,13 @@ export default class GoTrueClient { const { data, error: userError } = await _request(this.fetch, 'PUT', `${this.url}/user`, { headers: this.headers, redirectTo: options?.emailRedirectTo, - body: attributes, + body: { + ...attributes, + code_challenge: codeChallenge, + code_challenge_method: codeChallengeMethod, + }, jwt: session.access_token, xform: _userResponse, - code_challenge: codeChallenge, - code_challenge_method: codeChallengeMethod, }) if (userError) throw userError session.user = data.user as User From a64b549d0746106f415c956b8f02cd4f7d509309 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 18 Apr 2023 15:59:02 +0800 Subject: [PATCH 4/7] fix: convert codeVerifier and codeChallenge into let vars --- src/GoTrueClient.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 52a697571..871f963ba 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -271,12 +271,12 @@ export default class GoTrueClient { let res: AuthResponse if ('email' in credentials) { const { email, password, options } = credentials - // TODO(joel): Refactor pkce check into helper let codeChallenge: string | null = null + let codeVerifier: string | null = null if (this.flowType === 'pkce') { - const codeVerifier = generatePKCEVerifier() + codeVerifier = generatePKCEVerifier() await setItemAsync(this.storage, `${this.storageKey}-code-verifier`, codeVerifier) - const codeChallenge = await generatePKCEChallenge(codeVerifier) + codeChallenge = await generatePKCEChallenge(codeVerifier) } res = await _request(this.fetch, 'POST', `${this.url}/signup`, { headers: this.headers, From e2833c0ed41f1a2df449f16972e26d5b5c63d536 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 18 Apr 2023 16:11:41 +0800 Subject: [PATCH 5/7] refactor: support plain for signup --- src/GoTrueClient.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 2d60ab446..64ae5f0a9 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -272,11 +272,12 @@ export default class GoTrueClient { if ('email' in credentials) { const { email, password, options } = credentials let codeChallenge: string | null = null - let codeVerifier: string | null = null + let codeChallengeMethod: string | null = null if (this.flowType === 'pkce') { - codeVerifier = generatePKCEVerifier() + const codeVerifier = generatePKCEVerifier() await setItemAsync(this.storage, `${this.storageKey}-code-verifier`, codeVerifier) codeChallenge = await generatePKCEChallenge(codeVerifier) + codeChallengeMethod = codeVerifier === codeChallenge ? 'plain' : 's256' } res = await _request(this.fetch, 'POST', `${this.url}/signup`, { headers: this.headers, From 180c62025a0dcf99355398c37bbdf4e12cbb298f Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 18 Apr 2023 16:15:22 +0800 Subject: [PATCH 6/7] fix: remove redundant params for code_challenge_method --- src/GoTrueClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 64ae5f0a9..dee2d5ab9 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -288,7 +288,7 @@ export default class GoTrueClient { data: options?.data ?? {}, gotrue_meta_security: { captcha_token: options?.captchaToken }, code_challenge: codeChallenge, - code_challenge_method: codeChallenge ? 's256' : null, + code_challenge_method: codeChallengeMethod, }, xform: _sessionResponse, }) From 88753a433aa1dadf104264280dda895bb50f7e76 Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Tue, 18 Apr 2023 23:53:23 +0800 Subject: [PATCH 7/7] fix: remove updateUser method --- src/GoTrueClient.ts | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index dee2d5ab9..6245ae94e 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -728,23 +728,11 @@ export default class GoTrueClient { if (!sessionData.session) { throw new AuthSessionMissingError() } - let codeChallenge: string | null = null - let codeChallengeMethod: string | null = null - if (this.flowType === 'pkce') { - const codeVerifier = generatePKCEVerifier() - await setItemAsync(this.storage, `${this.storageKey}-code-verifier`, codeVerifier) - codeChallenge = await generatePKCEChallenge(codeVerifier) - codeChallengeMethod = codeVerifier === codeChallenge ? 'plain' : 's256' - } const session: Session = sessionData.session const { data, error: userError } = await _request(this.fetch, 'PUT', `${this.url}/user`, { headers: this.headers, redirectTo: options?.emailRedirectTo, - body: { - ...attributes, - code_challenge: codeChallenge, - code_challenge_method: codeChallengeMethod, - }, + body: attributes, jwt: session.access_token, xform: _userResponse, })