Skip to content

Commit 47e2002

Browse files
cancan101Alan-Cha
authored andcommitted
Add support for Bearer Authentication
Signed-off-by: Alex Rothberg <[email protected]>
1 parent bbcf3fd commit 47e2002

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

packages/openapi-to-graphql/src/auth_builder.ts

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export function createAndLoadViewer<TSource, TContext, TArgs>(
8585
viewerType = 'basicAuth'
8686
break
8787

88+
case 'bearer':
89+
viewerType = 'bearerAuth'
90+
break
91+
8892
default:
8993
handleWarning({
9094
mitigationType: MitigationTypes.UNSUPPORTED_HTTP_SECURITY_SCHEME,

packages/openapi-to-graphql/src/preprocessor.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,27 @@ function getProcessedSecuritySchemes<TSource, TContext, TArgs>(
584584
}
585585
break
586586

587+
case 'bearer':
588+
description = `Bearer auth credentials for security protocol '${schemeKey}'`
589+
590+
parameters = {
591+
token: Oas3Tools.sanitize(
592+
`${schemeKey}_token`,
593+
Oas3Tools.CaseStyle.camelCase
594+
)
595+
}
596+
597+
schema = {
598+
type: 'object',
599+
description,
600+
properties: {
601+
token: {
602+
type: 'string'
603+
}
604+
}
605+
}
606+
break
607+
587608
default:
588609
handleWarning({
589610
mitigationType: MitigationTypes.UNSUPPORTED_HTTP_SECURITY_SCHEME,
@@ -700,12 +721,12 @@ export function createDataDef<TSource, TContext, TArgs>(
700721
const existingDataDef = data.defs[index]
701722

702723
/**
703-
* Special handling for oneOf. Subdefinitions are always an array
724+
* Special handling for oneOf. Subdefinitions are always an array
704725
* (see createOneOfUnion)
705726
*/
706727
if (
707-
existingDataDef.targetGraphQLType === TargetGraphQLType.oneOfUnion &&
708-
Array.isArray(existingDataDef.subDefinitions)
728+
existingDataDef.targetGraphQLType === TargetGraphQLType.oneOfUnion &&
729+
Array.isArray(existingDataDef.subDefinitions)
709730
) {
710731
existingDataDef.subDefinitions.forEach((def) => {
711732
collapseLinksIntoDataDefinition({

packages/openapi-to-graphql/src/resolver_builder.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,9 @@ export function getResolver<TSource, TContext, TArgs>({
671671

672672
if (form) {
673673
/**
674-
* When there is a form, remove default content type and leave
674+
* When there is a form, remove default content type and leave
675675
* computation of content-type header to fetch
676-
*
676+
*
677677
* See https://github.com/github/fetch/issues/505#issuecomment-293064470
678678
*/
679679
Object.assign(options.headers, form.getHeaders())
@@ -1090,6 +1090,11 @@ function getAuthOptions<TSource, TContext, TArgs>(
10901090
credentials
10911091
).toString('base64')}`
10921092
break
1093+
case 'bearer':
1094+
const token =
1095+
_openAPIToGraphQL.security[sanitizedSecurityRequirement].token
1096+
authHeaders['Authorization'] = `Bearer ${token}`
1097+
break
10931098
default:
10941099
throw new Error(
10951100
`Cannot recognize http security scheme ` +

0 commit comments

Comments
 (0)