Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #652 from Shopify/allow_extra_params_in_oauth
Browse files Browse the repository at this point in the history
Don't ignore previous headers when beginning OAuth
  • Loading branch information
paulomarg authored Dec 23, 2022
2 parents 16730c5 + 0fca891 commit efe0770
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

- [Patch] Don't ignore previous headers when beginning OAuth [#652](https://github.com/Shopify/shopify-api-js/pull/652)
- [Patch] Export missing client types from package [#648](https://github.com/Shopify/shopify-api-js/pull/648)
- [Patch] Add an info-level log of API library version and runtime environment string during initialization, to aid in troubleshooting [650](https://github.com/Shopify/shopify-api-js/pull/650)

Expand Down
12 changes: 12 additions & 0 deletions adapters/node/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ export async function nodeConvertRequest(
};
}

export async function nodeConvertIncomingResponse(
adapterArgs: NodeAdapterArgs,
): Promise<NormalizedResponse> {
return {
statusCode: adapterArgs.rawResponse.statusCode,
statusText: adapterArgs.rawResponse.statusMessage,
headers: canonicalizeHeaders(
adapterArgs.rawResponse.getHeaders() as any as Headers,
),
} as NormalizedResponse;
}

export async function nodeConvertAndSendResponse(
response: NormalizedResponse,
adapterArgs: NodeAdapterArgs,
Expand Down
3 changes: 3 additions & 0 deletions adapters/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import crypto from 'crypto';
import {
setAbstractFetchFunc,
setAbstractConvertRequestFunc,
setAbstractConvertIncomingResponseFunc,
setAbstractConvertResponseFunc,
setAbstractConvertHeadersFunc,
setAbstractRuntimeString,
Expand All @@ -12,13 +13,15 @@ import {
import {
nodeFetch,
nodeConvertRequest,
nodeConvertIncomingResponse,
nodeConvertAndSendResponse,
nodeConvertAndSetHeaders,
nodeRuntimeString,
} from './adapter';

setAbstractFetchFunc(nodeFetch);
setAbstractConvertRequestFunc(nodeConvertRequest);
setAbstractConvertIncomingResponseFunc(nodeConvertIncomingResponse);
setAbstractConvertResponseFunc(nodeConvertAndSendResponse);
setAbstractConvertHeadersFunc(nodeConvertAndSetHeaders);
setAbstractRuntimeString(nodeRuntimeString);
Expand Down
4 changes: 4 additions & 0 deletions docs/guides/runtimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ These are the functions you'll need to create:
- `setAbstractRuntimeString`
- `setCrypto`

For runtimes that pass in a response object, such as Node.js, you'll also need to create:

- `setAbstractConvertIncomingResponseFunc`

Below is a _very_ simplified example with some functions:

```ts
Expand Down
17 changes: 9 additions & 8 deletions lib/auth/oauth/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {httpClientClass} from '../../clients/http_client/http_client';
import {DataType, RequestReturn} from '../../clients/http_client/types';
import {
abstractConvertRequest,
abstractConvertIncomingResponse,
abstractConvertResponse,
abstractConvertHeaders,
AdapterResponse,
Expand Down Expand Up @@ -55,8 +56,9 @@ export function begin(config: ConfigInterface) {

const cleanShop = sanitizeShop(config)(shop, true)!;
const request = await abstractConvertRequest(adapterArgs);
const response = await abstractConvertIncomingResponse(adapterArgs);

const cookies = new Cookies(request, {} as NormalizedResponse, {
const cookies = new Cookies(request, response, {
keys: [config.apiSecretKey],
secure: true,
});
Expand All @@ -81,13 +83,12 @@ export function begin(config: ConfigInterface) {
processedQuery.putAll(query);

const redirectUrl = `https://${cleanShop}/admin/oauth/authorize${processedQuery.stringify()}`;
const response: NormalizedResponse = {
statusCode: 302,
statusText: 'Found',
headers: {
...cookies.response.headers!,
Location: redirectUrl,
},
response.statusCode = 302;
response.statusText = 'Found';
response.headers = {
...response.headers,
...cookies.response.headers!,
Location: redirectUrl,
};

log.debug(`OAuth started, redirecting to ${redirectUrl}`, {shop, isOnline});
Expand Down
11 changes: 11 additions & 0 deletions runtime/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AbstractFetchFunc,
AbstractConvertRequestFunc,
AbstractConvertIncomingResponseFunc,
AbstractConvertResponseFunc,
NormalizedResponse,
AbstractConvertHeadersFunc,
Expand Down Expand Up @@ -41,6 +42,16 @@ export function setAbstractConvertRequestFunc(
abstractConvertRequest = func;
}

// By default we just return an empty NormalizedResponse because not all adapters will need to convert an incoming response
// eslint-disable-next-line import/no-mutable-exports
export let abstractConvertIncomingResponse: AbstractConvertIncomingResponseFunc =
() => Promise.resolve({} as NormalizedResponse);
export function setAbstractConvertIncomingResponseFunc(
func: AbstractConvertIncomingResponseFunc,
) {
abstractConvertIncomingResponse = func;
}

// eslint-disable-next-line import/no-mutable-exports
export let abstractConvertResponse: AbstractConvertResponseFunc = () => {
throw new Error(
Expand Down
4 changes: 4 additions & 0 deletions runtime/http/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export type AbstractConvertRequestFunc = (
adapterArgs: AdapterArgs,
) => Promise<NormalizedRequest>;

export type AbstractConvertIncomingResponseFunc = (
adapterArgs: AdapterArgs,
) => Promise<NormalizedResponse>;

export type AbstractConvertResponseFunc = (
response: NormalizedResponse,
adapterArgs: AdapterArgs,
Expand Down

0 comments on commit efe0770

Please sign in to comment.