mirror of
https://github.com/dadosfera/maestro.git
synced 2026-11-24 03:29:09 +00:00
REFACTOR: openid module
This commit is contained in:
@@ -17,12 +17,10 @@ import {
|
||||
AuthResetPasswordRequest,
|
||||
AuthVerifyResetPasswordCodeRequest,
|
||||
AuthConfirmResetPasswordRequest,
|
||||
IdProvider,
|
||||
} from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/messages';
|
||||
import { DucClient } from '../duc/client.config';
|
||||
import { Metadata } from '@grpc/grpc-js';
|
||||
import * as client from 'openid-client'
|
||||
import { Request } from 'express';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class AuthClientService implements OnModuleInit {
|
||||
@@ -180,84 +178,5 @@ export class AuthClientService implements OnModuleInit {
|
||||
);
|
||||
}
|
||||
|
||||
createIdentityProvider(body: IdProvider, metadata: Metadata) {
|
||||
return lastValueFrom(
|
||||
this.identityProviderService.CreateIdentityProvider(body, metadata),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
async loginIdp(name: string, req: Request) {
|
||||
const idp = await lastValueFrom(
|
||||
this.identityProviderService.FindIdentityProvider({ name }),
|
||||
);
|
||||
|
||||
const config: client.Configuration = await client.discovery(
|
||||
new URL(idp.issueUrl),
|
||||
idp.clientId,
|
||||
idp.clientSecret,
|
||||
)
|
||||
|
||||
const redirect_uri = `http://localhost:3000/auth/idp/${name}/callback`;
|
||||
const scope = 'openid email';
|
||||
|
||||
const code_verifier: string = client.randomPKCECodeVerifier()
|
||||
const code_challenge: string =
|
||||
await client.calculatePKCECodeChallenge(code_verifier)
|
||||
let state: string;
|
||||
const parameters: Record<string, string> = {
|
||||
redirect_uri,
|
||||
scope,
|
||||
code_challenge,
|
||||
code_challenge_method: 'S256',
|
||||
}
|
||||
req.session.code_verifier = code_verifier;
|
||||
|
||||
if (!config.serverMetadata().supportsPKCE()) {
|
||||
/**
|
||||
* We cannot be sure the server supports PKCE so we're going to use state too.
|
||||
* Use of PKCE is backwards compatible even if the AS doesn't support it which
|
||||
* is why we're using it regardless. Like PKCE, random state must be generated
|
||||
* for every redirect to the authorization_endpoint.
|
||||
*/
|
||||
state = client.randomState()
|
||||
parameters.state = state
|
||||
req.session.state = state;
|
||||
}
|
||||
|
||||
const authUrl = client.buildAuthorizationUrl(config, parameters);
|
||||
return authUrl;
|
||||
}
|
||||
|
||||
async authorizeIdp(name: string, req: Request) {
|
||||
const code_verifier_session = req.session.code_verifier;
|
||||
const state_session = req.session.state;
|
||||
const url = new URL("http://localhost:3000" + req.url);
|
||||
|
||||
const idp = await lastValueFrom(
|
||||
this.identityProviderService.FindIdentityProvider({ name }),
|
||||
);
|
||||
|
||||
const config: client.Configuration = await client.discovery(
|
||||
new URL(idp.issueUrl),
|
||||
idp.clientId,
|
||||
idp.clientSecret,
|
||||
)
|
||||
|
||||
const tokens: client.TokenEndpointResponse = await client.authorizationCodeGrant(
|
||||
config,
|
||||
url,
|
||||
{
|
||||
pkceCodeVerifier: code_verifier_session,
|
||||
expectedState: state_session,
|
||||
},
|
||||
)
|
||||
|
||||
delete req.session.code_verifier;
|
||||
delete req.session.state;
|
||||
|
||||
console.log('Token Endpoint Response', tokens)
|
||||
|
||||
return tokens;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user