ts-keycloak-authentication
Typescript Keycloak authentication package for handling internal service-to-service authentication within Fidel API services using Keycloak.
Installation
You can install the package using npm:
npm install @fidel.uk/ts-keycloak-authentication
Usage
import KeycloakAuthentication, {
KeycloakAuthenticationConfig,
} from '@fidel.uk/ts-keycloak-authentication';
import { Log } from '@fidel.uk/ts-logger';
const keycloakAuthenticationConfig: KeycloakAuthenticationConfig = {
clientId: 'your-keycloak-client-id',
clientSecret: 'your-keycloak-client-secret',
};
const logger = new Log();
const authentication = new KeycloakAuthentication(
keycloakAuthenticationConfig,
logger,
);
(async () => {
try {
const accessToken = await authentication.getAccessToken();
} catch (error) {
console.error('Authentication error:', error);
}
})();
Configuration
The KeycloakAuthentication
constructor supports an KeycloakAuthenticationConfig
configuration object:
Parameter | Type | Required | Description |
---|
clientId | string | yes | The client ID for your Keycloak client. |
clientSecret | string | yes | The client secret for your Keycloak client. |
keycloakUrl | string | no | The URL of your Keycloak server. If not provided, it defaults to the stage-based URLs: "dev" - https://auth.platform.fidelapi.rocks ; "prod" - https://auth.platform.fidelapi.com |
realmName | string | no | The realm name for your Keycloak setup. Defaults to "fidelapi". |
stage | string | no | The environment stage, either "dev" or "prod". Defaults to "prod". |
Development
Install dependencies
Lint
- Lint:
npm run lint
- Fix:
npm run prettier:fix
Test
- Tests:
npm run test
- Tests with coverage:
npm run test:coverage