async authenticate()

in modules/identity/src/apiGateway.ts [30:99]


	async authenticate(
		event: APIGatewayProxyEvent,
	): Promise<AuthenticationResult> {
		const authHeader = event.headers.Authorization;
		if (!authHeader) {
			console.log('No Authorization header provided in request', event);
			return {
				type: 'failure',
				response: {
					statusCode: 401,
					body: JSON.stringify({ message: 'No Authorization header provided' }),
				},
			};
		}
		try {
			const userDetails = await this.tokenHelper.getIdentityId(authHeader);
			console.log(
				`Successfully authenticated user with identityId: ${userDetails.identityId}`,
			);
			return {
				type: 'success',
				userDetails,
			};
		} catch (error) {
			console.log('Caught exception with message: ', error);
			if (error instanceof ExpiredTokenError) {
				return {
					type: 'failure',
					response: {
						body: 'Token has expired',
						statusCode: 401,
					},
				};
			}
			if (error instanceof InvalidTokenError) {
				return {
					type: 'failure',
					response: {
						body: 'Token is invalid',
						statusCode: 401,
					},
				};
			}
			if (error instanceof InvalidScopesError) {
				return {
					type: 'failure',
					response: {
						body: 'Token does not have the required scopes',
						statusCode: 403,
					},
				};
			}
			if (error instanceof ValidationError) {
				return {
					type: 'failure',
					response: {
						body: error.message,
						statusCode: 403,
					},
				};
			}
			return {
				type: 'failure',
				response: {
					body: 'Internal server error',
					statusCode: 500,
				},
			};
		}
	}