Skip to content

Commit

Permalink
Handle change of API endpoint in next places and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek555 committed Apr 4, 2024
1 parent e11ec7d commit a491798
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 64 deletions.
5 changes: 5 additions & 0 deletions apps/blog/pages/[param]/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { useParams } from 'next/navigation';
import { useUser } from '@smart-signer/lib/auth/use-user';
import { cn } from '@ui/lib/utils';
import { hiveChainService } from '@transaction/lib/hive-chain-service';
import { hbauthUseStrictMode, hbauthService } from '@smart-signer/lib/hbauth-service';

const DEFAULTS_ENDPOINTS = [
'https://api.hive.blog',
Expand Down Expand Up @@ -213,6 +214,10 @@ export default function UserSettings() {
onValueChange={async (newEndpoint) => {
setEndpoint(newEndpoint);
await hiveChainService.setHiveChainEndpoint(newEndpoint);
await hbauthService.setOnlineClient(
hbauthUseStrictMode,
{node: newEndpoint}
);
}}
value={endpoint}
>
Expand Down
2 changes: 1 addition & 1 deletion packages/smart-signer/lib/api-handlers/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IronSessionData } from '@smart-signer/types/common';
import { cookieNamePrefix } from '@smart-signer/lib/session';
import { checkCsrfHeader } from '@smart-signer/lib/csrf-protection';
import { verifyLoginChallenge } from '@smart-signer/lib/verify-login-challenge';
import { verifyLogin } from '@smart-signer/lib/auth/use-sign-in';
import { verifyLogin } from '@smart-signer/lib/verify-login';
import { getLoginChallengeFromTransactionForLogin } from '@smart-signer/lib/login-operation'

import { getLogger } from '@hive/ui/lib/logging';
Expand Down
62 changes: 2 additions & 60 deletions packages/smart-signer/lib/auth/use-sign-in.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,13 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
import { QUERY_KEY } from '@smart-signer/lib/query-keys';
import { fetchJson } from '@smart-signer/lib/fetch-json';
import { PostLoginSchema } from '@smart-signer/lib/auth/utils';
import { User, KeyType } from '@smart-signer/types/common';
import { User } from '@smart-signer/types/common';
import { csrfHeaderName } from '@smart-signer/lib/csrf-protection';
import { authorityChecker, AuthorityLevel } from '@smart-signer/lib/authority-checker';
import { ApiTransaction } from '@hive/wax';
import { verifyLogin } from '@smart-signer/lib/verify-login';

import { getLogger } from '@ui/lib/logging';
const logger = getLogger('app');

/**
* Authenticate user by checking signature in fake transaction.
*
* @param {PostLoginSchema} data
* @param {string} [uid='']
* @returns {Promise<User>}
*/
export async function verifyLogin(data: PostLoginSchema, uid: string = ''): Promise<User> {
const { username, keyType, pack, strict, loginType } = data;
logger.info('verifyLogin arg data', data);
let authorityLevel: AuthorityLevel;
if (keyType === KeyType.posting) {
authorityLevel = AuthorityLevel.POSTING;
} else if (keyType === KeyType.active) {
authorityLevel = AuthorityLevel.ACTIVE;
} else {
throw new Error('Unsupported keyType');
}

try {
const isAuthenticated = await authorityChecker(
JSON.parse(data.txJSON) as ApiTransaction,
username,
authorityLevel,
pack,
strict
);

const mode = strict ? 'strict' : 'non-strict';
if (isAuthenticated) {
logger.info(
'User %s passed authentication in %s mode with key type %s',
username, mode, keyType
);
} else {
logger.info(
'User %s failed authentication in %s mode with key type %s',
username, mode, keyType
);
}

const user: User = {
isLoggedIn: isAuthenticated,
username,
avatarUrl: '',
loginType,
keyType,
authenticateOnBackend: false,
};
return user;

} catch (error) {
logger.error('error in verifyLogin', error);
throw error;
}
}

/**
* Authenticate user via request to backend.
*
Expand Down
4 changes: 2 additions & 2 deletions packages/smart-signer/lib/authority-checker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
createHiveChain,
IHiveChainInterface,
ApiTransaction,
ApiAuthority,
TAccountName,
ApiKeyAuth,
TTransactionPackType
} from '@hive/wax';
import { hiveChainService } from '@transaction/lib/hive-chain-service';

import { getLogger } from '@hive/ui/lib/logging';
const logger = getLogger('app');
Expand Down Expand Up @@ -71,7 +71,7 @@ export const authorityChecker = async (
logger.info('authorityChecker args: %o',
{ txJSON, expectedSignerAccount, expectedAuthorityLevel, pack });

const hiveChain: IHiveChainInterface = await createHiveChain();
const hiveChain: IHiveChainInterface = await hiveChainService.getHiveChain();
const txBuilder = hiveChain.TransactionBuilder.fromApi(txJSON);

const authorityVerificationResult = await hiveChain.api.database_api
Expand Down
1 change: 1 addition & 0 deletions packages/smart-signer/lib/storage-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function StorageMixin<TBase extends WithStorage>(Base: TBase) {
&& isStorageAvailable(this.storageType, true)) {
this.storage = window.sessionStorage;
} else {
this.storageType = 'memoryStorage';
this.storage = memoryStorage;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/smart-signer/lib/use-local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function useLocalStorage<T extends string | object>(key: string, initialValue: T
// Return a wrapped version of useState's setter function that
// persists the new value to localStorage.
const setValue = (value: T) => {
logger.info('in useLocalStorage setValue, value is %o', value);
logger.info('In useLocalStorage setValue for key %s to: %o', key, value);
try {
// Allow value to be a function so we have same API as useState
const valueToStore = value instanceof Function ? value(storedValue) : value;
Expand Down
65 changes: 65 additions & 0 deletions packages/smart-signer/lib/verify-login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { PostLoginSchema } from '@smart-signer/lib/auth/utils';
import { User, KeyType } from '@smart-signer/types/common';
import { authorityChecker, AuthorityLevel } from '@smart-signer/lib/authority-checker';
import { ApiTransaction } from '@hive/wax';

import { getLogger } from '@ui/lib/logging';
const logger = getLogger('app');

/**
* Authenticate user by checking signature in fake transaction.
*
* @param {PostLoginSchema} data
* @param {string} [uid='']
* @returns {Promise<User>}
*/
export async function verifyLogin(data: PostLoginSchema, uid: string = ''): Promise<User> {
const { username, keyType, pack, strict, loginType } = data;
logger.info('verifyLogin argument data: %o', data);
let authorityLevel: AuthorityLevel;
if (keyType === KeyType.posting) {
authorityLevel = AuthorityLevel.POSTING;
} else if (keyType === KeyType.active) {
authorityLevel = AuthorityLevel.ACTIVE;
} else {
throw new Error('Unsupported keyType');
}

try {
const isAuthenticated = await authorityChecker(
JSON.parse(data.txJSON) as ApiTransaction,
username,
authorityLevel,
pack,
strict
);

const mode = strict ? 'strict' : 'non-strict';
if (isAuthenticated) {
logger.info(
'User %s passed authentication in %s mode with key type %s',
username, mode, keyType
);
} else {
logger.info(
'User %s failed authentication in %s mode with key type %s',
username, mode, keyType
);
}

const user: User = {
isLoggedIn: isAuthenticated,
username,
avatarUrl: '',
loginType,
keyType,
authenticateOnBackend: false,
};
return user;

} catch (error) {
logger.error('error in verifyLogin', error);
throw error;
}
}

0 comments on commit a491798

Please sign in to comment.