Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optional auth metadata #2218

Merged
merged 1 commit into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ import {AuthenticationMetadata, getAuthenticateMetadata} from '../decorators';
export class AuthMetadataProvider
implements Provider<AuthenticationMetadata | undefined> {
constructor(
@inject(CoreBindings.CONTROLLER_CLASS)
@inject(CoreBindings.CONTROLLER_CLASS, {optional: true})
private readonly controllerClass: Constructor<{}>,
@inject(CoreBindings.CONTROLLER_METHOD_NAME)
@inject(CoreBindings.CONTROLLER_METHOD_NAME, {optional: true})
private readonly methodName: string,
) {}

/**
* @returns AuthenticationMetadata
*/
value(): AuthenticationMetadata | undefined {
if (!this.controllerClass || !this.methodName) return;
return getAuthenticateMetadata(this.controllerClass, this.methodName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ describe('AuthMetadataProvider', () => {
);
expect(authMetadata).to.be.undefined();
});

it('returns undefined when the class or method is missing', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we separate this test into two? i.e. one for class missing and the other class defined but method missing? or is that not useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b-admike The method lives in the controller, so if the controller class is missing then the method will definitely be missing. Which means the only valid scenario is no controller class + no method.

What we could improve in the next PR is the scenario that Miroslav brings in his comment:

const spec: OperationObject = { /*...*/ };
function greet(name: string) {
  return `hello ${name}`;
}

app.route('get', '/', spec, greet);

And as Raymond said, the auth metadata could be included in the OpenAPI spec.

const context: Context = new Context();
context
.bind(CoreBindings.CONTROLLER_METHOD_META)
.toProvider(AuthMetadataProvider);
const authMetadata = await context.get(
CoreBindings.CONTROLLER_METHOD_META,
);
expect(authMetadata).to.be.undefined();
});
});
});

Expand Down