Skip to content

Commit

Permalink
i think i have everything needed to support both Google and Google JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
glitch003 committed Feb 21, 2024
1 parent 91b3381 commit 5e27f84
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/constants/src/lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export enum LitNetwork {
export enum ProviderType {
Discord = 'discord',
Google = 'google',
GoogleJwt = 'googleJwt',
GoogleBearer = 'googleBearer',
EthWallet = 'ethwallet',
WebAuthn = 'webauthn',
Apple = 'apple',
Expand Down
11 changes: 11 additions & 0 deletions packages/lit-auth-client/src/lib/lit-auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
44 changes: 32 additions & 12 deletions packages/lit-auth-client/src/lib/providers/GoogleProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
);

Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,10 @@ export interface OAuthProviderOptions {
* OAuth client ID
*/
clientId?: string;
/**
* The actual auth method type number
*/
authMethodType?: number;
}

export interface EthWalletProviderOptions {
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5e27f84

Please sign in to comment.