11import { inject , Provider } from '@loopback/core' ;
22import { HttpErrors , Request } from '@loopback/rest' ;
3- import * as ClientPasswordStrategy from 'passport-oauth2- client-password' ;
3+ import * as ClientPasswordStrategy from './ client-password-strategy ' ;
44
55import { AuthErrorKeys } from '../../../error-keys' ;
66import { IAuthClient } from '../../../types' ;
@@ -27,60 +27,52 @@ export class ClientPasswordStrategyFactoryProvider
2727 this . getClientPasswordVerifier ( options , verifier ) ;
2828 }
2929
30+ clientPasswordVerifierHelper (
31+ client : IAuthClient | null ,
32+ clientSecret : string | undefined ,
33+ ) {
34+ if ( ! client ?. clientSecret || client . clientSecret !== clientSecret ) {
35+ throw new HttpErrors . Unauthorized ( AuthErrorKeys . ClientVerificationFailed ) ;
36+ } else {
37+ // do nothing
38+ }
39+ }
40+
3041 getClientPasswordVerifier (
3142 options ?: ClientPasswordStrategy . StrategyOptionsWithRequestInterface ,
3243 verifierPassed ?: VerifyFunction . OauthClientPasswordFn ,
3344 ) : ClientPasswordStrategy . Strategy {
3445 const verifyFn = verifierPassed ?? this . verifier ;
3546 if ( options ?. passReqToCallback ) {
3647 return new ClientPasswordStrategy . Strategy (
37- options ,
38-
3948 // eslint-disable-next-line @typescript-eslint/no-misused-promises
4049 async (
41- req : Request ,
4250 clientId : string ,
43- clientSecret : string ,
44- cb : ( err : Error | null , client ?: IAuthClient | false ) => void ,
51+ clientSecret : string | undefined ,
52+ cb : ( err : Error | null , client ?: IAuthClient | null ) => void ,
53+ req : Request | undefined ,
4554 ) => {
4655 try {
4756 const client = await verifyFn ( clientId , clientSecret , req ) ;
48- if ( ! client ) {
49- throw new HttpErrors . Unauthorized ( AuthErrorKeys . ClientInvalid ) ;
50- } else if (
51- ! client . clientSecret ||
52- client . clientSecret !== clientSecret
53- ) {
54- throw new HttpErrors . Unauthorized (
55- AuthErrorKeys . ClientVerificationFailed ,
56- ) ;
57- }
57+ this . clientPasswordVerifierHelper ( client , clientSecret ) ;
5858 cb ( null , client ) ;
5959 } catch ( err ) {
6060 cb ( err ) ;
6161 }
6262 } ,
63+ options ,
6364 ) ;
6465 } else {
6566 return new ClientPasswordStrategy . Strategy (
6667 // eslint-disable-next-line @typescript-eslint/no-misused-promises
6768 async (
6869 clientId : string ,
69- clientSecret : string ,
70- cb : ( err : Error | null , client ?: IAuthClient | false ) => void ,
70+ clientSecret : string | undefined ,
71+ cb : ( err : Error | null , client ?: IAuthClient | null ) => void ,
7172 ) => {
7273 try {
7374 const client = await verifyFn ( clientId , clientSecret ) ;
74- if ( ! client ) {
75- throw new HttpErrors . Unauthorized ( AuthErrorKeys . ClientInvalid ) ;
76- } else if (
77- ! client . clientSecret ||
78- client . clientSecret !== clientSecret
79- ) {
80- throw new HttpErrors . Unauthorized (
81- AuthErrorKeys . ClientVerificationFailed ,
82- ) ;
83- }
75+ this . clientPasswordVerifierHelper ( client , clientSecret ) ;
8476 cb ( null , client ) ;
8577 } catch ( err ) {
8678 cb ( err ) ;
0 commit comments