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

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaansyed committed Nov 10, 2023
1 parent 9b67fef commit bc6221a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 45 deletions.
12 changes: 10 additions & 2 deletions packages/shopify-api/lib/auth/get-embedded-app-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {sanitizeHost} from '../utils/shop-validator';
import {decodeHost} from './decode-host';
import {GetEmbeddedAppUrlParams} from './types';

export function getEmbeddedAppUrl(config: ConfigInterface) {
export type GetEmbeddedAppUrl = (
params: GetEmbeddedAppUrlParams,
) => Promise<string>;

export type BuildEmbeddedAppUrl = (host: string) => string;

export function getEmbeddedAppUrl(config: ConfigInterface): GetEmbeddedAppUrl {
return async ({...adapterArgs}: GetEmbeddedAppUrlParams): Promise<string> => {
const request = await abstractConvertRequest(adapterArgs);

Expand Down Expand Up @@ -35,7 +41,9 @@ export function getEmbeddedAppUrl(config: ConfigInterface) {
};
}

export function buildEmbeddedAppUrl(config: ConfigInterface) {
export function buildEmbeddedAppUrl(
config: ConfigInterface,
): BuildEmbeddedAppUrl {
return (host: string): string => {
sanitizeHost()(host, true);
const decodedHost = decodeHost(host);
Expand Down
25 changes: 15 additions & 10 deletions packages/shopify-api/lib/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {ConfigInterface} from '../base-types';
import {FeatureEnabled, FutureFlagOptions} from '../../future/flags';

import {begin, callback} from './oauth/oauth';
import {nonce} from './oauth/nonce';
import {safeCompare} from './oauth/safe-compare';
import {getEmbeddedAppUrl, buildEmbeddedAppUrl} from './get-embedded-app-url';
import {OAuthBegin, OAuthCallback, begin, callback} from './oauth/oauth';
import {Nonce, nonce} from './oauth/nonce';
import {SafeCompare, safeCompare} from './oauth/safe-compare';
import {
getEmbeddedAppUrl,
buildEmbeddedAppUrl,
GetEmbeddedAppUrl,
BuildEmbeddedAppUrl,
} from './get-embedded-app-url';
import {TokenExchange, tokenExchange} from './oauth/token-exchange';

export function shopifyAuth<Config extends ConfigInterface>(
Expand All @@ -27,12 +32,12 @@ export function shopifyAuth<Config extends ConfigInterface>(
}

export type ShopifyAuth<Future extends FutureFlagOptions> = {
begin: ReturnType<typeof begin>;
callback: ReturnType<typeof callback>;
nonce: typeof nonce;
safeCompare: typeof safeCompare;
getEmbeddedAppUrl: ReturnType<typeof getEmbeddedAppUrl>;
buildEmbeddedAppUrl: ReturnType<typeof buildEmbeddedAppUrl>;
begin: OAuthBegin;
callback: OAuthCallback;
nonce: Nonce;
safeCompare: SafeCompare;
getEmbeddedAppUrl: GetEmbeddedAppUrl;
buildEmbeddedAppUrl: BuildEmbeddedAppUrl;
} & (FeatureEnabled<Future, 'unstable_tokenExchange'> extends true
? {tokenExchange: TokenExchange}
: {[key: string]: never});
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,38 @@ beforeEach(() => {

describe('createSession', () => {
describe('when receiving an offline token', () => {
[{isEmbeddedApp: true}, {isEmbeddedApp: false}].forEach(
({isEmbeddedApp}) => {
test(`creates a new offline session when embedded is ${isEmbeddedApp}`, () => {
const shopify = shopifyApi(testConfig({isEmbeddedApp}));

const accessTokenResponse = {
statusCode: 200,
statusText: 'OK',
headers: {},
body: {
access_token: 'some access token string',
scope: shopify.config.scopes.toString(),
},
};

const session = createSession({
config: shopify.config,
postResponse: accessTokenResponse,
test.each([true, false])(
`creates a new offline session when embedded is %s`,
(isEmbeddedApp) => {
const shopify = shopifyApi(testConfig({isEmbeddedApp}));

const accessTokenResponse = {
statusCode: 200,
statusText: 'OK',
headers: {},
body: {
access_token: 'some access token string',
scope: shopify.config.scopes.toString(),
},
};

const session = createSession({
config: shopify.config,
postResponse: accessTokenResponse,
shop,
state: 'test-state',
});

expect(session).toEqual(
new Session({
id: `offline_${shop}`,
shop,
isOnline: false,
state: 'test-state',
});

expect(session).toEqual(
new Session({
id: `offline_${shop}`,
shop,
isOnline: false,
state: 'test-state',
accessToken: accessTokenResponse.body.access_token,
scope: accessTokenResponse.body.scope,
}),
);
});
accessToken: accessTokenResponse.body.access_token,
scope: accessTokenResponse.body.scope,
}),
);
},
);
});
Expand Down
2 changes: 2 additions & 0 deletions packages/shopify-api/lib/auth/oauth/nonce.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {crypto} from '../../../runtime/crypto';

export type Nonce = () => string;

export function nonce(): string {
const length = 15;

Expand Down
10 changes: 8 additions & 2 deletions packages/shopify-api/lib/auth/oauth/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ import {nonce} from './nonce';
import {safeCompare} from './safe-compare';
import {createSession} from './create-session';

export type OAuthBegin = (beginParams: BeginParams) => Promise<AdapterResponse>;

export interface CallbackResponse<T = AdapterHeaders> {
headers: T;
session: Session;
}

export type OAuthCallback = <T = AdapterHeaders>(
callbackParams: CallbackParams,
) => Promise<CallbackResponse<T>>;

interface BotLog {
request: NormalizedRequest;
log: ShopifyLogger;
Expand All @@ -49,7 +55,7 @@ const logForBot = ({request, log, func}: BotLog) => {
});
};

export function begin(config: ConfigInterface) {
export function begin(config: ConfigInterface): OAuthBegin {
return async ({
shop,
callbackPath,
Expand Down Expand Up @@ -113,7 +119,7 @@ export function begin(config: ConfigInterface) {
};
}

export function callback(config: ConfigInterface) {
export function callback(config: ConfigInterface): OAuthCallback {
return async function callback<T = AdapterHeaders>({
...adapterArgs
}: CallbackParams): Promise<CallbackResponse<T>> {
Expand Down
5 changes: 5 additions & 0 deletions packages/shopify-api/lib/auth/oauth/safe-compare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as ShopifyErrors from '../../error';

export type SafeCompare = (
strA: string | {[key: string]: string} | string[] | number[],
strB: string | {[key: string]: string} | string[] | number[],
) => boolean;

export function safeCompare(
strA: string | {[key: string]: string} | string[] | number[],
strB: string | {[key: string]: string} | string[] | number[],
Expand Down

0 comments on commit bc6221a

Please sign in to comment.