wildflock/src/services/auth-oidc/auth-oidc.class.ts

49 lines
1.6 KiB
TypeScript

import type { Params, ServiceInterface } from '@feathersjs/feathers';
import type { Application } from '../../declarations';
import { Issuer, generators } from 'openid-client';
import config from 'config';
type AuthOidcResponse = string;
type AuthOidcQuery = any;
export type { AuthOidcResponse as AuthOidc, AuthOidcQuery };
export interface AuthOidcServiceOptions {
app: Application;
}
export interface AuthOidcParams extends Params<AuthOidcQuery> {
session?: any;
}
export class AuthOidcService<ServiceParams extends AuthOidcParams = AuthOidcParams>
implements ServiceInterface<AuthOidcResponse, ServiceParams>
{
constructor(public options: AuthOidcServiceOptions) {}
async find(params: ServiceParams): Promise<AuthOidcResponse> {
const issuer = await Issuer.discover(config.get('oidc.gatewayUri'));
const client = new issuer.Client({
client_id: config.get('oidc.clientId'),
client_secret: config.get('oidc.clientSecret'),
redirect_uris: [config.get('oidc.redirectUris')],
response_types: [config.get('oidc.responseTypes')],
id_token_signed_response_alg: config.get('oidc.signedResponseAlg'),
token_endpoint_auth_method: config.get('oidc.authMethod'),
});
const url = client.authorizationUrl({
redirect_uri: config.get('clientUrl') + '/auth-oidc/callback',
scope: config.get('oidc.scopes'),
response_type: config.get('oidc.responseTypes'),
});
return url;
}
}
export const getOptions = (app: Application) => {
return { app };
};