Skip to content

Commit

Permalink
Add support for Bearer Authentication
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Rothberg <[email protected]>
  • Loading branch information
cancan101 authored and Alan-Cha committed May 3, 2022
1 parent bbcf3fd commit 47e2002
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/openapi-to-graphql/src/auth_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export function createAndLoadViewer<TSource, TContext, TArgs>(
viewerType = 'basicAuth'
break

case 'bearer':
viewerType = 'bearerAuth'
break

default:
handleWarning({
mitigationType: MitigationTypes.UNSUPPORTED_HTTP_SECURITY_SCHEME,
Expand Down
27 changes: 24 additions & 3 deletions packages/openapi-to-graphql/src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,27 @@ function getProcessedSecuritySchemes<TSource, TContext, TArgs>(
}
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,
Expand Down Expand Up @@ -700,12 +721,12 @@ export function createDataDef<TSource, TContext, TArgs>(
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({
Expand Down
9 changes: 7 additions & 2 deletions packages/openapi-to-graphql/src/resolver_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,9 @@ export function getResolver<TSource, TContext, TArgs>({

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())
Expand Down Expand Up @@ -1090,6 +1090,11 @@ function getAuthOptions<TSource, TContext, TArgs>(
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 ` +
Expand Down

0 comments on commit 47e2002

Please sign in to comment.