Skip to content

Commit edd8d57

Browse files
authored
Merge pull request #36 from apiture/bugfix-35
update CLI to correctly parse the options from commander scopeDescriptionFile: opts.oidcToOauth2 || opts.scopes, and set a new convertOpenIdConnectToOAuth2 boolean flag scopeDescriptionFile: opts.oidcToOauth2 || opts.scopes,
2 parents e0bc3e4 + 6137d3c commit edd8d57

5 files changed

Lines changed: 28 additions & 15 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@apiture/openapi-down-convert",
3-
"version": "0.14.0",
3+
"version": "0.14.1",
44
"description": "Tool to down convert OpenAPI 3.1 to OpenAPI 3.0",
55
"main": "lib/src/index.js",
66
"bin": {

src/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ async function main(args: string[] = process.argv) {
3737
allOfTransform: Boolean(opts.allOf),
3838
authorizationUrl: opts.authorizationUrl,
3939
tokenUrl: opts.tokenUrl,
40-
scopeDescriptionFile: opts.scopes,
40+
scopeDescriptionFile: opts.oidcToOauth2 || opts.scopes,
41+
convertOpenIdConnectToOAuth2: !! (opts.oidcToOauth2 || opts.scopes),
4142
convertSchemaComments: opts.convertSchemaComments,
4243
};
4344
const converter = new Converter(source, cOpts);

src/converter.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export interface ConverterOptions {
4040
authorizationUrl?: string;
4141
/** The tokenUrl for openIdConnect -> oauth2 transformation */
4242
tokenUrl?: string;
43+
/**
44+
* If `true`, convert `openIdConnect` security scheme
45+
* to `oauth2`. Some tools (even those which purport to support OAS 3.0)
46+
* do not process the `openIdConnect` security scheme
47+
* (I'm looking at you, openapi-generator)
48+
*/
49+
convertOpenIdConnectToOAuth2?: boolean;
4350
/** Name of YAML/JSON file with scope descriptions.
4451
* This is a simple map in the format
4552
* `{ scope1: "description of scope1", ... }`
@@ -64,6 +71,7 @@ export class Converter {
6471
private scopeDescriptions = undefined;
6572
private convertSchemaComments = false;
6673
private returnCode = 0;
74+
private convertOpenIdConnectToOAuth2: boolean;
6775

6876
/**
6977
* Construct a new Converter
@@ -76,17 +84,17 @@ export class Converter {
7684
this.allOfTransform = Boolean(options?.allOfTransform);
7785
this.authorizationUrl = options?.authorizationUrl || 'https://www.example.com/oauth2/authorize';
7886
this.tokenUrl = options?.tokenUrl || 'https://www.example.com/oauth2/token';
79-
this.loadScopeDescriptions(options?.scopeDescriptionFile);
87+
this.convertOpenIdConnectToOAuth2 = options?.convertOpenIdConnectToOAuth2 || !!(options?.scopeDescriptionFile);
88+
if (this.convertOpenIdConnectToOAuth2) {
89+
this.loadScopeDescriptions(options.scopeDescriptionFile);
90+
}
8091
this.convertSchemaComments = options?.convertSchemaComments;
8192
}
8293

8394
/** Load the scopes.yaml file and save in this.scopeDescriptions
8495
* @throws Error if the file cannot be read or parsed as YAML/JSON
8596
*/
8697
private loadScopeDescriptions(scopeDescriptionFile?: string) {
87-
if (!scopeDescriptionFile) {
88-
return;
89-
}
9098
this.scopeDescriptions = yaml.load(fs.readFileSync(scopeDescriptionFile, 'utf8'));
9199
}
92100

@@ -136,8 +144,8 @@ export class Converter {
136144
this.removeLicenseIdentifier();
137145
this.convertSchemaRef();
138146
this.simplifyNonSchemaRef();
139-
if (this.scopeDescriptions) {
140-
this.convertSecuritySchemes();
147+
if (this.convertOpenIdConnectToOAuth2) {
148+
this.convertOpenIdConnectSecuritySchemesToOAuth2();
141149
}
142150
this.convertJsonSchemaExamples();
143151
this.convertJsonSchemaContentEncoding();
@@ -373,13 +381,16 @@ export class Converter {
373381
/** HTTP methods */
374382
static readonly HTTP_METHODS = ['delete', 'get', 'head', 'options', 'patch', 'post', 'put', 'trace' ];
375383
/**
376-
* OpenAPI 3.1 defines a new `openIdConnect` security scheme.
377-
* Down-convert the scheme to `oauth2` / authorization code flow.
384+
* OpenAPI 3.0 defines a new `openIdConnect` security scheme
385+
* but not all tools support `openIdConnect`, even if such tools
386+
* claim support for OAS 3.0
387+
* This converts the `openIdConnect` security scheme
388+
* to the `oauth2` security scheme.
378389
* Collect all the scopes used in any security requirements within
379390
* operations and add them to the scheme. Also define the
380391
* URLs to the `authorizationUrl` and `tokenUrl` of `oauth2`.
381392
*/
382-
convertSecuritySchemes() {
393+
convertOpenIdConnectSecuritySchemesToOAuth2() {
383394
const oauth2Scopes = (schemeName: string): object => {
384395
const scopes = {};
385396
const paths = this.openapi30?.paths;
@@ -410,8 +421,8 @@ export class Converter {
410421
scheme.type = 'oauth2';
411422
const openIdConnectUrl = scheme.openIdConnectUrl;
412423
scheme.description = `OAuth2 Authorization Code Flow. The client may
413-
GET the OpenID Connect configuration JSON from \`${openIdConnectUrl}\`
414-
to get the correct \`authorizationUrl\` and \`tokenUrl\`.`;
424+
GET the OpenID Connect configuration JSON from \`${openIdConnectUrl}\`
425+
to get the correct \`authorizationUrl\` and \`tokenUrl\`.`;
415426
delete scheme.openIdConnectUrl;
416427
const scopes = oauth2Scopes(schemeName);
417428
scheme.flows = {

test/converter.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ describe('resolver test suite', () => {
134134
authorizationUrl: 'https://www.example.com/test/authorize',
135135
tokenUrl: 'https://www.example.com/test/token',
136136
scopeDescriptionFile: path.join(__dirname, 'data/scopes.yaml'),
137+
convertOpenIdConnectToOAuth2: true
137138
};
138139
const converter = new Converter(input, options);
139140
const converted: any = converter.convert();

0 commit comments

Comments
 (0)