-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9988b2
commit c2f91ad
Showing
9 changed files
with
184 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './client-auth-strategy.provider'; | ||
export * from './user-auth-strategy.provider'; | ||
export * from './passport'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './keycloak-strategy-factory-provider'; | ||
export * from './keycloak-verify.provider'; |
54 changes: 54 additions & 0 deletions
54
src/strategies/passport/passport-keycloak/keycloak-strategy-factory-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {inject, Provider} from '@loopback/core'; | ||
import {HttpErrors} from '@loopback/rest'; | ||
|
||
import {AuthErrorKeys} from '../../../error-keys'; | ||
import {IAuthUser} from '../../../types'; | ||
import {Strategies} from '../../keys'; | ||
import {KeycloakProfile, VerifyFunction} from '../../types'; | ||
|
||
export const KeycloakStrategy = require('@exlinc/keycloak-passport'); | ||
|
||
export interface KeycloakStrategyFactory { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(options: any): typeof KeycloakStrategy; | ||
} | ||
|
||
export class KeycloakStrategyFactoryProvider | ||
implements Provider<KeycloakStrategyFactory> { | ||
constructor( | ||
@inject(Strategies.Passport.KEYCLOAK_VERIFIER) | ||
private readonly verifierKeycloak: VerifyFunction.KeycloakAuthFn, | ||
) {} | ||
|
||
value(): KeycloakStrategyFactory { | ||
return (options) => this.getGoogleAuthStrategyVerifier(options); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
getGoogleAuthStrategyVerifier(options: any): typeof KeycloakStrategy { | ||
return new KeycloakStrategy( | ||
options, | ||
async ( | ||
accessToken: string, | ||
refreshToken: string, | ||
profile: KeycloakProfile, | ||
cb: (err?: string | Error, user?: IAuthUser) => void, | ||
) => { | ||
try { | ||
const user = await this.verifierKeycloak( | ||
accessToken, | ||
refreshToken, | ||
profile, | ||
cb, | ||
); | ||
if (!user) { | ||
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); | ||
} | ||
cb(undefined, user); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
}, | ||
); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/strategies/passport/passport-keycloak/keycloak-verify.provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {Provider} from '@loopback/context'; | ||
import {HttpErrors} from '@loopback/rest'; | ||
|
||
import {KeycloakProfile, VerifyFunction} from '../../types'; | ||
|
||
/** | ||
* A provider for default implementation of VerifyFunction.LocalPasswordFn | ||
* | ||
* It will just throw an error saying Not Implemented | ||
*/ | ||
export class KeycloakVerifyProvider | ||
implements Provider<VerifyFunction.KeycloakAuthFn> { | ||
constructor() {} | ||
|
||
value(): VerifyFunction.KeycloakAuthFn { | ||
return async ( | ||
accessToken: string, | ||
refreshToken: string, | ||
profile: KeycloakProfile, | ||
cb: (err?: string | Error, user?: KeycloakProfile) => void, | ||
) => { | ||
throw new HttpErrors.NotImplemented( | ||
`VerifyFunction.KeycloakAuthFn is not implemented`, | ||
); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters