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

Wrong Security decorator signature #91

Closed
Leogiciel opened this issue Apr 26, 2019 · 7 comments
Closed

Wrong Security decorator signature #91

Leogiciel opened this issue Apr 26, 2019 · 7 comments
Assignees

Comments

@Leogiciel
Copy link

Related to thiagobustamante/typescript-rest-swagger#76 (comment)

Security decorator needs two params to be generated correctfully.

eg :
@Security("bearer", ["admin","operator"])

In typescript-rest, you only implemented the second parameters (roles), so Swagger can't recognize it and generate swagger.yaml file properly :

security: - '[object Object]': []

instead of

security: - 'bearer': ["admin", "operator"]

@evelyne24
Copy link

I can also confirm the Auth header is broken.

@evelyne24
Copy link

This is really annoying, could you please merge the suggestion in typescript-rest-swagger? Otherwise, Swagger UI is unusable. Thank you!

@Leogiciel
Copy link
Author

@evelyne24, Two fixes have been given here : thiagobustamante/typescript-rest-swagger#76 (comment)

thiagobustamante added a commit that referenced this issue Jun 9, 2019
@thiagobustamante
Copy link
Owner

thiagobustamante commented Jun 9, 2019

This problem requires changes in both typescript-rest and typescript-rest-swagger projects... I finished the changes in typescript-rest and I am working to finish the changes in typescript-rest-swagger.

Typescript-rest-swagger will receive a major version soon. It is an old project that needs a refactory... And I was postponing it.

But the idea is:

Add an optional parameter to Server.registerAuthenticator. It will be a second parameter to keep compatibility with the previous contract:

Server.registerAuthenticator(new PassportAuthenticator(strategy), 'MyAuth');

The presious contract is still valid, and if not provided, it will register an authenticator with name 'default'.

It will solve the problem with swagger generation, and also allow us to define multiple authentication schemas in typescript-rest.

We can now reference which authenticator we want to use in our classes or methods with the @Security decorator receiving an extra optional parameter.

@Path('myPath')
export class MultipleAuthenticateRole {
    @Context
    public context: ServiceContext;

    @GET
    @Path('myPath1')
    @Security('ROLE_ADMIN', 'MyAuth')
    public test() {
        return this.context.request.user;
    }

    @GET
    @Path('myPath2')
    @Security('ROLE_ADMIN')
    public test2() {
        return this.context.request.user;
    }
}

In the previous example, the two endpoints will use different authenticators. The second one will use the 'default' authenticator.

It is already implemented (here) and will be available this week (I need to update the documentation before publish it to npm). But I still need to change typescript-rest-swagger. The change is to remove the decorator Security from that project and consider only the decorator defined here, in typescript-rest

@thiagobustamante
Copy link
Owner

Done. Available in 2.2.0

@Leogiciel
Copy link
Author

Thank you. Great work, will test it tomorrow. Best regards.

@evelyne24
Copy link

I have an issue when I try to implement it as in your example:

node_modules/typescript-rest-swagger/dist/metadata/controllerGenerator.js:93
            scopes: d.arguments[1] ? d.arguments[1].elements.map(function (e) { return e.text; }) : undefined
                                                             ^

TypeError: Cannot read property 'map' of undefined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants