All files / services/core credential-factory.ts

100% Statements 5/5
100% Branches 9/9
100% Functions 5/5
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                1x                   3x                           3x                                     4x                                             3x                
import {
	BasicAuthCredential,
	BearerAuthCredential,
	EntraClientAuthCredential,
	MicrosoftAuthCredential
} from "./credential";
 
/**
 * Factory class for creating different types of authentication credentials.
 */
export class CredentialFactory {
	/**
	 * Get the credential types supported by this factory class.
	 * @returns An array of supported credential types.
	 */
	static getCredentialTypes(): string[] {
		return ['basic', 'bearer', 'microsoft', 'entra-client-credentials'];
	}
 
	/**
	 * Creates a Basic Authentication credential object.
	 * @param username Username to be used for authentication.
	 * @param password Password to be used for authentication.
	 * @returns A `BasicAuthCredential` object.
	 */
	static createBasicAuthCredential(username: string = '', password: string = ''): BasicAuthCredential {
		return {
			type: 'basic',
			username,
			password
		};
	}
 
	/**
	 * Creates a Bearer Authentication credential object.
	 * @param token The token to be used for authentication.
	 * @param prefix The token prefix, defaults to "Bearer".
	 * @returns A `BearerAuthCredential` object.
	 */
	static createBearerAuthCredential(token: string = '', prefix: string = 'Bearer'): BearerAuthCredential {
		return {
			type: 'bearer',
			token,
			prefix
		};
	}
 
	/**
	 * Creates a Microsoft Entra ID Authentication credential object.
	 * @param scopes The scopes to be used for authentication.
	 * @param sessionId The session ID of an active session (optional).
	 * @param accessToken The access token of an active session (optional).
	 * @returns A `MicrosoftAuthCredential` object.
	 */
	static createMicrosoftAuthCredential(
		scopes: string[] = ['https://graph.microsoft.com/.default'],
		sessionId?: string,
		accessToken?: string
	): MicrosoftAuthCredential {
		return {
			type: 'microsoft',
			scopes,
			sessionId,
			accessToken
		};
	}
 
	/**
	 * Creates a Microsoft Entra ID Client Credentials credential object.
	 * Used for service-to-service authentication (OAuth 2.0 Client Credentials Grant).
	 * @param tenantId The Azure AD tenant ID.
	 * @param clientId The application (client) ID.
	 * @param clientSecret The client secret.
	 * @param scopes The scopes to request.
	 * @returns An `EntraClientCredential` object.
	 */
	static createEntraClientCredential(
		tenantId: string = '',
		clientId: string = '',
		clientSecret: string = '',
		scopes: string[] = ['https://graph.microsoft.com/.default']
	): EntraClientAuthCredential {
		return {
			type: 'entra-client-credentials',
			tenantId,
			clientId,
			clientSecret,
			scopes
		};
	}
}