-
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.
Implement provider to return controller method metadata
- Loading branch information
1 parent
25b95f9
commit 4662193
Showing
7 changed files
with
126 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
|
||
export * from './decorator'; | ||
export * from './strategy-adapter'; | ||
export * from './metadata-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,26 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: loopback | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Keys} from '@loopback/core'; | ||
import {Constructor, Provider, inject} from '@loopback/context'; | ||
import {Metadata, getAuthenticateMetadata} from './decorator'; | ||
|
||
/** | ||
* @description Provides authentication metadata of a controller method | ||
* @example `context.bind('authentication.meta').toProvider(AuthMetadataProvider)` | ||
*/ | ||
export class AuthMetadataProvider<T> implements Provider<Metadata> { | ||
constructor( | ||
@inject(Keys.Context.CONTROLLER_CLASS) private readonly controllerClass: Constructor<T>, | ||
@inject(Keys.Context.CONTROLLER_METHOD_NAME) private readonly methodName: string, | ||
) {} | ||
|
||
/** | ||
* @returns IMetadata | ||
*/ | ||
value(): Metadata { | ||
return getAuthenticateMetadata(this.controllerClass, this.methodName); | ||
} | ||
} |
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,70 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: loopback | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {expect} from '@loopback/testlab'; | ||
import {Context, Provider} from '@loopback/context'; | ||
import {ParsedRequest, Keys} from '@loopback/core'; | ||
import {AuthMetadataProvider, Metadata, authenticate} from '../..'; | ||
|
||
describe('AuthMetadataProvider', () => { | ||
let provider: Provider<Metadata>; | ||
|
||
class TestController { | ||
@authenticate('my-strategy', {option1: 'value1', option2: 'value2'}) | ||
async whoAmI() {} | ||
} | ||
|
||
class ControllerWithNoMetadata { | ||
async whoAmI() {} | ||
} | ||
|
||
beforeEach(givenAuthMetadataProvider); | ||
|
||
describe('value()', () => { | ||
it('returns the authentication metadata of a controller method', async () => { | ||
const authMetadata: Metadata = await Promise.resolve(provider.value()); | ||
expect(authMetadata).to.be.eql({strategy: 'my-strategy', options: {option1: 'value1', option2: 'value2'}}); | ||
}); | ||
|
||
describe('context.get(provider_key)', () => { | ||
it('returns the authentication metadata of a controller method', async () => { | ||
const context: Context = new Context(); | ||
context.bind(Keys.Context.CONTROLLER_CLASS).to(TestController); | ||
context.bind(Keys.Context.CONTROLLER_METHOD_NAME).to('whoAmI'); | ||
context.bind(Keys.Context.CONTROLLER_METHOD_META).toProvider(AuthMetadataProvider); | ||
const authMetadata = await context.get('controller.method.meta'); | ||
expect(authMetadata).to.be.eql({strategy: 'my-strategy', options: {option1: 'value1', option2: 'value2'}}); | ||
}); | ||
}); | ||
|
||
describe('context.get(provider_key)', () => { | ||
it('returns the authentication metadata of a controller method', async () => { | ||
const context: Context = new Context(); | ||
context.bind(Keys.Context.CONTROLLER_CLASS).to(TestController); | ||
context.bind(Keys.Context.CONTROLLER_METHOD_NAME).to('whoAmI'); | ||
context.bind(Keys.Context.CONTROLLER_METHOD_META).toProvider(AuthMetadataProvider); | ||
const authMetadata = await context.get('controller.method.meta'); | ||
expect(authMetadata).to.be.eql({strategy: 'my-strategy', options: {option1: 'value1', option2: 'value2'}}); | ||
}); | ||
|
||
it('throws error if no authentication metadata is defined', async () => { | ||
const context: Context = new Context(); | ||
context.bind(Keys.Context.CONTROLLER_CLASS).to(ControllerWithNoMetadata); | ||
context.bind(Keys.Context.CONTROLLER_METHOD_NAME).to('whoAmI'); | ||
context.bind(Keys.Context.CONTROLLER_METHOD_META).toProvider(AuthMetadataProvider); | ||
try { | ||
await context.get('controller.method.meta'); | ||
expect.fail('0', '1', 'Metadata error should have been thrown', ''); | ||
} catch (err) { | ||
expect(err).to.have.property('message', 'No metadata defined'); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
function givenAuthMetadataProvider() { | ||
provider = new AuthMetadataProvider(TestController, 'whoAmI'); | ||
} | ||
}); |
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 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: @loopback/core | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export namespace Keys { | ||
export class Context { | ||
static readonly CONTROLLER_CLASS: string = 'controller.class'; | ||
static readonly CONTROLLER_METHOD_NAME: string = 'controller.method.name'; | ||
static readonly CONTROLLER_METHOD_META: string = 'controller.method.meta'; | ||
} | ||
|
||
export class Authentication { | ||
static readonly DECORATOR: string = 'authenticate'; | ||
} | ||
} |
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