-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): add a new strategy for otp
added a new 2-factor authentication strategy
- Loading branch information
1 parent
401097c
commit 801621c
Showing
12 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
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
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 './otp-strategy-factory.provider'; | ||
export * from './otp-verify.provider'; |
53 changes: 53 additions & 0 deletions
53
src/strategies/passport/passport-local-with-otp/otp-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,53 @@ | ||
import {inject, Provider} from '@loopback/core'; | ||
import {HttpErrors} from '@loopback/rest'; | ||
import {AuthErrorKeys} from '../../../error-keys'; | ||
import {Strategies} from '../../keys'; | ||
import {Otp, VerifyFunction} from '../../types'; | ||
export const PassportOtpStrategy = require('@artsy/passport-local-with-otp'); | ||
|
||
export interface PassportOtpStrategyFactory { | ||
( | ||
options: Otp.StrategyOptions, | ||
verifierPassed?: VerifyFunction.OtpAuthFn, | ||
): typeof PassportOtpStrategy; | ||
} | ||
|
||
export class PassportOtpStrategyFactoryProvider | ||
implements Provider<PassportOtpStrategyFactory> | ||
{ | ||
constructor( | ||
@inject(Strategies.Passport.OTP_VERIFIER) | ||
private readonly verifierOtp: VerifyFunction.OtpAuthFn, | ||
) {} | ||
|
||
value(): PassportOtpStrategyFactory { | ||
return (options, verifier) => | ||
this.getPassportOtpStrategyVerifier(options, verifier); | ||
} | ||
|
||
getPassportOtpStrategyVerifier( | ||
options?: Otp.StrategyOptions, | ||
verifierPassed?: VerifyFunction.OtpAuthFn, | ||
): typeof PassportOtpStrategy { | ||
const verifyFn = verifierPassed ?? this.verifierOtp; | ||
return new PassportOtpStrategy( | ||
options, | ||
async ( | ||
username: string, | ||
password: string, | ||
otp: string, | ||
cb: Otp.VerifyCallback, | ||
) => { | ||
try { | ||
const user = await verifyFn(username, password, otp, cb); | ||
if (!user) { | ||
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); | ||
} | ||
cb(undefined, user); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
}, | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/strategies/passport/passport-local-with-otp/otp-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,16 @@ | ||
import {Provider} from '@loopback/context'; | ||
import {HttpErrors} from '@loopback/rest'; | ||
|
||
import {VerifyFunction} from '../../types'; | ||
|
||
export class OtpVerifyProvider implements Provider<VerifyFunction.OtpAuthFn> { | ||
constructor() {} | ||
|
||
value(): VerifyFunction.OtpAuthFn { | ||
return async (username: string, password: string, otp: string) => { | ||
throw new HttpErrors.NotImplemented( | ||
`VerifyFunction.OtpAuthFn 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './types'; | ||
export * from './keycloak.types'; | ||
export * from './otp-auth.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export namespace Otp { | ||
export interface StrategyOptions { | ||
skipTotpVerification: string; | ||
usernameField: string; | ||
passwordField: string; | ||
codeField: string; | ||
window: number; | ||
otpField: string; | ||
passReqToCallback: boolean; | ||
} | ||
|
||
export type VerifyCallback = ( | ||
err?: string | Error, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
user?: any, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
info?: any, | ||
) => void; | ||
} |
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