-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add support for extension point/extension
Extension point/extension is a pattern for extensibility. This PR illustrates how we can support it using the Context apis and naming conventions. It also serves as an invitation to discuss if we should support such entities out of box.
- Loading branch information
1 parent
7a644ea
commit 2b7268c
Showing
13 changed files
with
686 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/authentication | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {ParsedRequest} from '@loopback/rest'; | ||
|
||
/** | ||
* interface definition of a function which accepts a request | ||
* and returns an authenticated user | ||
*/ | ||
export interface AuthenticateFn { | ||
(request: ParsedRequest): Promise<UserProfile | undefined>; | ||
} | ||
|
||
/** | ||
* interface definition of a user profile | ||
* http://openid.net/specs/openid-connect-core-1_0.html#StandardClaims | ||
*/ | ||
export interface UserProfile { | ||
id: string; | ||
name?: string; | ||
email?: string; | ||
} | ||
|
||
/** | ||
* Authentication metadata stored via Reflection API | ||
*/ | ||
export interface AuthenticationMetadata { | ||
/** | ||
* Name of the authentication strategy | ||
*/ | ||
strategy: string; | ||
/** | ||
* Options for authentication | ||
*/ | ||
options?: Object; | ||
} | ||
|
||
/** | ||
* Interface for authentication providers | ||
*/ | ||
export interface Authenticator { | ||
/** | ||
* Check if the given strategy is supported by the authenticator | ||
* @param stragety Name of the authentication strategy | ||
*/ | ||
isSupported(strategy: string): boolean; | ||
|
||
/** | ||
* Authenticate a request with given options | ||
* @param request HTTP request | ||
* @param metadata Authentication metadata | ||
*/ | ||
authenticate( | ||
request: ParsedRequest, | ||
metadata?: AuthenticationMetadata, | ||
): Promise<UserProfile | undefined>; | ||
} | ||
|
||
/** | ||
* Passport monkey-patches Node.js' IncomingMessage prototype | ||
* and adds extra methods like "login" and "isAuthenticated" | ||
*/ | ||
export type PassportRequest = ParsedRequest & Express.Request; |
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
40 changes: 40 additions & 0 deletions
40
packages/authentication/src/providers/auth-extension-point.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,40 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/authentication | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {ExtensionPoint, Context} from '@loopback/core'; | ||
import {ParsedRequest} from '@loopback/rest'; | ||
import { | ||
UserProfile, | ||
AuthenticationMetadata, | ||
Authenticator, | ||
} from '../authentication'; | ||
import {AuthenticationBindings} from '../keys'; | ||
|
||
export class AuthenticationExtensionPoint extends ExtensionPoint< | ||
Authenticator | ||
> { | ||
static extensionPointName = 'authenticators'; | ||
|
||
async authenticate( | ||
ctx: Context, | ||
request: ParsedRequest, | ||
): Promise<UserProfile | undefined> { | ||
const meta: AuthenticationMetadata | undefined = await ctx.get( | ||
AuthenticationBindings.METADATA, | ||
); | ||
if (meta == undefined) { | ||
return undefined; | ||
} | ||
const authenticators = await this.getAllExtensions(ctx); | ||
let user: UserProfile | undefined = undefined; | ||
for (const authenticator of authenticators) { | ||
if (authenticator.isSupported(meta.strategy)) { | ||
user = await authenticator.authenticate(request, meta); | ||
if (user === undefined) continue; | ||
} | ||
} | ||
return user; | ||
} | ||
} |
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
Oops, something went wrong.