Skip to content

Commit

Permalink
fix(ManageTokens): manage tokens does not work for http [#953]
Browse files Browse the repository at this point in the history
  • Loading branch information
vitshev committed Feb 6, 2025
1 parent 4c84b01 commit 03fdc67
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from 'axios';
import * as React from 'react';
import {useSelector} from 'react-redux';
import {Alert, Dialog} from '@gravity-ui/uikit';
import {sha256} from '../../../utils/sha256';
import {Alert, Dialog, Link} from '@gravity-ui/uikit';
import {isCryptoSubtleAvailable} from '../../../utils/sha256';
import {createPasswordStrategy} from './password-strategies';
import {YTDFDialog, makeErrorFields} from '../../../components/Dialog';
import {getCurrentUserName, getSettingsCluster} from '../../../store/selectors/global';
import {YTError} from '../../../../@types/types';
Expand Down Expand Up @@ -31,6 +31,7 @@ const PasswordModal = (props: PasswordModalProps) => {
<Dialog open={props.visible} hasCloseButton={true} onClose={props.handleCancel}>
<YTDFDialog<{
password: string;
sha256_password: string;
}>
headerProps={{
title: 'Authentication',
Expand All @@ -41,26 +42,43 @@ const PasswordModal = (props: PasswordModalProps) => {
initialValues={{}}
onAdd={async (data) => {
setError(undefined);

const password = data.getState().values.password;

return sha256(password).then((password_sha256) => {
axios
.post(`/api/yt/${ytAuthCluster}/login`, {
username,
password,
})
.then(() => props.handleConfirm(password_sha256))
.catch((error) => setError(error?.response?.data || error));
});
const {password, sha256_password} = data.getState().values;

const strategy = createPasswordStrategy(
username,
ytAuthCluster,
isCryptoSubtleAvailable(),
);

return strategy(sha256_password || password)
.then((hashedPassword) => {
props.handleConfirm(hashedPassword);
})
.catch(setError);
}}
fields={[
{
name: 'error-block',
type: 'block',
extras: {
children: (
children: isCryptoSubtleAvailable() ? (
<Alert message="To access tokens management, you need enter your password" />
) : (
<Alert
message={
<span>
<Link
target="_blank"
href="https://developer.mozilla.org/en-US/docs/Web/API/Crypto/subtle"
>
crypto.subtle
</Link>{' '}
is not available from your browser, so SHA-256 hash
cannot be generated. Please generate the hash of
your password by self.
</span>
}
/>
),
},
},
Expand All @@ -70,6 +88,20 @@ const PasswordModal = (props: PasswordModalProps) => {
required: true,
caption: 'Password',
extras: () => ({type: 'password'}),
visibilityCondition: {
when: '',
isActive: () => isCryptoSubtleAvailable(),
},
},
{
name: 'sha256_password',
type: 'text',
required: true,
caption: 'Password Hash (SHA-256)',
visibilityCondition: {
when: '',
isActive: () => !isCryptoSubtleAvailable(),
},
},
...makeErrorFields([error]),
]}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import axios from 'axios';
import {sha256} from '../../../utils/sha256';

type PasswordStrategy = (password: string) => Promise<string>;

const regularPasswordStrategy = (username: string, ytAuthCluster: string) => {
return async (password: string): Promise<string> => {
const hashedPassword = await sha256(password);
await axios.post(`/api/yt/${ytAuthCluster}/login`, {
username,
password,
});

return hashedPassword;
};
};

const sha256PasswordStrategy = () => {
return async (sha256Password: string): Promise<string> => {
return sha256Password;
};
};

export function createPasswordStrategy(
username: string,
ytAuthCluster: string,
isSha256Mode: boolean,
): PasswordStrategy {
return isSha256Mode
? regularPasswordStrategy(username, ytAuthCluster)
: sha256PasswordStrategy();
}
4 changes: 4 additions & 0 deletions packages/ui/src/ui/utils/sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export async function sha256(str: string) {

return [...new Uint8Array(buf)].map((x) => ('00' + x.toString(16)).slice(-2)).join('');
}

export const isCryptoSubtleAvailable = () => {
return Boolean(crypto?.subtle?.digest);
};

0 comments on commit 03fdc67

Please sign in to comment.