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

propagate configured AuthenticationStrategy to OpenApiSpec #3669

Closed
2 tasks
derdeka opened this issue Sep 5, 2019 · 8 comments
Closed
2 tasks

propagate configured AuthenticationStrategy to OpenApiSpec #3669

derdeka opened this issue Sep 5, 2019 · 8 comments

Comments

@derdeka
Copy link
Contributor

derdeka commented Sep 5, 2019

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.

@derdeka derdeka added the feature label Sep 5, 2019
@derdeka
Copy link
Contributor Author

derdeka commented Sep 5, 2019

I found a simpler way without modifying all endpoints:

constants.ts

export namespace SECURITY_REQUIREMENT {
  export const DEFAULT: SecurityRequirementObject[] = [
    {
      BearerAuth: [],
    },
  ]

  export const NONE: SecurityRequirementObject[] = [
  ]
}

application.ts

import { merge } from 'lodash';
// ...
constructor(options: ApplicationConfig = {}) {
	// ...
	const spec = this.getSync(RestBindings.API_SPEC);
	merge(spec, {
          security: SECURITY_REQUIREMENT.DEFAULT,
	  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('none')
@post('/users/login', {
  security: SECURITY_REQUIREMENT.NONE,
  responses: {
    // ...
  },
})
async userLogin(
...

@derdeka
Copy link
Contributor Author

derdeka commented Sep 5, 2019

related to #2027

@jannyHou
Copy link
Contributor

jannyHou commented Sep 9, 2019

@derdeka I am working on #2027 :) And have similar code as

mport { 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',
		  },
		},
	  },
	});
	// ...
}

on my local. Your suggestion seems reasonable to me. Will submit a draft PR in the near future and we can discuss further.

@jannyHou
Copy link
Contributor

jannyHou commented Sep 9, 2019

see draft PR loopbackio/loopback4-example-shopping#267

@jannyHou
Copy link
Contributor

Acceptance Criteria updated.

@dhmlau
Copy link
Member

dhmlau commented Sep 24, 2019

Related to #2027

@jannyHou
Copy link
Contributor

jannyHou commented Jan 8, 2020

some update:

The openapi spec enhancer service is created in #4258.

To allow contributing custom security spec, we need #4380 happen first.

And two more improvements:
#4386
#4385

@achrinza
Copy link
Member

Closing as done (see #4693).

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

5 participants