From 47e20024db5c40c6329a4c5abfc5f3e90ac52c66 Mon Sep 17 00:00:00 2001 From: Alex Rothberg Date: Mon, 11 Apr 2022 16:01:02 -0400 Subject: [PATCH] Add support for Bearer Authentication Signed-off-by: Alex Rothberg --- .../openapi-to-graphql/src/auth_builder.ts | 4 +++ .../openapi-to-graphql/src/preprocessor.ts | 27 ++++++++++++++++--- .../src/resolver_builder.ts | 9 +++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/openapi-to-graphql/src/auth_builder.ts b/packages/openapi-to-graphql/src/auth_builder.ts index 625f0478..950f4358 100644 --- a/packages/openapi-to-graphql/src/auth_builder.ts +++ b/packages/openapi-to-graphql/src/auth_builder.ts @@ -85,6 +85,10 @@ export function createAndLoadViewer( viewerType = 'basicAuth' break + case 'bearer': + viewerType = 'bearerAuth' + break + default: handleWarning({ mitigationType: MitigationTypes.UNSUPPORTED_HTTP_SECURITY_SCHEME, diff --git a/packages/openapi-to-graphql/src/preprocessor.ts b/packages/openapi-to-graphql/src/preprocessor.ts index 0a51b679..defcfe2e 100644 --- a/packages/openapi-to-graphql/src/preprocessor.ts +++ b/packages/openapi-to-graphql/src/preprocessor.ts @@ -584,6 +584,27 @@ function getProcessedSecuritySchemes( } break + case 'bearer': + description = `Bearer auth credentials for security protocol '${schemeKey}'` + + parameters = { + token: Oas3Tools.sanitize( + `${schemeKey}_token`, + Oas3Tools.CaseStyle.camelCase + ) + } + + schema = { + type: 'object', + description, + properties: { + token: { + type: 'string' + } + } + } + break + default: handleWarning({ mitigationType: MitigationTypes.UNSUPPORTED_HTTP_SECURITY_SCHEME, @@ -700,12 +721,12 @@ export function createDataDef( const existingDataDef = data.defs[index] /** - * Special handling for oneOf. Subdefinitions are always an array + * Special handling for oneOf. Subdefinitions are always an array * (see createOneOfUnion) */ if ( - existingDataDef.targetGraphQLType === TargetGraphQLType.oneOfUnion && - Array.isArray(existingDataDef.subDefinitions) + existingDataDef.targetGraphQLType === TargetGraphQLType.oneOfUnion && + Array.isArray(existingDataDef.subDefinitions) ) { existingDataDef.subDefinitions.forEach((def) => { collapseLinksIntoDataDefinition({ diff --git a/packages/openapi-to-graphql/src/resolver_builder.ts b/packages/openapi-to-graphql/src/resolver_builder.ts index 94f7b42d..79dfac13 100644 --- a/packages/openapi-to-graphql/src/resolver_builder.ts +++ b/packages/openapi-to-graphql/src/resolver_builder.ts @@ -671,9 +671,9 @@ export function getResolver({ if (form) { /** - * When there is a form, remove default content type and leave + * When there is a form, remove default content type and leave * computation of content-type header to fetch - * + * * See https://github.com/github/fetch/issues/505#issuecomment-293064470 */ Object.assign(options.headers, form.getHeaders()) @@ -1090,6 +1090,11 @@ function getAuthOptions( credentials ).toString('base64')}` break + case 'bearer': + const token = + _openAPIToGraphQL.security[sanitizedSecurityRequirement].token + authHeaders['Authorization'] = `Bearer ${token}` + break default: throw new Error( `Cannot recognize http security scheme ` +