Skip to content

propagate configured AuthenticationStrategy to OpenApiSpec #3669

@derdeka

Description

@derdeka

Suggestion

The configured AuthenticationStrategy should propagate the securityschema into the generated openapi spec. Currently each endpoint need several configuration options to make this possible.

Use Cases

With configured securityschemas the api explorer handles authentication information automatically and sends it to the lb4 server.

Examples

Currently I'm doing something like this:

application.ts

import { merge } from 'lodash';
// ...
constructor(options: ApplicationConfig = {}) {
	// ...
	const spec = this.getSync(RestBindings.API_SPEC);
	merge(spec, {
	  components: {
		securitySchemes: {
		  BasicAuth: {
			type: 'http',
			scheme: 'basic',
		  },
		  BearerAuth: {
			type: 'http',
			scheme: 'bearer',
		  },
		  ApiKeyAuth: {
			type: 'apiKey',
			in: 'header',
			name: 'X-API-Key',
		  },
		},
	  },
	});
	// ...
}

user.controller.ts

...
@authorize('jwt')
@get('/users', {
  security: [{
    BearerAuth: [],
  }],
  responses: {
    [STATUS_CODE.OK]: {
      content: { [CONTENT_TYPE.JSON]: { schema: { type: 'array', items: getModelSchemaRef(User) } } },
    },
  },
})
async userFind(
...

Please note, that @authorize('jwt') and security is some kind of redundant and needs to be configured for each endpoint.

Acceptance criteria

  • Authentication strategy can contribute security schemas when it gets registered to an application. The security schema specs will be merged into OpenAPISpec.components.schemas. Modify the registerAuthenticationStrategy() method to handle the spec merge.

  • Update loopback4-shopping-example to leverage the new change.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions