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

feat(Authentication) enable authentication strategies to contribute OASEnhancer #4693

Merged
merged 1 commit into from
Mar 12, 2020

Conversation

dougal83
Copy link
Contributor

@dougal83 dougal83 commented Feb 20, 2020

Extend AuthenticationStrategy to implement OASEnhancer.

Example usage:
App > jwt-strategy.ts:

// ...
@bind(asAuthStrategy, asSpecEnhancer)
export class JWTAuthenticationStrategy
  implements AuthenticationStrategy, OASEnhancer {
  // ...
  modifySpec(spec: OpenApiSpec): OpenApiSpec {
    return mergeSecuritySchemeToSpec(spec, this.name, {
      type: 'http',
      scheme: 'bearer',
      bearerFormat: 'JWT',
    });
  }
}

App > application.ts:

// ...
// authentication
this.add(createBindingFromClass(JWTAuthenticationStrategy));

See also #4554
Impl. #3669

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@dougal83
Copy link
Contributor Author

@derdeka Would this satisfy #3669? PTAL

Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

@dougal83 🙇‍♀ Thank you again for creating a follow-up PR for the security spec. I left a few suggestions.
sorry for the delay again 😅 finished the auth migration PR so I have enough bandwidth to help you apply this feature this month

packages/authentication/src/types.ts Outdated Show resolved Hide resolved
packages/authentication/src/types.ts Outdated Show resolved Hide resolved
@dougal83 dougal83 marked this pull request as ready for review March 6, 2020 00:21
@dougal83 dougal83 force-pushed the add-oas-security-enhancer branch 2 times, most recently from e8bef12 to 31b5c90 Compare March 6, 2020 13:57
@dougal83 dougal83 requested a review from jannyHou March 6, 2020 14:01
@dougal83
Copy link
Contributor Author

dougal83 commented Mar 6, 2020

Restricted to a single SecuritySchemeObject as I didn't see eventuality of adding multiple.

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

Thank you @dougal83 for the pull request, I appreciate the effort you are putting into this initiative ❤️

I like that your pull request includes test coverage to verify that security schemas are correctly contributed. What I like even more is that we have two examples of real-world security schemas that we can use when users come asking about examples 👍

I'd like to discuss few aspects of the proposed implementation, see my comments below.

packages/authentication/src/types.ts Outdated Show resolved Hide resolved
packages/authentication/src/types.ts Outdated Show resolved Hide resolved
packages/authentication/src/types.ts Outdated Show resolved Hide resolved
@dougal83 dougal83 force-pushed the add-oas-security-enhancer branch 2 times, most recently from 1c39e48 to e2b2f16 Compare March 7, 2020 23:08
@dougal83 dougal83 requested a review from bajtos March 7, 2020 23:41
@dougal83 dougal83 force-pushed the add-oas-security-enhancer branch from e2b2f16 to 9ad1ac9 Compare March 8, 2020 18:58
@dougal83 dougal83 changed the title feat(Authentication) enable authentication strategies to contribute OASEnhancer DO NOT MERGE: feat(Authentication) enable authentication strategies to contribute OASEnhancer Mar 8, 2020
@dougal83 dougal83 force-pushed the add-oas-security-enhancer branch from 9ad1ac9 to 59782c3 Compare March 9, 2020 20:21
@dougal83 dougal83 changed the title DO NOT MERGE: feat(Authentication) enable authentication strategies to contribute OASEnhancer feat(Authentication) enable authentication strategies to contribute OASEnhancer Mar 9, 2020
@dougal83 dougal83 force-pushed the add-oas-security-enhancer branch from 59782c3 to 2c57295 Compare March 9, 2020 20:34
Copy link
Contributor

@jannyHou jannyHou left a comment

Choose a reason for hiding this comment

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

🙇‍♀ LGTM when CI passes. Thanks @bajtos and @raymondfeng for joining the discussion, extending the enhancer class is much better than having a separate class(my original proposal).

I will refactor https://github.com/strongloop/loopback-next/tree/master/examples/access-control-migration/src/components/jwt-authentication accordingly as well.

@jannyHou jannyHou mentioned this pull request Mar 9, 2020
7 tasks
Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

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

I find this new version so much more elegant 👏

Can you please add a tsdoc comment and a unit test for mergeSecuritySchemeToSpec?

packages/openapi-v3/src/enhancers/spec-enhancer.service.ts Outdated Show resolved Hide resolved
@bajtos bajtos added the feature label Mar 10, 2020
@dougal83 dougal83 force-pushed the add-oas-security-enhancer branch from 2c57295 to d6a3e4d Compare March 10, 2020 16:22
add asAuthStrategy decorator for AuthenticationStrategy.

Decorate and extend AuthenticationStrategy as follows:
```ts
  @Bind(asAuthStrategy, asSpecEnhancer)
  class MyAuthenticationStrategy
    implements AuthenticationStrategy, OASEnhancer {
      // ...
    }
```

Bind to application:
```ts
  this.add(createBindingFromClass(MyAuthenticationStrategy));
```

Signed-off-by: Douglas McConnachie <[email protected]>
@dougal83 dougal83 force-pushed the add-oas-security-enhancer branch from d6a3e4d to 5535135 Compare March 10, 2020 16:27
@dougal83
Copy link
Contributor Author

The simplicity of the PR is down to great architectural design by the Loopback Team. It was surprising how little work was required in the end! Keep up the great work! ❤️

@jannyHou jannyHou merged commit df7dd2b into loopbackio:master Mar 12, 2020
@jannyHou
Copy link
Contributor

jannyHou commented Mar 12, 2020

🎉 🎉 Merged! Thank you @dougal83 for the effort and patience, next target is #4365. (design kudos goes to Miroslav and Raymond :p)

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

Successfully merging this pull request may close these issues.

4 participants