-
Notifications
You must be signed in to change notification settings - Fork 0
feat!: support oauth authorization code flow (#906) #905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92c9933
feat: apply hannes' poc for oauth authorization code flow
frano-m d7866d2
feat: make oauth authorize url configurable on oauthprovider
frano-m dad07c1
fix: handle authorize fetch failures and missing access tokens (#906)
frano-m 7957b1d
refactor: make codeResponse fields optional (#907)
frano-m ace57fa
refactor: split google service into implicit flow and authorization c…
frano-m 2236ef2
refactor: add flow discriminator to oauthprovider (#906)
frano-m c9d8b28
fix: restore globalThis.fetch instead of deleting it (#906)
frano-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,75 +1,31 @@ | ||
| import { requestAuth, resetAuthState } from "../auth/dispatch/auth"; | ||
| import { | ||
| requestAuthentication, | ||
| resetAuthenticationState, | ||
| updateAuthentication, | ||
| } from "../auth/dispatch/authentication"; | ||
| import { resetCredentialsState } from "../auth/dispatch/credentials"; | ||
| import { resetTokenState, updateToken } from "../auth/dispatch/token"; | ||
| import { AUTHENTICATION_STATUS } from "../auth/types/authentication"; | ||
| import { OAuthProvider } from "../config/entities"; | ||
| import { GoogleProfile, SessionDispatch, TokenSetParameters } from "./types"; | ||
| import { fetchProfile, getAuthenticationRequestOptions } from "./utils/auth"; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO see https://github.com/clevercanary/data-browser/issues/544. | ||
| declare const google: any; | ||
| import { OAUTH_FLOW, type OAuthProvider } from "../config/entities"; | ||
| import { login as authorizationCodeFlowLogin } from "./services/authorizationCodeFlow"; | ||
| import { logout, type LoginDispatch } from "./services/common"; | ||
| import { login as implicitFlowLogin } from "./services/implicitFlow"; | ||
| import type { SessionDispatch } from "./types"; | ||
|
|
||
| /** | ||
| * Google Sign-In service. | ||
| */ | ||
| export const service = { | ||
| /** | ||
| * Login with Google OAuth. | ||
| * Dispatches to the configured flow based on `provider.flow`. | ||
| * @param provider - OAuth provider configuration. | ||
| * @param dispatch - Dispatch functions for auth state. | ||
| */ | ||
| login: ( | ||
| provider: OAuthProvider, | ||
| dispatch: Pick< | ||
| SessionDispatch, | ||
| "authDispatch" | "authenticationDispatch" | "tokenDispatch" | ||
| >, | ||
| ): void => { | ||
| const client = google.accounts.oauth2.initTokenClient({ | ||
| callback: (response: TokenSetParameters) => { | ||
| const { id, profile, userinfo } = provider; | ||
| const { access_token: token } = response; | ||
| dispatch.authDispatch?.(requestAuth()); | ||
| dispatch.authenticationDispatch?.(requestAuthentication()); | ||
| dispatch.tokenDispatch?.(updateToken({ providerId: id, token })); | ||
| fetchProfile(userinfo, getAuthenticationRequestOptions(token), { | ||
| onError: () => { | ||
| dispatch.authDispatch?.(resetAuthState()); | ||
| dispatch.authenticationDispatch?.( | ||
| updateAuthentication({ | ||
| profile: undefined, | ||
| status: AUTHENTICATION_STATUS.SETTLED, | ||
| }), | ||
| ); | ||
| dispatch.tokenDispatch?.(resetTokenState()); | ||
| }, | ||
| onSuccess: (r: GoogleProfile) => | ||
| dispatch.authenticationDispatch?.( | ||
| updateAuthentication({ | ||
| profile: profile(r), | ||
| status: AUTHENTICATION_STATUS.PENDING, // Authentication is pending until session controller is resolved. | ||
| }), | ||
| ), | ||
| }); | ||
| }, | ||
| client_id: provider.clientId, | ||
| scope: provider.authorization.params.scope, | ||
| }); | ||
| client.requestAccessToken(); | ||
| login: (provider: OAuthProvider, dispatch: LoginDispatch): void => { | ||
| if (provider.flow === OAUTH_FLOW.AUTHORIZATION_CODE) { | ||
| authorizationCodeFlowLogin(provider, dispatch); | ||
| } else { | ||
| implicitFlowLogin(provider, dispatch); | ||
| } | ||
| }, | ||
| /** | ||
| * Logout and clear all auth state. | ||
| * @param dispatch - Dispatch functions for auth state. | ||
| */ | ||
| logout: (dispatch: SessionDispatch): void => { | ||
| dispatch.authDispatch?.(resetAuthState()); | ||
| dispatch.authenticationDispatch?.(resetAuthenticationState()); | ||
| dispatch.credentialsDispatch?.(resetCredentialsState()); | ||
| dispatch.tokenDispatch?.(resetTokenState()); | ||
| logout(dispatch); | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import type { AuthorizationCodeFlowProvider } from "../../config/entities"; | ||
| import type { CodeResponse, TokenSetParameters } from "../types"; | ||
| import { | ||
| createOnAccessToken, | ||
| createResetSession, | ||
| type LoginDispatch, | ||
| } from "./common"; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO see https://github.com/clevercanary/data-browser/issues/544. | ||
| declare const google: any; | ||
|
|
||
| /** | ||
| * Login using the OAuth 2.0 authorization code flow. | ||
| * Uses Google's initCodeClient to request an authorization code, | ||
| * then exchanges it for an access token via the configured authorize endpoint. | ||
| * @param provider - OAuth provider configuration. | ||
| * @param dispatch - Dispatch functions for auth state. | ||
|
frano-m marked this conversation as resolved.
|
||
| */ | ||
| export function login( | ||
| provider: AuthorizationCodeFlowProvider, | ||
| dispatch: LoginDispatch, | ||
| ): void { | ||
| const { authorize } = provider; | ||
| const resetSession = createResetSession(dispatch); | ||
| const onAccessToken = createOnAccessToken(provider, dispatch, resetSession); | ||
| const client = google.accounts.oauth2.initCodeClient({ | ||
| callback: (response: CodeResponse) => { | ||
|
frano-m marked this conversation as resolved.
|
||
| fetch(authorize, { | ||
| body: JSON.stringify(response), | ||
| headers: { "Content-Type": "application/json" }, | ||
| method: "POST", | ||
| }) | ||
| .then((r) => { | ||
| if (!r.ok) { | ||
| throw new Error(`authorize request failed (${r.status})`); | ||
| } | ||
| return r.json(); | ||
| }) | ||
| .then((tokens: TokenSetParameters) => { | ||
| if (!tokens?.access_token) { | ||
| throw new Error("authorize response missing access_token"); | ||
| } | ||
| onAccessToken(tokens.access_token); | ||
| }) | ||
| .catch(resetSession); | ||
| }, | ||
| client_id: provider.clientId, | ||
| scope: provider.authorization.params.scope, | ||
| }); | ||
| client.requestCode(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { requestAuth, resetAuthState } from "../../auth/dispatch/auth"; | ||
| import { | ||
| requestAuthentication, | ||
| resetAuthenticationState, | ||
| updateAuthentication, | ||
| } from "../../auth/dispatch/authentication"; | ||
| import { resetCredentialsState } from "../../auth/dispatch/credentials"; | ||
| import { resetTokenState, updateToken } from "../../auth/dispatch/token"; | ||
| import { AUTHENTICATION_STATUS } from "../../auth/types/authentication"; | ||
| import type { OAuthProvider } from "../../config/entities"; | ||
| import type { GoogleProfile, SessionDispatch } from "../types"; | ||
| import { fetchProfile, getAuthenticationRequestOptions } from "../utils/auth"; | ||
|
|
||
| export type LoginDispatch = Pick< | ||
| SessionDispatch, | ||
| "authDispatch" | "authenticationDispatch" | "tokenDispatch" | ||
| >; | ||
|
|
||
| /** | ||
| * Creates a function that resets the session state on auth failure. | ||
| * @param dispatch - Dispatch functions for auth state. | ||
| * @returns reset session function. | ||
| */ | ||
| export function createResetSession(dispatch: LoginDispatch): () => void { | ||
| return (): void => { | ||
| dispatch.authDispatch?.(resetAuthState()); | ||
| dispatch.authenticationDispatch?.( | ||
| updateAuthentication({ | ||
| profile: undefined, | ||
| status: AUTHENTICATION_STATUS.SETTLED, | ||
| }), | ||
| ); | ||
| dispatch.tokenDispatch?.(resetTokenState()); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a function that handles a successful access token. | ||
| * Dispatches auth state updates and fetches the user profile. | ||
| * @param provider - OAuth provider configuration. | ||
| * @param dispatch - Dispatch functions for auth state. | ||
| * @param resetSession - Reset session function. | ||
| * @returns on access token function. | ||
| */ | ||
| export function createOnAccessToken( | ||
| provider: OAuthProvider, | ||
| dispatch: LoginDispatch, | ||
| resetSession: () => void, | ||
| ): (token: string) => void { | ||
| const { id, profile, userinfo } = provider; | ||
| return (token: string): void => { | ||
| dispatch.authDispatch?.(requestAuth()); | ||
| dispatch.authenticationDispatch?.(requestAuthentication()); | ||
| dispatch.tokenDispatch?.(updateToken({ providerId: id, token })); | ||
| fetchProfile(userinfo, getAuthenticationRequestOptions(token), { | ||
| onError: resetSession, | ||
| onSuccess: (r: GoogleProfile) => | ||
| dispatch.authenticationDispatch?.( | ||
| updateAuthentication({ | ||
|
Comment on lines
+55
to
+59
|
||
| profile: profile(r), | ||
| status: AUTHENTICATION_STATUS.PENDING, | ||
| }), | ||
| ), | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Logout and clear all auth state. | ||
| * @param dispatch - Dispatch functions for auth state. | ||
|
frano-m marked this conversation as resolved.
|
||
| */ | ||
| export function logout(dispatch: SessionDispatch): void { | ||
| dispatch.authDispatch?.(resetAuthState()); | ||
| dispatch.authenticationDispatch?.(resetAuthenticationState()); | ||
| dispatch.credentialsDispatch?.(resetCredentialsState()); | ||
| dispatch.tokenDispatch?.(resetTokenState()); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { ImplicitFlowProvider } from "../../config/entities"; | ||
| import type { TokenSetParameters } from "../types"; | ||
| import { | ||
| createOnAccessToken, | ||
| createResetSession, | ||
| type LoginDispatch, | ||
| } from "./common"; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO see https://github.com/clevercanary/data-browser/issues/544. | ||
| declare const google: any; | ||
|
|
||
| /** | ||
| * Login using the OAuth 2.0 implicit flow. | ||
| * Uses Google's initTokenClient to request an access token directly. | ||
| * @param provider - OAuth provider configuration. | ||
| * @param dispatch - Dispatch functions for auth state. | ||
|
frano-m marked this conversation as resolved.
|
||
| */ | ||
| export function login( | ||
| provider: ImplicitFlowProvider, | ||
| dispatch: LoginDispatch, | ||
| ): void { | ||
| const resetSession = createResetSession(dispatch); | ||
| const onAccessToken = createOnAccessToken(provider, dispatch, resetSession); | ||
| const client = google.accounts.oauth2.initTokenClient({ | ||
| callback: (response: TokenSetParameters) => | ||
| onAccessToken(response.access_token), | ||
|
Comment on lines
+25
to
+26
|
||
| client_id: provider.clientId, | ||
| scope: provider.authorization.params.scope, | ||
| }); | ||
| client.requestAccessToken(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.