-
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(provider): make client secret non mandatory for public clients
make client secret non mandatory for public clients GH-153
- Loading branch information
1 parent
0e51de7
commit a996951
Showing
6 changed files
with
136 additions
and
78 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
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
// 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 ( | ||
/* eslint-disable @typescript-eslint/prefer-optional-chain */ | ||
!req.body || | ||
!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'; |
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