From 5e27f84976e00ff554b929fe6c1cf640544e0d4c Mon Sep 17 00:00:00 2001 From: Chris Cassano Date: Tue, 20 Feb 2024 17:53:11 -0800 Subject: [PATCH] i think i have everything needed to support both Google and Google JWT --- package.json | 2 +- packages/constants/src/lib/enums.ts | 2 + .../src/lib/lit-auth-client.ts | 11 +++++ .../src/lib/providers/GoogleProvider.ts | 44 ++++++++++++++----- packages/types/src/lib/interfaces.ts | 4 ++ yarn.lock | 7 ++- 6 files changed, 56 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 01c819f70e..5ffdbeeca8 100644 --- a/package.json +++ b/package.json @@ -192,7 +192,7 @@ "nx": "^14.5.10", "nx-plugin-devkit": "^1.3.1", "path": "^0.12.7", - "prettier": "^2.6.2", + "prettier": "^3.2.5", "react-router-dom": "^6.4.5", "react-test-renderer": "18.0.0", "start-server-and-test": "^1.14.0", diff --git a/packages/constants/src/lib/enums.ts b/packages/constants/src/lib/enums.ts index 6312ea041f..7389bddfde 100644 --- a/packages/constants/src/lib/enums.ts +++ b/packages/constants/src/lib/enums.ts @@ -66,6 +66,8 @@ export enum LitNetwork { export enum ProviderType { Discord = 'discord', Google = 'google', + GoogleJwt = 'googleJwt', + GoogleBearer = 'googleBearer', EthWallet = 'ethwallet', WebAuthn = 'webauthn', Apple = 'apple', diff --git a/packages/lit-auth-client/src/lib/lit-auth-client.ts b/packages/lit-auth-client/src/lib/lit-auth-client.ts index fe935592dd..58e5e719fb 100644 --- a/packages/lit-auth-client/src/lib/lit-auth-client.ts +++ b/packages/lit-auth-client/src/lib/lit-auth-client.ts @@ -162,21 +162,32 @@ export class LitAuthClient { log('resolving provider of type: ', type); switch (type) { case 'google': + case 'googleJwt': provider = new GoogleProvider({ ...baseParams, ...(options as OAuthProviderOptions), + authMethodType: AuthMethodType.GoogleJwt, }) as unknown as T; break; + case 'googleBearer': + provider = new GoogleProvider({ + ...baseParams, + ...(options as OAuthProviderOptions), + authMethodType: AuthMethodType.Google, + }) as unknown as T; + break; case 'apple': provider = new AppleProvider({ ...baseParams, ...(options as OAuthProviderOptions), + authMethodType: AuthMethodType.AppleJwt, }) as unknown as T; break; case 'discord': provider = new DiscordProvider({ ...baseParams, ...(options as OAuthProviderOptions), + authMethodType: AuthMethodType.Discord, }) as unknown as T; break; case 'ethwallet': diff --git a/packages/lit-auth-client/src/lib/providers/GoogleProvider.ts b/packages/lit-auth-client/src/lib/providers/GoogleProvider.ts index 424822760b..7a41c016f0 100644 --- a/packages/lit-auth-client/src/lib/providers/GoogleProvider.ts +++ b/packages/lit-auth-client/src/lib/providers/GoogleProvider.ts @@ -20,10 +20,16 @@ export default class GoogleProvider extends BaseProvider { * The redirect URI that Lit's login server should send the user back to */ public redirectUri: string; + + /** + * The actual AuthMethodType for GoogleProvider. This can be either Google or GoogleJwt. + */ + public authMethodType: AuthMethodType; constructor(options: BaseProviderOptions & OAuthProviderOptions) { super(options); this.redirectUri = options.redirectUri || window.location.origin; + this.authMethodType = options.authMethodType || AuthMethodType.GoogleJwt; } /** @@ -65,7 +71,7 @@ export default class GoogleProvider extends BaseProvider { } // Check url for params - const { provider, idToken, state, error } = parseLoginParams( + const { provider, idToken, state, error, accessToken } = parseLoginParams( window.location.search ); @@ -95,18 +101,32 @@ export default class GoogleProvider extends BaseProvider { window.location.pathname ); - // Check if id token is present in url - if (!idToken) { - throw new Error( - `Missing ID token in redirect callback URL for Google OAuth"` - ); + if (this.authMethodType === AuthMethodType.Google) { + // Check if access token is present in url + if (!accessToken) { + throw new Error( + `Missing ID token in redirect callback URL for Google OAuth"` + ); + } + const authMethod = { + authMethodType: AuthMethodType.Google, + accessToken: accessToken, + }; + return authMethod; + } + else { + // Check if id token is present in url + if (!idToken) { + throw new Error( + `Missing ID token in redirect callback URL for Google OAuth"` + ); + } + const authMethod = { + authMethodType: AuthMethodType.GoogleJwt, + accessToken: idToken, + }; + return authMethod; } - - const authMethod = { - authMethodType: AuthMethodType.GoogleJwt, - accessToken: idToken, - }; - return authMethod; } /** diff --git a/packages/types/src/lib/interfaces.ts b/packages/types/src/lib/interfaces.ts index 7a930bddd2..54853cf926 100644 --- a/packages/types/src/lib/interfaces.ts +++ b/packages/types/src/lib/interfaces.ts @@ -1377,6 +1377,10 @@ export interface OAuthProviderOptions { * OAuth client ID */ clientId?: string; + /** + * The actual auth method type number + */ + authMethodType?: number; } export interface EthWalletProviderOptions { diff --git a/yarn.lock b/yarn.lock index 7f7126ee30..b4df917566 100644 --- a/yarn.lock +++ b/yarn.lock @@ -22110,11 +22110,16 @@ prettier-linter-helpers@^1.0.0: dependencies: fast-diff "^1.1.2" -prettier@^2.0.5, prettier@^2.3.1, prettier@^2.6.2, prettier@^2.7.1: +prettier@^2.0.5, prettier@^2.3.1, prettier@^2.7.1: version "2.8.8" resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da" integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q== +prettier@^3.2.5: + version "3.2.5" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.5.tgz#e52bc3090586e824964a8813b09aba6233b28368" + integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A== + pretty-bytes@^5.6.0: version "5.6.0" resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-5.6.0.tgz#356256f643804773c82f64723fe78c92c62beaeb"