Skip to content

Commit

Permalink
feat: basic auth api security
Browse files Browse the repository at this point in the history
  • Loading branch information
Sorikairox committed Apr 7, 2023
1 parent 7657b99 commit d491746
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
11 changes: 11 additions & 0 deletions decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ export const Tag = (tagName: string) =>
MetadataHelper.setMetadata(TAGS_KEY, tagName, target);
}
};

export const API_SECURITY = 'api-security';

export const ApiSecurity = (security: string) =>
(
// deno-lint-ignore ban-types
target: Object,
propertyKey: string | symbol,
) => {
MetadataHelper.setMetadata(API_SECURITY, security, target, propertyKey);
};
3 changes: 2 additions & 1 deletion example/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
ApiProperty,
ApiProperty, ApiSecurity,
BodyType,
Optional,
QueryType,
Expand Down Expand Up @@ -128,6 +128,7 @@ class MyController {
@Tag('second')
@Controller('second-endpoint')
class SecondController {
@ApiSecurity('basic')
@Get()
getSecond() {
return 'hello';
Expand Down
1 change: 1 addition & 0 deletions example/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const spec = new SpecBuilder()
.setDescription(description)
.setVersion(version)
.addTag(tagName)
.addBasicAuth()
.build();
const swaggerPath = '/api';
const document = await SwaggerModule.createDocument(app, spec) as any;
Expand Down
19 changes: 16 additions & 3 deletions method-definer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Swagger } from './swagger.ts';
import { Constructor } from './mod.ts';
import { API_PROPERTY, OPTIONAL_KEY, RETURNED_TYPE_KEY, TAGS_KEY } from './decorators.ts';
import { API_PROPERTY, API_SECURITY, OPTIONAL_KEY, RETURNED_TYPE_KEY, TAGS_KEY } from './decorators.ts';
import { RequestBodyBuilder, ResponseBuilder } from './builder.ts';
import DataType = Swagger.DataType;
import DataFormat = Swagger.DataFormat;
Expand Down Expand Up @@ -65,6 +65,7 @@ export class MethodDefiner {
this.addQueryParams(actualPath);
this.addResponse(actualPath);
this.addRequestBody(actualPath);
this.addSecurity(actualPath);
schemas = {
...schemas,
...this.schemas,
Expand All @@ -76,7 +77,19 @@ export class MethodDefiner {
};
}

private addTags(actuaPath: Operation) {
private addSecurity(actualPath: Operation) {
const methodSecurity = MetadataHelper.getMetadata<string>(
API_SECURITY,
this.Controller.prototype,
this.methodName,
);
if (methodSecurity) {
actualPath.security = [{
[methodSecurity]: []
}]
}
}
private addTags(actualPath: Operation) {
const controllerTag = MetadataHelper.getMetadata<string>(
TAGS_KEY,
this.Controller,
Expand All @@ -87,7 +100,7 @@ export class MethodDefiner {
this.methodName,
);
if (controllerTag || methodTag) {
actuaPath.tags = [controllerTag, methodTag].filter((t) => !!t);
actualPath.tags = [controllerTag, methodTag].filter((t) => !!t);
}
}

Expand Down
5 changes: 5 additions & 0 deletions spec/generate-schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const expectedSpec = {
'servers': [],
'openapi': '3.0.3',
'components': {
"securitySchemes":{"basic":{"type":"http","scheme":"basic"}},
'schemas': {
'NameSearch': {
'required': ['name'],
Expand Down Expand Up @@ -55,6 +56,9 @@ const expectedSpec = {
'operationId': 'getSecond',
'responses': { '200': { 'description': '' } },
'tags': ['second'],
'security': [{
'basic': [],
}]
},
},
'/second-endpoint/{id}/{name}': {
Expand Down Expand Up @@ -198,6 +202,7 @@ const spec = new SpecBuilder()
.setTitle(title)
.setDescription(description)
.setVersion(version)
.addBasicAuth()
.addTag(tagName)
.build();
const document = await SwaggerModule.createDocument(app, spec) as any;
Expand Down

0 comments on commit d491746

Please sign in to comment.