Skip to content

Commit

Permalink
include auth token in functional headers
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mcafee committed Nov 15, 2023
1 parent 2a72796 commit 092beb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export interface AWSAppSyncRealTimeProviderOptions {
| Record<string, string>
| (() => Promise<Record<string, string>>);
additionalCustomHeaders?: Record<string, string>;
authToken?: string;
}

type AWSAppSyncRealTimeAuthInput =
Expand Down Expand Up @@ -201,6 +202,7 @@ export class AWSAppSyncRealTimeProvider {
authenticationType,
additionalHeaders,
apiKey,
authToken,
} = options || {};

return new Observable(observer => {
Expand Down Expand Up @@ -232,6 +234,7 @@ export class AWSAppSyncRealTimeProvider {
appSyncGraphqlEndpoint,
additionalHeaders,
apiKey,
authToken,
},
observer,
subscriptionId,
Expand Down Expand Up @@ -312,6 +315,7 @@ export class AWSAppSyncRealTimeProvider {
region,
graphql_headers = () => ({}),
additionalHeaders = {},
authToken,
} = options;

let additionalCustomHeaders: Record<string, string> = {};
Expand All @@ -322,6 +326,14 @@ export class AWSAppSyncRealTimeProvider {
additionalCustomHeaders = additionalHeaders;
}

// if an authorization header is set, have the explicit authToken take precedence
if (authToken) {
additionalCustomHeaders = {
...additionalCustomHeaders,
Authorization: authToken,
};
}

const subscriptionState: SUBSCRIPTION_STATUS = SUBSCRIPTION_STATUS.PENDING;
const data = {
query,
Expand Down
36 changes: 20 additions & 16 deletions packages/api-graphql/src/internals/InternalGraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,7 @@ export class InternalGraphQLAPIClass {
const { operation: operationType } =
operationDef as OperationDefinitionNode;

let headers = additionalHeaders || {};

// if an authorization header is set, have the explicit authToken take precedence
if (authToken) {
if (typeof headers === 'object') {
headers = {
...headers,
Authorization: authToken,
};
}
}
const headers = additionalHeaders || {};

switch (operationType) {
case 'query':
Expand All @@ -198,7 +188,8 @@ export class InternalGraphQLAPIClass {
{ query, variables, authMode },
headers,
abortController,
customUserAgentDetails
customUserAgentDetails,
authToken
);
} else {
const wrapper = (amplifyInstance: AmplifyClassV6) =>
Expand All @@ -207,7 +198,8 @@ export class InternalGraphQLAPIClass {
{ query, variables, authMode },
headers,
abortController,
customUserAgentDetails
customUserAgentDetails,
authToken
);

responsePromise = amplify(wrapper) as unknown as Promise<
Expand All @@ -225,7 +217,8 @@ export class InternalGraphQLAPIClass {
amplify as AmplifyClassV6,
{ query, variables, authMode },
headers,
customUserAgentDetails
customUserAgentDetails,
authToken
);
default:
throw new Error(`invalid operation type: ${operationType}`);
Expand All @@ -237,7 +230,8 @@ export class InternalGraphQLAPIClass {
{ query, variables, authMode }: GraphQLOptions,
additionalHeaders: CustomHeaders = {},
abortController: AbortController,
customUserAgentDetails?: CustomUserAgentDetails
customUserAgentDetails?: CustomUserAgentDetails,
authToken?: string
): Promise<GraphQLResult<T>> {
const {
region: region,
Expand Down Expand Up @@ -269,6 +263,14 @@ export class InternalGraphQLAPIClass {
additionalCustomHeaders = additionalHeaders;
}

// if an authorization header is set, have the explicit authToken take precedence
if (authToken) {
additionalCustomHeaders = {
...additionalCustomHeaders,
Authorization: authToken,
};
}

// TODO: Figure what we need to do to remove `!`'s.
const headers = {
...(!customEndpoint &&
Expand Down Expand Up @@ -416,7 +418,8 @@ export class InternalGraphQLAPIClass {
amplify: AmplifyClassV6,
{ query, variables, authMode }: GraphQLOptions,
additionalHeaders: CustomHeaders = {},
customUserAgentDetails?: CustomUserAgentDetails
customUserAgentDetails?: CustomUserAgentDetails,
authToken?: string
): Observable<any> {
const config = resolveConfig(amplify);

Expand All @@ -429,6 +432,7 @@ export class InternalGraphQLAPIClass {
authenticationType: authMode || config?.defaultAuthMode,
apiKey: config?.apiKey,
additionalHeaders,
authToken,
},
customUserAgentDetails
);
Expand Down

0 comments on commit 092beb0

Please sign in to comment.