-
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.
feat(provider): make client secret non mandatory for public clients (#…
…154) * feat(provider): added clientType to auth client making client secret non mandatory for public clients GH-153
- Loading branch information
1 parent
13b67ac
commit 039fa8c
Showing
8 changed files
with
230 additions
and
30 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
75 changes: 75 additions & 0 deletions
75
src/strategies/passport/passport-client-password/client-password-strategy.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,75 @@ | ||
// Type definitions for passport-oauth2-client-password 0.1.2 | ||
// Project: https://github.com/jaredhanson/passport-oauth2-client-password | ||
// Definitions by: Ivan Zubok <https://github.com/akaNightmare> | ||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped | ||
// TypeScript Version: 2.3 | ||
|
||
import * as passport from 'passport'; | ||
import * as express from 'express'; | ||
import {IAuthClient, IAuthSecureClient} from '../../../types'; | ||
|
||
export interface StrategyOptionsWithRequestInterface { | ||
passReqToCallback: boolean; | ||
} | ||
|
||
export interface VerifyFunctionWithRequest { | ||
( | ||
clientId: string, | ||
clientSecret: string | undefined, | ||
done: ( | ||
error: Error | null, | ||
client?: IAuthSecureClient | IAuthClient | null, | ||
info?: Object | undefined, | ||
) => void, | ||
req?: express.Request, | ||
): void; | ||
} | ||
|
||
export class Strategy extends passport.Strategy { | ||
constructor( | ||
verify: VerifyFunctionWithRequest, | ||
options?: StrategyOptionsWithRequestInterface, | ||
) { | ||
super(); | ||
if (!verify) | ||
throw new Error( | ||
'OAuth 2.0 client password strategy requires a verify function', | ||
); | ||
|
||
this.verify = verify; | ||
if (options) this.passReqToCallback = options.passReqToCallback; | ||
this.name = 'oauth2-client-password'; | ||
} | ||
|
||
private readonly verify: VerifyFunctionWithRequest; | ||
private readonly passReqToCallback: boolean; | ||
name: string; | ||
authenticate(req: express.Request, options?: {}): void { | ||
if (!req?.body?.client_id) { | ||
return this.fail(); | ||
} | ||
|
||
const clientId = req.body['client_id']; | ||
const clientSecret = req.body['client_secret']; | ||
|
||
const verified = ( | ||
err: Error | null, | ||
client: IAuthSecureClient | IAuthClient | null | undefined, | ||
info: Object | undefined, | ||
) => { | ||
if (err) { | ||
return this.error(err); | ||
} | ||
if (!client) { | ||
return this.fail(); | ||
} | ||
this.success(client, info); | ||
}; | ||
|
||
if (this.passReqToCallback) { | ||
this.verify(clientId, clientSecret, verified, req); | ||
} else { | ||
this.verify(clientId, clientSecret, verified); | ||
} | ||
} | ||
} |
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 './client-password-verify.provider'; | ||
export * from './client-password-strategy-factory-provider'; | ||
export * from './client-password-strategy'; |
91 changes: 91 additions & 0 deletions
91
...ies/passport/passport-client-password/secure-client-password-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,91 @@ | ||
import {inject, Provider} from '@loopback/core'; | ||
import {HttpErrors, Request} from '@loopback/rest'; | ||
import * as ClientPasswordStrategy from './client-password-strategy'; | ||
|
||
import {AuthErrorKeys} from '../../../error-keys'; | ||
import {ClientType, IAuthSecureClient} from '../../../types'; | ||
import {Strategies} from '../../keys'; | ||
import {VerifyFunction} from '../../types'; | ||
|
||
export interface SecureClientPasswordStrategyFactory { | ||
( | ||
options?: ClientPasswordStrategy.StrategyOptionsWithRequestInterface, | ||
verifierPassed?: VerifyFunction.OauthSecureClientPasswordFn, | ||
): ClientPasswordStrategy.Strategy; | ||
} | ||
|
||
export class SecureClientPasswordStrategyFactoryProvider | ||
implements Provider<SecureClientPasswordStrategyFactory> | ||
{ | ||
constructor( | ||
@inject(Strategies.Passport.OAUTH2_CLIENT_PASSWORD_VERIFIER) | ||
private readonly verifier: VerifyFunction.OauthSecureClientPasswordFn, | ||
) {} | ||
|
||
value(): SecureClientPasswordStrategyFactory { | ||
return (options, verifier) => | ||
this.getSecureClientPasswordVerifier(options, verifier); | ||
} | ||
|
||
secureClientPasswordVerifierHelper( | ||
client: IAuthSecureClient | null, | ||
clientSecret: string | undefined, | ||
) { | ||
if ( | ||
!client || | ||
(client.clientType !== ClientType.public && | ||
(!client.clientSecret || client.clientSecret !== clientSecret)) | ||
) { | ||
throw new HttpErrors.Unauthorized(AuthErrorKeys.ClientVerificationFailed); | ||
} else { | ||
// do nothing | ||
} | ||
} | ||
|
||
getSecureClientPasswordVerifier( | ||
options?: ClientPasswordStrategy.StrategyOptionsWithRequestInterface, | ||
verifierPassed?: VerifyFunction.OauthSecureClientPasswordFn, | ||
): ClientPasswordStrategy.Strategy { | ||
const verifyFn = verifierPassed ?? this.verifier; | ||
if (options?.passReqToCallback) { | ||
return new ClientPasswordStrategy.Strategy( | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
async ( | ||
clientId: string, | ||
clientSecret: string | undefined, | ||
cb: (err: Error | null, client?: IAuthSecureClient | null) => void, | ||
req: Request | undefined, | ||
) => { | ||
try { | ||
const client = await verifyFn(clientId, clientSecret, req); | ||
this.secureClientPasswordVerifierHelper(client, clientSecret); | ||
|
||
cb(null, client); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
}, | ||
options, | ||
); | ||
} else { | ||
return new ClientPasswordStrategy.Strategy( | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
async ( | ||
clientId: string, | ||
clientSecret: string | undefined, | ||
cb: (err: Error | null, client?: IAuthSecureClient | null) => void, | ||
) => { | ||
try { | ||
const client = await verifyFn(clientId, clientSecret); | ||
|
||
this.secureClientPasswordVerifierHelper(client, clientSecret); | ||
|
||
cb(null, client); | ||
} catch (err) { | ||
cb(err); | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
} |
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