Skip to content

Commit

Permalink
fix: minor refactor (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
aversini authored Aug 1, 2024
1 parent d85ee5a commit 3ff20c0
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 94 deletions.
3 changes: 3 additions & 0 deletions packages/auth-provider/src/common/TokenManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { getAccessTokenSilently } from "./utilities";
* TokenManager class to manage access and refresh tokens
* @class
* @public
*
* @param {string} accessToken - Access token
* @param {string} refreshToken - Refresh token
*
* @returns {TokenManager}
*
* @example
* const tokenManager = new TokenManager();
* tokenManager.refreshtoken({
Expand Down
86 changes: 85 additions & 1 deletion packages/auth-provider/src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const EXPIRED_SESSION =
"Oops! It looks like your session has expired. For your security, please log in again to continue.";
"Your session has expired. For your security, please log in again to continue.";
export const LOGOUT_SESSION = "Your session has been successfully terminated.";
export const LOGIN_ERROR = "Login failed. Please try again.";
export const ACCESS_TOKEN_ERROR =
Expand All @@ -21,3 +21,87 @@ export const ACTION_TYPE_LOGOUT = "LOGOUT";

export const STATUS_SUCCESS = "success";
export const STATUS_FAILURE = "failure";

export const REQUEST_CREDENTIALS = "include";
export const REQUEST_METHOD = "POST";
export const REQUEST_CONTENT_TYPE = "application/json";

export const GRAPHQL_QUERIES = {
GET_REGISTRATION_OPTIONS: `mutation GetPasskeyRegistrationOptions(
$clientId: String!,
$username: String!,
$id: String!) {
getPasskeyRegistrationOptions(clientId: $clientId, username: $username, id: $id) {
challenge
rp {
id
name
}
user {
id
name
displayName
}
pubKeyCredParams {
type
alg
}
timeout
attestation
}
}`,
VERIFY_REGISTRATION: `mutation VerifyPasskeyRegistration(
$clientId: String!,
$username: String!,
$id: String!,
$registration: RegistrationOptionsInput!) {
verifyPasskeyRegistration(
clientId: $clientId,
username: $username,
id: $id,
registration: $registration) {
status
message
}
}`,
GET_AUTHENTICATION_OPTIONS: `mutation GetPasskeyAuthenticationOptions(
$id: String!,
$clientId: String!,
) {
getPasskeyAuthenticationOptions(
id: $id,
clientId: $clientId) {
rpId,
challenge,
allowCredentials {
id,
type,
transports
}
timeout,
userVerification,
}
}`,
VERIFY_AUTHENTICATION: `mutation VerifyPasskeyAuthentication(
$clientId: String!,
$id: String!,
$authentication: AuthenticationOptionsInput!,
$nonce: String!,
$domain: String,
$fingerprint: String) {
verifyPasskeyAuthentication(
clientId: $clientId,
id: $id,
authentication: $authentication,
nonce: $nonce,
domain: $domain,
fingerprint: $fingerprint) {
status,
idToken,
accessToken,
refreshToken,
userId,
username,
}
}`,
};
114 changes: 26 additions & 88 deletions packages/auth-provider/src/common/services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { HEADERS } from "@versini/auth-common";
import { API_ENDPOINT, STATUS_FAILURE, STATUS_SUCCESS } from "./constants";

import {
API_ENDPOINT,
GRAPHQL_QUERIES,
REQUEST_CONTENT_TYPE,
REQUEST_CREDENTIALS,
REQUEST_METHOD,
STATUS_FAILURE,
STATUS_SUCCESS,
} from "./constants";
import type {
GraphQLCallProps,
GraphQLCallResponse,
Expand All @@ -8,85 +17,6 @@ import type {
} from "./types";
import { isDev } from "./utilities";

const GRAPHQL_QUERIES = {
GET_REGISTRATION_OPTIONS: `mutation GetPasskeyRegistrationOptions(
$clientId: String!,
$username: String!,
$id: String!) {
getPasskeyRegistrationOptions(clientId: $clientId, username: $username, id: $id) {
challenge
rp {
id
name
}
user {
id
name
displayName
}
pubKeyCredParams {
type
alg
}
timeout
attestation
}
}`,
VERIFY_REGISTRATION: `mutation VerifyPasskeyRegistration(
$clientId: String!,
$username: String!,
$id: String!,
$registration: RegistrationOptionsInput!) {
verifyPasskeyRegistration(
clientId: $clientId,
username: $username,
id: $id,
registration: $registration) {
status
message
}
}`,
GET_AUTHENTICATION_OPTIONS: `mutation GetPasskeyAuthenticationOptions(
$id: String!,
$clientId: String!,
) {
getPasskeyAuthenticationOptions(
id: $id,
clientId: $clientId) {
rpId,
challenge,
allowCredentials {
id,
type,
transports
}
timeout,
userVerification,
}
}`,
VERIFY_AUTHENTICATION: `mutation VerifyPasskeyAuthentication(
$clientId: String!,
$id: String!,
$authentication: AuthenticationOptionsInput!,
$nonce: String!,
$domain: String,
$fingerprint: String) {
verifyPasskeyAuthentication(
clientId: $clientId,
id: $id,
authentication: $authentication,
nonce: $nonce,
domain: $domain,
fingerprint: $fingerprint) {
status,
idToken,
accessToken,
refreshToken,
userId,
username,
}
}`,
};
export const SERVICE_TYPES = {
GET_REGISTRATION_OPTIONS: {
schema: GRAPHQL_QUERIES.GET_REGISTRATION_OPTIONS,
Expand All @@ -111,6 +41,11 @@ export const SERVICE_TYPES = {
*
* @async
* @param {GraphQLCallProps} props - GraphQL call properties
* @param {string} props.accessToken - Access token
* @param {string} props.clientId - Client ID
* @param {object} props.params - Parameters
* @param {object} props.type - Service type
*
* @returns {Promise<GraphQLCallResponse>} - Generic response
*/
export const graphQLCall = async ({
Expand All @@ -120,17 +55,16 @@ export const graphQLCall = async ({
params = {},
}: GraphQLCallProps): Promise<GraphQLCallResponse> => {
try {
const requestData = type?.data ? type.data(params) : params;
const requestData = params;
const authorization = `Bearer ${accessToken}`;
const response = await fetch(
isDev ? `${API_ENDPOINT.dev}/graphql` : `${API_ENDPOINT.prod}/graphql`,
{
method: "POST",
credentials: "include",
credentials: REQUEST_CREDENTIALS,
method: REQUEST_METHOD,
headers: {
authorization,
"Content-Type": "application/json",
Accept: "application/json",
"Content-Type": REQUEST_CONTENT_TYPE,
[HEADERS.CLIENT_ID]: `${clientId}`,
},
body: JSON.stringify({
Expand Down Expand Up @@ -158,6 +92,10 @@ export const graphQLCall = async ({
*
* @async
* @param {RestCallProps} props - REST call properties
* @param {string} props.type - Service type
* @param {string} props.clientId - Client ID
* @param {object} props.params - Parameters
*
* @returns {Promise<RestCallResponse>} - Generic response
*/
export const restCall = async ({
Expand All @@ -169,10 +107,10 @@ export const restCall = async ({
const response = await fetch(
isDev ? `${API_ENDPOINT.dev}/${type}` : `${API_ENDPOINT.prod}/${type}`,
{
credentials: "include",
method: "POST",
credentials: REQUEST_CREDENTIALS,
method: REQUEST_METHOD,
headers: {
"Content-Type": "application/json",
"Content-Type": REQUEST_CONTENT_TYPE,
[HEADERS.CLIENT_ID]: `${clientId}`,
},
body: JSON.stringify(params),
Expand Down
5 changes: 4 additions & 1 deletion packages/auth-provider/src/common/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export type RestCallResponse = GenericResponse & {
export type GraphQLCallProps = {
accessToken: string;
clientId: string;
type: any;
type: {
schema: string;
method: string;
};
params?: any;
};
export type GraphQLCallResponse = GenericResponse & {
Expand Down
30 changes: 29 additions & 1 deletion packages/auth-provider/src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
verifyAndExtractToken,
} from "@versini/auth-common";
import { getFingerprintHash } from "@versini/ui-fingerprint";

import { STATUS_FAILURE, STATUS_SUCCESS } from "./constants";
import { restCall } from "./services";
import type {
Expand Down Expand Up @@ -39,7 +40,8 @@ export const emptyState: AuthState = {
* Get the user ID from the token
*
* @param {string} token - JWT token
* @returns {string} - User ID
*
* @returns {string} - User ID or empty string
*/
export const getUserIdFromToken = (token: string): string => {
try {
Expand All @@ -55,6 +57,13 @@ export const getUserIdFromToken = (token: string): string => {
*
* @async
* @param {LogoutProps} props - Logout properties
* @param {string} props.userId - User ID
* @param {string} props.idToken - ID token
* @param {string} props.accessToken - Access token
* @param {string} props.refreshToken - Refresh token
* @param {string} props.clientId - Client ID
* @param {string} props.domain - Domain
*
* @returns {LogoutResponse} - Logout response
*/
export const logoutUser = async ({
Expand Down Expand Up @@ -92,6 +101,17 @@ export const logoutUser = async ({
*
* @async
* @param {AuthenticateUserProps} props - Authentication properties
* @param {string} props.username - Username
* @param {string} props.password - Password
* @param {string} props.clientId - Client ID
* @param {string} props.nonce - Nonce
* @param {string} props.type - Authentication type
* @param {string} props.sessionExpiration - Session expiration
* @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 ({
Expand Down Expand Up @@ -155,6 +175,7 @@ export const authenticateUser = async ({
* @param {string} options.nonce - The nonce value used for authorization.
* @param {string} options.clientId - The client ID.
* @param {string} options.code_challenge - The code challenge.
*
* @returns {GetPreAuthCodeResponse} - The response object.
*/
export const getPreAuthCode = async ({
Expand Down Expand Up @@ -196,6 +217,13 @@ export const getPreAuthCode = async ({
*
* @async
* @param {GetAccessTokenSilentlyProps} props - GetAccessTokenSilently properties
* @param {string} props.clientId - Client ID
* @param {string} props.userId - User ID
* @param {string} props.nonce - Nonce
* @param {string} props.refreshToken - Refresh token
* @param {string} props.accessToken - Access token
* @param {string} props.domain - Domain
*
* @returns {GetAccessTokenSilentlyResponse} - GetAccessTokenSilently response
*/
export const getAccessTokenSilently = async ({
Expand Down
Loading

0 comments on commit 3ff20c0

Please sign in to comment.