Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/provider-cloud-ui/src/pages/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
Flex,
Icon,
IconButton,
Text,
iconClose,
iconExpandAccordion,
Text,
} from '@waves.exchange/react-uikit';
import React, { FC, useCallback, useEffect, useRef, useState } from 'react';
import { IUser } from '../../interface';
/* eslint-disable sort-imports */
import { ForgotPassword } from '../../components/Auth/ForgotPassword';
import { SignInForm } from '../../components/Auth/SignInForm';
import { CodeConfirmation } from '../../components/Auth/CodeConfirmation';
Expand Down Expand Up @@ -41,6 +42,7 @@ type LoginProps = {

const day = 1000 * 60 * 60 * 24;

// eslint-disable-next-line complexity
export const Login: FC<LoginProps> = ({
identity,
onConfirm,
Expand Down Expand Up @@ -104,7 +106,10 @@ export const Login: FC<LoginProps> = ({
const signIn = useCallback(
async (username: string, password: string): Promise<void> => {
try {
const geeTest = await getGeeTestToken(identity.geetestUrl);
const geeTest = await getGeeTestToken(
identity.geetestUrl,
identity.isUseCaptcha
);

const cognitoUser = await identity.signIn(
username,
Expand Down Expand Up @@ -147,7 +152,10 @@ export const Login: FC<LoginProps> = ({

const signUp = useCallback(
async (username: string, password: string): Promise<SignUpResponse> => {
const geeTest = await getGeeTestToken(identity.geetestUrl);
const geeTest = await getGeeTestToken(
identity.geetestUrl,
identity.isUseCaptcha
);
const result = await identity.signUp(username, password, geeTest);

userData.current = {
Expand Down
68 changes: 37 additions & 31 deletions packages/provider-cloud-ui/src/services/IdentityService.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { seedUtils, libs } from '@waves/waves-transactions';
import { libs, seedUtils } from '@waves/waves-transactions';

// eslint-disable-next-line sort-imports
import {
AuthenticationDetails,
CognitoIdToken,
CodeDeliveryDetails,
ISignUpResult,
CognitoIdToken,
CognitoUser,
CognitoUserPool,
CognitoUserAttribute,
CognitoUserPool,
ICognitoStorage,
ISignUpResult,
} from 'amazon-cognito-identity-js';

import { MemoryStorage } from './MemoryStorage';

type IdentityUser = {
Expand Down Expand Up @@ -39,10 +42,16 @@ type IdentityServiceOptions = {
clientId: string;
endpoint: string;
geetestUrl: string;
disableCaptcha?: boolean;
};

const USER = {
CHALLENGE: { NAME: 'challengeName', PARAM: 'challengeParam' },
};

export class IdentityService {
public geetestUrl = '';
public isUseCaptcha = false;
private readonly storage: ICognitoStorage = new MemoryStorage();
private userPool: CognitoUserPool | undefined = undefined;
private currentUser: CognitoUser | undefined = undefined;
Expand All @@ -57,9 +66,13 @@ export class IdentityService {
userPoolId,
endpoint,
geetestUrl,
disableCaptcha,
}: IdentityServiceOptions): void {
this.apiUrl = apiUrl;

this.isUseCaptcha =
disableCaptcha == null ? this.isUseCaptcha : disableCaptcha;

this.userPool = new CognitoUserPool({
UserPoolId: userPoolId,
ClientId: clientId,
Expand Down Expand Up @@ -208,52 +221,50 @@ export class IdentityService {
}),
{
onSuccess: async () => {
const identityUser = await this.fetchIdentityUser();
this.identityUser = await this.fetchIdentityUser();

this.identityUser = identityUser;

delete user['challengeName'];
delete user['challengeParam'];
delete user[USER.CHALLENGE.NAME];
delete user[USER.CHALLENGE.PARAM];
resolve(user);
},

onFailure: (err) => {
reject(err);
},
customChallenge: function (challengeParam) {
user['challengeName'] = 'CUSTOM_CHALLENGE';
user['challengeParam'] = challengeParam;
user[USER.CHALLENGE.NAME] = 'CUSTOM_CHALLENGE';
user[USER.CHALLENGE.PARAM] = challengeParam;
resolve(user);
},
mfaRequired: function (challengeName, challengeParam) {
user['challengeName'] = challengeName;
user['challengeParam'] = challengeParam;
user[USER.CHALLENGE.NAME] = challengeName;
user[USER.CHALLENGE.PARAM] = challengeParam;
resolve(user);
},
mfaSetup: function (challengeName, challengeParam) {
user['challengeName'] = challengeName;
user['challengeParam'] = challengeParam;
user[USER.CHALLENGE.NAME] = challengeName;
user[USER.CHALLENGE.PARAM] = challengeParam;
resolve(user);
},
newPasswordRequired: function (
userAttributes,
requiredAttributes
) {
user['challengeName'] = 'NEW_PASSWORD_REQUIRED';
user['challengeParam'] = {
user[USER.CHALLENGE.NAME] = 'NEW_PASSWORD_REQUIRED';
user[USER.CHALLENGE.PARAM] = {
userAttributes: userAttributes,
requiredAttributes: requiredAttributes,
};
resolve(user);
},
totpRequired: function (challengeName, challengeParam) {
user['challengeName'] = challengeName;
user['challengeParam'] = challengeParam;
user[USER.CHALLENGE.NAME] = challengeName;
user[USER.CHALLENGE.PARAM] = challengeParam;
resolve(user);
},
selectMFAType: function (challengeName, challengeParam) {
user['challengeName'] = challengeName;
user['challengeParam'] = challengeParam;
user[USER.CHALLENGE.NAME] = challengeName;
user[USER.CHALLENGE.PARAM] = challengeParam;
resolve(user);
},
}
Expand All @@ -272,15 +283,12 @@ export class IdentityService {
{
onSuccess: async (session) => {
if (this.currentUser) {
delete this.currentUser['challengeName'];
delete this.currentUser['challengeParam'];
delete this.currentUser[USER.CHALLENGE.NAME];
delete this.currentUser[USER.CHALLENGE.PARAM];
}

if (session && !this.identityUser) {
const identityUser = await this.fetchIdentityUser();

this.identityUser = identityUser;

this.identityUser = await this.fetchIdentityUser();
resolve();
}
},
Expand Down Expand Up @@ -436,9 +444,8 @@ export class IdentityService {
Authorization: `Bearer ${token}`,
},
});
const identityUser: IdentityUser = await itentityUserResponse.json();

return identityUser;
return (await itentityUserResponse.json()) as IdentityUser;
}

private async signByIdentity(
Expand All @@ -454,9 +461,8 @@ export class IdentityService {
},
body: JSON.stringify(body),
});
const result: IdentitySignTxResponse = await response.json();

return result;
return (await response.json()) as IdentitySignTxResponse;
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/provider-cloud-ui/src/services/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type Config = {
clientId: string;
userPoolId: string;
endpoint: string;
disableCaptcha?: string;
};
geetest: {
url: string;
Expand Down
11 changes: 10 additions & 1 deletion packages/provider-cloud-ui/src/utils/geeTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
const w = window as any;

export const getGeeTestToken = (
geetestUrl: string
geetestUrl: string,
useCaptcha = true
): Promise<{
geetest_challenge: string;
geetest_seccode: string;
geetest_validate: string;
}> => {
if (!useCaptcha) {
return Promise.resolve({
geetest_challenge: '',
geetest_seccode: '',
geetest_validate: '',
});
}

// eslint-disable-next-line no-async-promise-executor
return new Promise(async (res, rej) => {
try {
Expand Down