Skip to content

Commit

Permalink
refactor: port security scheme model to ts (asyncapi#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns authored and magicmatatjahu committed Oct 3, 2022
1 parent e788a1d commit 7f0fed5
Show file tree
Hide file tree
Showing 20 changed files with 471 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/models/oauth-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BaseModel } from './base';
import { ExtensionsMixinInterface } from './mixins';

export interface OAuthFlowInterface extends BaseModel, ExtensionsMixinInterface {
authorizationUrl(): string | undefined;
hasRefreshUrl(): boolean;
refreshUrl(): string | undefined;
scopes(): Record<string, string> | undefined;
tokenUrl(): string | undefined;
}
14 changes: 14 additions & 0 deletions src/models/oauth-flows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { OAuthFlowInterface } from './oauth-flow';
import { BaseModel } from './base';
import {ExtensionsMixinInterface} from './mixins';

export interface OAuthFlowsInterface extends BaseModel, ExtensionsMixinInterface {
hasAuthorizationCode(): boolean;
authorizationCode(): OAuthFlowInterface | undefined;
hasClientCredentials(): boolean
clientCredentials(): OAuthFlowInterface | undefined;
hasImplicit(): boolean;
implicit(): OAuthFlowInterface | undefined;
hasPassword(): boolean;
password(): OAuthFlowInterface | undefined;
}
18 changes: 18 additions & 0 deletions src/models/security-scheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { BaseModel } from './base';
import { DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';
import { OAuthFlowsInterface } from './oauth-flows';

export type SecuritySchemaType = 'userPassword' | 'apiKey' | 'X509' | 'symmetricEncryption' | 'asymmetricEncryption' | 'httpApiKey' | 'http' | 'oauth2' | 'openIdConnect' | 'plain' | 'scramSha256' | 'scramSha512' | 'gssapi';


export interface SecuritySchemeInterface extends BaseModel, DescriptionMixinInterface, ExtensionsMixinInterface {
id(): string
hasBearerFormat(): boolean;
bearerFormat(): string | undefined;
openIdConnectUrl(): string;
scheme(): string | undefined;
flows(): OAuthFlowsInterface | undefined;
scopes(): string[];
type(): SecuritySchemaType;
in(): string | undefined;
}
4 changes: 4 additions & 0 deletions src/models/security-schemes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Collection} from './collection';
import { SecuritySchemeInterface } from './security-scheme';

export interface SecuritySchemesInterface extends Collection<SecuritySchemeInterface> {}
8 changes: 7 additions & 1 deletion src/models/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ export { Extensions as ExtensionsV2, Extension as ExtensionV2 } from './mixins/e
export { ExternalDocumentation as ExternalDocumentationV2 } from './mixins/external-docs';
export { Tags as TagsV2, Tag as TagV2 } from './mixins/tags';
export { Server as ServerV2 } from './server';
export { Servers as ServersV2 } from './servers';
export { Servers as ServersV2 } from './servers';
export { SecurityScheme as SecuritySchemeV2 } from './security-scheme';
export { SecuritySchemes as SecuritySchemesV2 } from './security-schemes';
export { ServerVariable as ServerVariableV2 } from './server-variable';
export { ServerVariables as ServerVariablesV2 } from './server-variables';
export {OAuthFlow as OAuthFlowV2} from './oauth-flow';
export {OAuthFlows as OAuthFlowsV2} from './oauth-flows';
23 changes: 23 additions & 0 deletions src/models/v2/oauth-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { OAuthFlowInterface } from '../oauth-flow';
import { BaseModel } from '../base';
import { Mixin } from '../utils';
import { ExtensionsMixin } from './mixins/extensions';

export class OAuthFlow extends Mixin(BaseModel, ExtensionsMixin) implements OAuthFlowInterface {
authorizationUrl(): string | undefined {
return this._json.authorizationUrl;
}
hasRefreshUrl(): boolean {
return !!this._json.refreshUrl;
}
refreshUrl(): string | undefined {
return this._json.refreshUrl;
}
scopes(): Record<string, string> | undefined {
return this._json.scopes;
}
tokenUrl(): string | undefined {
return this._json.tokenUrl;
}

}
39 changes: 39 additions & 0 deletions src/models/v2/oauth-flows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { OAuthFlowsInterface } from '../oauth-flows';
import { BaseModel } from '../base';
import { Mixin } from '../utils';
import { ExtensionsMixin } from './mixins/extensions';
import { OAuthFlowInterface } from 'models/oauth-flow';
import { OAuthFlow } from './oauth-flow';

export class OAuthFlows extends Mixin(BaseModel, ExtensionsMixin) implements OAuthFlowsInterface {
hasAuthorizationCode(): boolean {
return !!this._json.authorizationCode;
}
authorizationCode(): OAuthFlowInterface | undefined {
if (!this._json.authorizationCode) return undefined;
return new OAuthFlow(this._json.authorizationCode);
}
hasClientCredentials(): boolean {
return !!this._json.clientCredentials;
}
clientCredentials(): OAuthFlowInterface | undefined {
if (!this._json.clientCredentials) return undefined;
return new OAuthFlow(this._json.clientCredentials);
}
hasImplicit(): boolean {
return !!this._json.implicit;
}
implicit(): OAuthFlowInterface | undefined {
if (!this._json.implicit) return undefined;
return new OAuthFlow(this._json.implicit);
}
hasPassword(): boolean {
return !!this._json.password;
}
password(): OAuthFlowInterface | undefined {
if (!this._json.password) return undefined;
return new OAuthFlow(this._json.password);
}


}
55 changes: 55 additions & 0 deletions src/models/v2/security-scheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { BaseModel, ModelMetadata } from '../base';
import { Mixin } from '../utils';
import { DescriptionMixin } from './mixins/description';
import { ExtensionsMixin } from './mixins/extensions';
import { SecuritySchemaType, SecuritySchemeInterface } from '../security-scheme';
import { OAuthFlowsInterface } from 'models/oauth-flows';
import { OAuthFlows } from './oauth-flows';

export class SecurityScheme extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements SecuritySchemeInterface {
constructor(
private readonly _id: string,
_json: Record<string, any>,
_meta: ModelMetadata = {} as any
) {
super(_json, _meta);
}

id(): string {
return this._id;
}

hasBearerFormat(): boolean {
return !!this._json.bearerFormat;
}

bearerFormat(): string | undefined {
return this._json.bearerFormat;
}

openIdConnectUrl(): string {
return this._json.openIdConnectUrl;
}

scheme(): string | undefined {
return this._json.scheme
}

flows(): OAuthFlowsInterface | undefined {
if(!this._json.flows) return undefined;
return new OAuthFlows(this._json.flows);
}

scopes(): string[] {
return this._json.scopes;
}

type(): SecuritySchemaType {
return this._json.type;
}

in(): string | undefined {
return this._json.in;
}

}
12 changes: 12 additions & 0 deletions src/models/v2/security-schemes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SecuritySchemesInterface } from '../security-schemes';
import { Collection } from '../collection';
import { SecuritySchemeInterface } from '../security-scheme';

export class SecuritySchemes extends Collection<SecuritySchemeInterface> implements SecuritySchemesInterface {
get(id: string): SecuritySchemeInterface | undefined {
return this.collections.find(securityScheme => securityScheme.id() === id);
}
has(id: string): boolean {
return this.collections.some(securityScheme => securityScheme.id() === id);
}
}
5 changes: 3 additions & 2 deletions src/models/v2/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Ex
).map(
([serverVariableName, serverVariable]) => this.createModel(
ServerVariable, serverVariable, {
id: serverVariableName,
pointer: `${this._meta.pointer}/variables/${serverVariableName}`
id: serverVariableName,
pointer: `${this._meta.pointer}/variables/${serverVariableName}`
}
)
))
}

}
8 changes: 7 additions & 1 deletion src/models/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ export { Extensions as ExtensionsV3, Extension as ExtensionV3 } from './mixins/e
export { ExternalDocumentation as ExternalDocumentationV3 } from './mixins/external-docs';
export { Tags as TagsV3, Tag as TagV3 } from './mixins/tags';
export { Server as ServerV3 } from './server';
export { Servers as ServersV3 } from './servers';
export { Servers as ServersV3 } from './servers';
export { SecurityScheme as SecuritySchemeV3 } from './security-scheme';
export { SecuritySchemes as SecuritySchemesV3 } from './security-schemes';
export { ServerVariable as ServerVariableV3 } from './server-variable';
export { ServerVariables as ServerVariablesV3 } from './server-variables';
export {OAuthFlow as OAuthFlowV3} from './oauth-flow';
export {OAuthFlows as OAuthFlowsV3} from './oauth-flows';
24 changes: 24 additions & 0 deletions src/models/v3/oauth-flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import { OAuthFlowInterface } from '../oauth-flow';
import { BaseModel } from '../base';
import { Mixin } from '../utils';
import { ExtensionsMixin } from './mixins/extensions';

export class OAuthFlow extends Mixin(BaseModel, ExtensionsMixin) implements OAuthFlowInterface {
authorizationUrl(): string | undefined {
return this._json.authorizationUrl;
}
hasRefreshUrl(): boolean {
return !!this._json.refreshUrl;
}
refreshUrl(): string | undefined {
return this._json.refreshUrl;
}
scopes(): Record<string, string> | undefined {
return this._json.scopes;
}
tokenUrl(): string | undefined {
return this._json.tokenUrl;
}

}
40 changes: 40 additions & 0 deletions src/models/v3/oauth-flows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import { OAuthFlowsInterface } from '../oauth-flows';
import { BaseModel } from '../base';
import { Mixin } from '../utils';
import { ExtensionsMixin } from './mixins/extensions';
import { OAuthFlowInterface } from 'models/oauth-flow';
import { OAuthFlow } from './oauth-flow';

export class OAuthFlows extends Mixin(BaseModel, ExtensionsMixin) implements OAuthFlowsInterface {
hasAuthorizationCode(): boolean {
return !!this._json.authorizationCode;
}
authorizationCode(): OAuthFlowInterface | undefined {
if (!this._json.authorizationCode) return undefined;
return new OAuthFlow(this._json.authorizationCode);
}
hasClientCredentials(): boolean {
return !!this._json.clientCredentials;
}
clientCredentials(): OAuthFlowInterface | undefined {
if (!this._json.clientCredentials) return undefined;
return new OAuthFlow(this._json.clientCredentials);
}
hasImplicit(): boolean {
return !!this._json.implicit;
}
implicit(): OAuthFlowInterface | undefined {
if (!this._json.implicit) return undefined;
return new OAuthFlow(this._json.implicit);
}
hasPassword(): boolean {
return !!this._json.password;
}
password(): OAuthFlowInterface | undefined {
if (!this._json.password) return undefined;
return new OAuthFlow(this._json.password);
}


}
55 changes: 55 additions & 0 deletions src/models/v3/security-scheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { BaseModel, ModelMetadata } from '../base';
import { Mixin } from '../utils';
import { DescriptionMixin } from './mixins/description';
import { ExtensionsMixin } from './mixins/extensions';
import { SecuritySchemaType, SecuritySchemeInterface } from '../security-scheme';
import { OAuthFlowsInterface } from 'models/oauth-flows';
import { OAuthFlows } from './oauth-flows';

export class SecurityScheme extends Mixin(BaseModel, DescriptionMixin, ExtensionsMixin) implements SecuritySchemeInterface {
constructor(
private readonly _id: string,
_json: Record<string, any>,
_meta: ModelMetadata = {} as any
) {
super(_json, _meta);
}

id(): string {
return this._id;
}

hasBearerFormat(): boolean {
return !!this._json.bearerFormat;
}

bearerFormat(): string | undefined {
return this._json.bearerFormat;
}

openIdConnectUrl(): string {
return this._json.openIdConnectUrl;
}

scheme(): string | undefined {
return this._json.scheme
}

flows(): OAuthFlowsInterface | undefined {
if(!this._json.flows) return undefined;
return new OAuthFlows(this._json.flows);
}

scopes(): string[] {
return this._json.scopes;
}

type(): SecuritySchemaType {
return this._json.type;
}

in(): string | undefined {
return this._json.in;
}

}
12 changes: 12 additions & 0 deletions src/models/v3/security-schemes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SecuritySchemesInterface } from '../security-schemes';
import { Collection } from '../collection';
import { SecuritySchemeInterface } from '../security-scheme';

export class SecuritySchemes extends Collection<SecuritySchemeInterface> implements SecuritySchemesInterface {
get(id: string): SecuritySchemeInterface | undefined {
return this.collections.find(securityScheme => securityScheme.id() === id);
}
has(id: string): boolean {
return this.collections.some(securityScheme => securityScheme.id() === id);
}
}
6 changes: 4 additions & 2 deletions src/models/v3/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ServerVariablesInterface } from '../server-variables';
import type { ModelMetadata } from "../base";
import type { ServerInterface } from '../server';


export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, ExtensionsMixin) implements ServerInterface {
constructor(
private readonly _id: string,
Expand Down Expand Up @@ -47,10 +48,11 @@ export class Server extends Mixin(BaseModel, BindingsMixin, DescriptionMixin, Ex
).map(
([serverVariableName, serverVariable]) => this.createModel(
ServerVariable, serverVariable, {
id: serverVariableName,
pointer: `${this._meta.pointer}/variables/${serverVariableName}`
id: serverVariableName,
pointer: `${this._meta.pointer}/variables/${serverVariableName}`
}
)
))
}

}
Loading

0 comments on commit 7f0fed5

Please sign in to comment.