Skip to content

Commit

Permalink
chore(ui): bump oauth4webapi major version (akuity#3039)
Browse files Browse the repository at this point in the history
Signed-off-by: Mayursinh Sarvaiya <[email protected]>
  • Loading branch information
Marvin9 authored and fykaa committed Dec 20, 2024
1 parent 1550ef0 commit c70d73c
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 48 deletions.
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"moment": "^2.30.1",
"monaco-editor": "^0.52.0",
"monaco-yaml": "^5.2.2",
"oauth4webapi": "^2.17.0",
"oauth4webapi": "^3.1.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.53.0",
Expand Down
10 changes: 5 additions & 5 deletions ui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 63 additions & 35 deletions ui/src/features/auth/oidc-login.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { useQuery } from '@tanstack/react-query';
import { Button, notification } from 'antd';
import * as oauth from 'oauth4webapi';
import {
discoveryRequest,
processDiscoveryResponse,
generateRandomCodeVerifier,
calculatePKCECodeChallenge,
validateAuthResponse,
authorizationCodeGrantRequest,
processAuthorizationCodeResponse,
AuthorizationResponseError,
WWWAuthenticateChallengeError,
allowInsecureRequests
} from 'oauth4webapi';
import React from 'react';
import { useLocation } from 'react-router-dom';

import { OIDCConfig } from '@ui/gen/service/v1alpha1/service_pb';

import { useAuthContext } from './context/use-auth-context';
import { oidcClientAuth, shouldAllowIdpHttpRequest as shouldAllowHttpRequest } from './utils';

const codeVerifierKey = 'PKCE_code_verifier';

Expand Down Expand Up @@ -44,9 +56,10 @@ export const OIDCLogin = ({ oidcConfig }: Props) => {
queryKey: [issuerUrl],
queryFn: () =>
issuerUrl &&
oauth
.discoveryRequest(issuerUrl)
.then((response) => oauth.processDiscoveryResponse(issuerUrl, response))
discoveryRequest(issuerUrl, {
[allowInsecureRequests]: shouldAllowHttpRequest()
})
.then((response) => processDiscoveryResponse(issuerUrl, response))
.then((response) => {
if (response.code_challenge_methods_supported?.includes('S256') !== true) {
throw new Error('OIDC config fetch error');
Expand All @@ -60,7 +73,7 @@ export const OIDCLogin = ({ oidcConfig }: Props) => {
React.useEffect(() => {
if (error) {
const errorMessage = error instanceof Error ? error.message : 'OIDC config fetch error';
notification.error({ message: errorMessage, placement: 'bottomRight' });
notification.error({ message: `OIDC: ${errorMessage}`, placement: 'bottomRight' });
}
}, [error]);

Expand All @@ -69,10 +82,10 @@ export const OIDCLogin = ({ oidcConfig }: Props) => {
return;
}

const code_verifier = oauth.generateRandomCodeVerifier();
const code_verifier = generateRandomCodeVerifier();
sessionStorage.setItem(codeVerifierKey, code_verifier);

const code_challenge = await oauth.calculatePKCECodeChallenge(code_verifier);
const code_challenge = await calculatePKCECodeChallenge(code_verifier);
const url = new URL(as.authorization_endpoint);
url.searchParams.set('client_id', client.client_id);
url.searchParams.set('code_challenge', code_challenge);
Expand Down Expand Up @@ -106,42 +119,57 @@ export const OIDCLogin = ({ oidcConfig }: Props) => {
searchParams.delete('state');
}

const params = oauth.validateAuthResponse(as, client, searchParams, oauth.expectNoState);
try {
const params = validateAuthResponse(as, client, searchParams);

const response = await authorizationCodeGrantRequest(
as,
client,
oidcClientAuth,
params,
redirectURI,
code_verifier,
{
[allowInsecureRequests]: shouldAllowHttpRequest(),
additionalParameters: [['client_id', client.client_id]]
}
);

if (oauth.isOAuth2Error(params)) {
notification.error({
message: 'OIDC: Validation Auth Response error',
placement: 'bottomRight'
const result = await processAuthorizationCodeResponse(as, client, response, {
requireIdToken: true
});
return;
}

const response = await oauth.authorizationCodeGrantRequest(
as,
client,
params,
redirectURI,
code_verifier
);
if (!result.id_token) {
notification.error({
message: 'OIDC: Proccess Authorization Code Grant Response error',
placement: 'bottomRight'
});
return;
}

onLogin(result.id_token, result.refresh_token);
} catch (err) {
if (err instanceof AuthorizationResponseError) {
notification.error({
message: 'OIDC: Validation Auth Response error',
placement: 'bottomRight'
});
return;
}

if (err instanceof WWWAuthenticateChallengeError) {
notification.error({
message: 'OIDC: Parsing Authenticate Challenges error',
placement: 'bottomRight'
});
return;
}

if (oauth.parseWwwAuthenticateChallenges(response)) {
notification.error({
message: 'OIDC: Parsing Authenticate Challenges error',
message: `OIDC: ${JSON.stringify(err)}`,
placement: 'bottomRight'
});
return;
}

const result = await oauth.processAuthorizationCodeOpenIDResponse(as, client, response);
if (oauth.isOAuth2Error(result) || !result.id_token) {
notification.error({
message: 'OIDC: Proccess Authorization Code Grant Response error',
placement: 'bottomRight'
});
return;
}

onLogin(result.id_token, result.refresh_token);
})();
}, [as, client, location]);

Expand Down
26 changes: 19 additions & 7 deletions ui/src/features/auth/token-renew.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useQuery as useConnectQuery } from '@connectrpc/connect-query';
import { useQuery } from '@tanstack/react-query';
import { notification } from 'antd';
import * as oauth from 'oauth4webapi';
import {
allowInsecureRequests,
discoveryRequest,
processDiscoveryResponse,
refreshTokenGrantRequest,
processRefreshTokenResponse
} from 'oauth4webapi';
import React from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';

Expand All @@ -12,6 +18,7 @@ import { getPublicConfig } from '@ui/gen/service/v1alpha1/service-KargoService_c
import { LoadingState } from '../common';

import { useAuthContext } from './context/use-auth-context';
import { oidcClientAuth, shouldAllowIdpHttpRequest as shouldAllowHttpRequest } from './utils';

export const TokenRenew = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -44,9 +51,10 @@ export const TokenRenew = () => {
queryKey: [issuerUrl],
queryFn: () =>
issuerUrl &&
oauth
.discoveryRequest(issuerUrl)
.then((response) => oauth.processDiscoveryResponse(issuerUrl, response))
discoveryRequest(issuerUrl, {
[allowInsecureRequests]: shouldAllowHttpRequest()
})
.then((response) => processDiscoveryResponse(issuerUrl, response))
.then((response) => {
if (response.code_challenge_methods_supported?.includes('S256') !== true) {
throw new Error('OIDC config fetch error');
Expand All @@ -71,10 +79,14 @@ export const TokenRenew = () => {
}

(async () => {
const response = await oauth.refreshTokenGrantRequest(as, client, refreshToken);
const response = await refreshTokenGrantRequest(as, client, oidcClientAuth, refreshToken, {
[allowInsecureRequests]: shouldAllowHttpRequest(),
additionalParameters: [['client_id', client.client_id]]
});

const result = await processRefreshTokenResponse(as, client, response);

const result = await oauth.processRefreshTokenResponse(as, client, response);
if (oauth.isOAuth2Error(result) || !result.id_token) {
if (!result.id_token) {
notification.error({
message: 'OIDC: Proccess Authorization Code Grant Response error',
placement: 'bottomRight'
Expand Down
8 changes: 8 additions & 0 deletions ui/src/features/auth/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ClientAuth } from 'oauth4webapi';

// https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
export type JWTInfo = {
sub: string;
Expand Down Expand Up @@ -38,3 +40,9 @@ export const getUserEmail = (user?: JWTInfo | null) => {

return meta;
};

export const oidcClientAuth: ClientAuth = () => {
// equivalent function for token_endpoint_auth_method: 'none'
};

export const shouldAllowIdpHttpRequest = () => __UI_VERSION__ === 'development';

0 comments on commit c70d73c

Please sign in to comment.