Skip to content
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

feat!: refactor login processes #162

Merged
merged 3 commits into from
Aug 19, 2024
Merged
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
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"test:coverage": "lerna run test:coverage"
},
"devDependencies": {
"@node-cli/bundlesize": "4.2.0",
"@versini/dev-dependencies-client": "5.1.1",
"@versini/dev-dependencies-types": "1.3.1"
"@node-cli/bundlesize": "4.2.1",
"@versini/dev-dependencies-client": "6.0.0",
"@versini/dev-dependencies-types": "1.3.3"
},
"packageManager": "pnpm@9.6.0+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"
"packageManager": "pnpm@9.7.1+sha512.faf344af2d6ca65c4c5c8c2224ea77a81a5e8859cbc4e06b1511ddce2f0151512431dd19e6aff31f2c6a8f5f2aced9bd2273e1fed7dd4de1868984059d2c4247"
}
6 changes: 2 additions & 4 deletions packages/auth-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"files": ["dist"],
"scripts": {
"build:check": "tsc",
"build:js": "vite build",
Expand All @@ -33,7 +31,7 @@
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"jose": "5.6.3",
"jose": "5.7.0",
"uuid": "10.0.0"
}
}
3 changes: 2 additions & 1 deletion packages/auth-common/src/components/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const TOKEN_EXPIRATION = {
};

export const API_TYPE = {
AUTHENTICATE: "authenticate",
CODE: "code",
LOGOUT: "logout",
LOGIN: "login",
REFRESH: "refresh",
};
2 changes: 1 addition & 1 deletion packages/auth-provider/bundlesize.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
*/
{
path: "dist/index.js",
limit: "18 kb",
limit: "15 kb",
},
],
};
5 changes: 1 addition & 4 deletions packages/auth-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"files": ["dist"],
"scripts": {
"build:check": "tsc",
"build:js": "vite build",
Expand Down Expand Up @@ -46,7 +44,6 @@
"dependencies": {
"@simplewebauthn/browser": "10.0.0",
"@versini/auth-common": "workspace:../auth-common",
"@versini/ui-fingerprint": "1.0.1",
"@versini/ui-hooks": "4.0.1",
"jose": "5.6.3",
"uuid": "10.0.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/auth-provider/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ export const GRAPHQL_QUERIES = {
$authentication: AuthenticationOptionsInput!,
$nonce: String!,
$domain: String,
$fingerprint: String) {
$sessionExpiration: String) {
verifyPasskeyAuthentication(
clientId: $clientId,
id: $id,
authentication: $authentication,
nonce: $nonce,
domain: $domain,
fingerprint: $fingerprint) {
sessionExpiration: $sessionExpiration) {
status,
idToken,
accessToken,
Expand Down
19 changes: 2 additions & 17 deletions packages/auth-provider/src/common/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { API_TYPE, AUTH_TYPES } from "@versini/auth-common";
import { API_TYPE } from "@versini/auth-common";
import {
ACTION_TYPE_LOADING,
ACTION_TYPE_LOGIN,
Expand All @@ -7,22 +7,14 @@ import {
STATUS_SUCCESS,
} from "./constants";

type AuthenticationTypes =
| typeof AUTH_TYPES.PASSKEY
| typeof AUTH_TYPES.CODE
| typeof AUTH_TYPES.ID_TOKEN
| typeof AUTH_TYPES.ACCESS_TOKEN
| typeof AUTH_TYPES.ID_AND_ACCESS_TOKEN
| null;

export type GenericResponse = {
status: typeof STATUS_SUCCESS | typeof STATUS_FAILURE;
};

export type RestCallProps = {
params: any;
clientId: string;
type: typeof API_TYPE.LOGOUT | typeof API_TYPE.AUTHENTICATE;
type: typeof API_TYPE.LOGOUT | typeof API_TYPE.LOGIN;
};
export type RestCallResponse = GenericResponse & {
data: any;
Expand Down Expand Up @@ -54,7 +46,6 @@ export type AuthState = {
isLoading: boolean;
isAuthenticated: boolean;
logoutReason?: string;
authenticationType: AuthenticationTypes;
user?: {
userId?: string;
username?: string;
Expand All @@ -72,7 +63,6 @@ export type AuthenticateUserProps = {
code?: string;
code_verifier?: string;
domain: string;
fingerprint: string;
};

export type AuthenticateUserResponse =
Expand Down Expand Up @@ -119,7 +109,6 @@ export type GetPreAuthCodeResponse = GenericResponse & {
export type LoginProps = (
username: string,
password: string,
type?: typeof AUTH_TYPES.CODE | typeof AUTH_TYPES.PASSKEY,
) => Promise<boolean>;

export type AuthContextProps = {
Expand All @@ -141,7 +130,6 @@ export type InternalActions =
| {
type: typeof ACTION_TYPE_LOGIN;
payload: {
authenticationType: AuthenticationTypes;
user: {
userId: string;
username: string;
Expand All @@ -157,9 +145,6 @@ export type InternalActions =

export type LogoutProps = {
userId: string;
idToken: string;
accessToken: string;
refreshToken: string;
clientId: string;
domain: string;
};
Expand Down
32 changes: 3 additions & 29 deletions packages/auth-provider/src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
decodeToken,
verifyAndExtractToken,
} from "@versini/auth-common";
import { getFingerprintHash } from "@versini/ui-fingerprint";

import { STATUS_FAILURE, STATUS_SUCCESS } from "./constants";
import { restCall } from "./services";
Expand All @@ -30,7 +29,6 @@ export const isDev = !isProd;
export const emptyState: AuthState = {
isLoading: true,
isAuthenticated: false,
authenticationType: null,
user: undefined,
logoutReason: "",
debug: false,
Expand Down Expand Up @@ -68,9 +66,6 @@ export const getUserIdFromToken = (token: string): string => {
*/
export const logoutUser = async ({
userId,
idToken,
accessToken,
refreshToken,
clientId,
domain,
}: LogoutProps): Promise<LogoutResponse> => {
Expand All @@ -80,9 +75,6 @@ export const logoutUser = async ({
clientId,
params: {
userId,
idToken,
accessToken,
refreshToken,
domain,
},
});
Expand Down Expand Up @@ -110,11 +102,10 @@ export const logoutUser = async ({
* @param {string} props.code - Authorization code
* @param {string} props.code_verifier - Code verifier
* @param {string} props.domain - Domain
* @param {string} props.fingerprint - Fingerprint
*
* @returns {AuthenticateUserResponse} - Authentication response
*/
export const authenticateUser = async ({
export const loginUser = async ({
username,
password,
clientId,
Expand All @@ -124,11 +115,10 @@ export const authenticateUser = async ({
code,
code_verifier,
domain,
fingerprint,
}: AuthenticateUserProps): Promise<AuthenticateUserResponse> => {
try {
const response = await restCall({
type: API_TYPE.AUTHENTICATE,
type: API_TYPE.LOGIN,
clientId,
params: {
type: type || AUTH_TYPES.ID_AND_ACCESS_TOKEN,
Expand All @@ -139,7 +129,6 @@ export const authenticateUser = async ({
code,
code_verifier,
domain,
fingerprint,
},
});
const jwt = await verifyAndExtractToken(response?.data?.idToken);
Expand Down Expand Up @@ -236,7 +225,7 @@ export const getAccessTokenSilently = async ({
}: GetAccessTokenSilentlyProps): Promise<GetAccessTokenSilentlyResponse> => {
try {
const response = await restCall({
type: API_TYPE.AUTHENTICATE,
type: API_TYPE.REFRESH,
clientId,
params: {
type: AUTH_TYPES.REFRESH_TOKEN,
Expand All @@ -245,7 +234,6 @@ export const getAccessTokenSilently = async ({
refreshToken,
accessToken,
domain,
fingerprint: await getCustomFingerprint(),
},
});
const jwt = await verifyAndExtractToken(response?.data?.accessToken);
Expand All @@ -271,17 +259,3 @@ export const getAccessTokenSilently = async ({
};
}
};

/**
* Get the custom fingerprint
*
* @async
* @returns {string} - Custom fingerprint
*/
export const getCustomFingerprint = async (): Promise<string> => {
try {
return await getFingerprintHash();
} catch (_error) {
return "";
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const stub = (): never => {
export const AuthContext = createContext<AuthContextProps>({
isAuthenticated: false,
isLoading: false,
authenticationType: null,
login: stub,
logout: stub,
getAccessToken: stub,
Expand Down
Loading