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 ed1a3fa
Showing
8 changed files
with
129 additions
and
6 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,8 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: @loopback/authentication | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export class Keys { | ||
static readonly AUTHENTICATION_KEY: 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
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,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 {RequestContextkeys} from '@loopback/core'; | ||
import {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 implements Provider<Metadata> { | ||
constructor( | ||
@inject(RequestContextkeys.CONTROLLER_CLASS) private readonly controllerClass: Object, | ||
@inject(RequestContextkeys.CONTROLLER_METHOD_NAME) private readonly methodName: string, | ||
) {} | ||
|
||
/** | ||
* @returns IMetadata | ||
*/ | ||
value(): Metadata { | ||
return getAuthenticateMetadata(this.controllerClass, this.methodName); | ||
} | ||
} |
File renamed without changes.
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,73 @@ | ||
// 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, RequestContextkeys} 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() {} | ||
} | ||
|
||
const testController: TestController = new TestController(); | ||
const controllerNoMetadata: ControllerWithNoMetadata = new ControllerWithNoMetadata(); | ||
|
||
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(RequestContextkeys.CONTROLLER_CLASS).to(testController); | ||
context.bind(RequestContextkeys.CONTROLLER_METHOD_NAME).to('whoAmI'); | ||
context.bind(RequestContextkeys.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(RequestContextkeys.CONTROLLER_CLASS).to(testController); | ||
context.bind(RequestContextkeys.CONTROLLER_METHOD_NAME).to('whoAmI'); | ||
context.bind(RequestContextkeys.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(RequestContextkeys.CONTROLLER_CLASS).to(controllerNoMetadata); | ||
context.bind(RequestContextkeys.CONTROLLER_METHOD_NAME).to('whoAmI'); | ||
context.bind(RequestContextkeys.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,10 @@ | ||
// Copyright IBM Corp. 2017. All Rights Reserved. | ||
// Node module: @loopback/authentication | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
export class RequestContextkeys { | ||
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'; | ||
} |
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