12 change auth to OIDC (#13)
* switching auth strategy to oidc * switching auth strategy to oidc
This commit was merged in pull request #13.
This commit is contained in:
@@ -3,7 +3,7 @@ import { AuthGuard as PassportAuthGuard } from '@nestjs/passport';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class AuthGuard extends PassportAuthGuard('azure-ad') {
|
||||
export class AuthGuard extends PassportAuthGuard('oidc') {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
export interface AuthModuleOptions {
|
||||
tenantId: string;
|
||||
authorizationUrl: string;
|
||||
issuer: string;
|
||||
callbackUrl: string;
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
scope: string;
|
||||
tokenUrl: string;
|
||||
userInfoUrl: string;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { PassportModule } from '@nestjs/passport';
|
||||
import { AzureAdStrategy } from './auth.strategy';
|
||||
import { OidcStrategy } from './oidc.strategy';
|
||||
import { ConfigurableModuleClass } from './auth.module-definition';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PassportModule.register({
|
||||
defaultStrategy: 'azure-ad'
|
||||
defaultStrategy: 'oidc'
|
||||
})
|
||||
],
|
||||
providers: [
|
||||
AzureAdStrategy
|
||||
OidcStrategy
|
||||
]
|
||||
})
|
||||
export class AuthModule extends ConfigurableModuleClass {}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { AuthModuleOptions } from './auth.interface'
|
||||
import { MODULE_OPTIONS_TOKEN } from "./auth.module-definition";
|
||||
import { BearerStrategy } from 'passport-azure-ad'
|
||||
|
||||
@Injectable()
|
||||
export class AzureAdStrategy extends PassportStrategy(
|
||||
BearerStrategy,
|
||||
'azure-ad'
|
||||
) {
|
||||
constructor(@Inject(MODULE_OPTIONS_TOKEN) authModuleOptions: AuthModuleOptions) {
|
||||
super({
|
||||
identityMetadata: `https://login.microsoftonline.com/${authModuleOptions.tenantId}/.well-known/openid-configuration`,
|
||||
clientID: authModuleOptions.clientId,
|
||||
audience: `api://${authModuleOptions.clientId}`,
|
||||
loggingLevel: 'info',
|
||||
loggingNoPII: false
|
||||
})
|
||||
}
|
||||
|
||||
async validate(data: any): Promise<any> {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
31
libs/modules/src/auth/oidc.strategy.ts
Normal file
31
libs/modules/src/auth/oidc.strategy.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { Strategy, Profile } from 'passport-openidconnect';
|
||||
import { MODULE_OPTIONS_TOKEN } from "./auth.module-definition";
|
||||
import { AuthModuleOptions } from "./auth.interface";
|
||||
import { Inject, Injectable } from "@nestjs/common";
|
||||
|
||||
@Injectable()
|
||||
export class OidcStrategy extends PassportStrategy(Strategy, 'oidc') {
|
||||
constructor(@Inject(MODULE_OPTIONS_TOKEN) authModuleOptions: AuthModuleOptions) {
|
||||
super({
|
||||
authorizationURL: authModuleOptions.authorizationUrl,
|
||||
callbackURL: authModuleOptions.callbackUrl,
|
||||
clientID: authModuleOptions.clientId,
|
||||
clientSecret: authModuleOptions.clientSecret,
|
||||
issuer: authModuleOptions.issuer,
|
||||
scope: authModuleOptions.scope,
|
||||
tokenURL: authModuleOptions.tokenUrl,
|
||||
userInfoURL: authModuleOptions.userInfoUrl
|
||||
})
|
||||
}
|
||||
|
||||
async validate(profile: Profile, done: Function): Promise<any> {
|
||||
const user = {
|
||||
id: profile.id,
|
||||
email: profile.emails,
|
||||
name: profile.displayName
|
||||
}
|
||||
|
||||
done(null, user)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user