-
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 (#67)
* feat(component): add a new strategy for otp added a new 2-factor authentication strategy GH-69 * feat(component): add a new strategy for otp added a new 2-factor authentication strategy GH-69 * feat(component): add a new strategy for otp added a new 2-factor authentication strategy. gh-69
- Loading branch information
1 parent
b7bd5b5
commit 5f8cd5e
Showing
11 changed files
with
352 additions
and
0 deletions.
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,3 @@ | ||
export * from './otp-auth'; | ||
export * from './otp-strategy-factory.provider'; | ||
export * from './otp-verify.provider'; |
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,60 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import * as passport from 'passport'; | ||
|
||
export namespace Otp { | ||
export interface VerifyFunction { | ||
( | ||
key: string, | ||
otp: string, | ||
done: (error: any, user?: any, info?: any) => void, | ||
): void; | ||
} | ||
|
||
export interface StrategyOptions { | ||
key?: string; | ||
otp?: string; | ||
} | ||
|
||
export type VerifyCallback = ( | ||
err?: string | Error | null, | ||
user?: any, | ||
info?: any, | ||
) => void; | ||
|
||
export class Strategy extends passport.Strategy { | ||
constructor(_options?: StrategyOptions, verify?: VerifyFunction) { | ||
super(); | ||
this.name = 'otp'; | ||
if (verify) { | ||
this.verify = verify; | ||
} | ||
} | ||
|
||
name: string; | ||
private readonly verify: VerifyFunction; | ||
|
||
authenticate(req: any, options?: StrategyOptions): void { | ||
const key = req.body.key || options?.key; | ||
const otp = req.body.otp || options?.otp; | ||
|
||
if (!key || !otp) { | ||
this.fail(); | ||
return; | ||
} | ||
|
||
const verified = (err?: any, user?: any, _info?: any) => { | ||
if (err) { | ||
this.error(err); | ||
return; | ||
} | ||
if (!user) { | ||
this.fail(); | ||
return; | ||
} | ||
this.success(user); | ||
}; | ||
|
||
this.verify(key, otp, verified); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/strategies/passport/passport-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,49 @@ | ||
import {inject, Provider} from '@loopback/core'; | ||
import {HttpErrors} from '@loopback/rest'; | ||
import {AuthErrorKeys} from '../../../error-keys'; | ||
import {Strategies} from '../../keys'; | ||
import {VerifyFunction} from '../../types'; | ||
import {Otp} from './otp-auth'; | ||
|
||
export interface PassportOtpStrategyFactory { | ||
( | ||
options: Otp.StrategyOptions, | ||
verifierPassed?: VerifyFunction.OtpAuthFn, | ||
): Otp.Strategy; | ||
} | ||
|
||
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, | ||
): Otp.Strategy { | ||
const verifyFn = verifierPassed ?? this.verifierOtp; | ||
return new Otp.Strategy( | ||
options, | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
async (key: string, otp: string, cb: Otp.VerifyCallback) => { | ||
try { | ||
const user = await verifyFn(key, otp); | ||
if (!user) { | ||
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials); | ||
} | ||
cb(null, user); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
}, | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/strategies/passport/passport-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 (_key: string, _otp: string) => { | ||
throw new HttpErrors.NotImplemented( | ||
`VerifyFunction.OtpAuthFn is not implemented`, | ||
); | ||
}; | ||
} | ||
} |
Oops, something went wrong.