-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support client credentials in shopify-api-js
- Loading branch information
1 parent
fcd67ac
commit c16c2cd
Showing
8 changed files
with
354 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/shopify-api': minor | ||
--- | ||
|
||
Introduces Client credentials token acquisition flow to `shopify-api-js` library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/apps/shopify-api/lib/auth/oauth/__tests__/client-credentials.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import {shopifyApi} from '../../..'; | ||
import {testConfig} from '../../../__tests__/test-config'; | ||
import {queueMockResponse} from '../../../__tests__/test-helper'; | ||
import * as ShopifyErrors from '../../../error'; | ||
import {DataType} from '../../../clients/types'; | ||
|
||
describe('clientCredentials', () => { | ||
const shop = 'test-shop.myshopify.io'; | ||
|
||
describe('with valid parameters', () => { | ||
test('returns a session on success', async () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const successResponse = { | ||
access_token: 'some_access_token', | ||
scope: 'write_products,read_orders', | ||
expires_in: 3600, | ||
}; | ||
|
||
const expectedExpiration = new Date( | ||
Date.now() + successResponse.expires_in * 1000, | ||
).getTime(); | ||
|
||
queueMockResponse(JSON.stringify(successResponse)); | ||
|
||
const response = await shopify.auth.clientCredentials({ | ||
shop, | ||
}); | ||
|
||
// Verify the request was made with correct parameters | ||
expect({ | ||
method: 'POST', | ||
domain: shop, | ||
path: '/admin/oauth/access_token', | ||
headers: { | ||
'Content-Type': DataType.JSON, | ||
Accept: DataType.JSON, | ||
}, | ||
data: { | ||
client_id: shopify.config.apiKey, | ||
client_secret: shopify.config.apiSecretKey, | ||
grant_type: 'client_credentials', | ||
}, | ||
}).toMatchMadeHttpRequest(); | ||
|
||
// Verify the response contains expected session data | ||
expect(response.session).toEqual( | ||
expect.objectContaining({ | ||
accessToken: successResponse.access_token, | ||
scope: successResponse.scope, | ||
}), | ||
); | ||
|
||
expect(response.session?.expires?.getTime()).toBeWithinSecondsOf( | ||
expectedExpiration, | ||
1, | ||
); | ||
}); | ||
|
||
test('throws error when response is not successful', async () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const errorResponse = { | ||
error: 'invalid_client', | ||
error_description: 'Client authentication failed', | ||
}; | ||
|
||
queueMockResponse(JSON.stringify(errorResponse), { | ||
statusCode: 400, | ||
statusText: 'Bad request', | ||
}); | ||
|
||
await expect( | ||
shopify.auth.clientCredentials({ | ||
shop, | ||
}), | ||
).rejects.toThrow(ShopifyErrors.HttpResponseError); | ||
}); | ||
}); | ||
|
||
describe('with invalid parameters', () => { | ||
test('throws error for invalid shop domain', async () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const invalidShop = 'invalid-shop-url'; | ||
|
||
await expect( | ||
shopify.auth.clientCredentials({ | ||
shop: invalidShop, | ||
}), | ||
).rejects.toThrow(ShopifyErrors.InvalidShopError); | ||
}); | ||
|
||
test('throws error for non-myshopify domain', async () => { | ||
const shopify = shopifyApi(testConfig()); | ||
const invalidShop = 'test-shop.something.com'; | ||
|
||
await expect( | ||
shopify.auth.clientCredentials({ | ||
shop: invalidShop, | ||
}), | ||
).rejects.toThrow(ShopifyErrors.InvalidShopError); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/apps/shopify-api/lib/auth/oauth/client-credentials.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import {ConfigInterface} from '../../base-types'; | ||
import {throwFailedRequest} from '../../clients/common'; | ||
import {DataType} from '../../clients/types'; | ||
import {Session} from '../../session/session'; | ||
import {fetchRequestFactory} from '../../utils/fetch-request'; | ||
import {sanitizeShop} from '../../utils/shop-validator'; | ||
|
||
import {createSession} from './create-session'; | ||
import {AccessTokenResponse} from './types'; | ||
|
||
export interface ClientCredentialsParams { | ||
shop: string; | ||
} | ||
|
||
const ClientCredentialsGrantType = 'client_credentials'; | ||
|
||
export type ClientCredentials = ( | ||
params: ClientCredentialsParams, | ||
) => Promise<{session: Session}>; | ||
|
||
export function clientCredentials(config: ConfigInterface): ClientCredentials { | ||
return async ({shop}: ClientCredentialsParams) => { | ||
const cleanShop = sanitizeShop(config)(shop, true); | ||
if (!cleanShop) { | ||
throw new Error('Invalid shop domain'); | ||
} | ||
|
||
const requestConfig = { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
client_id: config.apiKey, | ||
client_secret: config.apiSecretKey, | ||
grant_type: ClientCredentialsGrantType, | ||
}), | ||
headers: { | ||
'Content-Type': DataType.JSON, | ||
Accept: DataType.JSON, | ||
}, | ||
}; | ||
|
||
const postResponse = await fetchRequestFactory(config)( | ||
`https://${cleanShop}/admin/oauth/access_token`, | ||
requestConfig, | ||
); | ||
|
||
const responseData = (await postResponse.json()) as AccessTokenResponse; | ||
|
||
if (!postResponse.ok) { | ||
throwFailedRequest(responseData, false, postResponse); | ||
} | ||
|
||
return { | ||
session: createSession({ | ||
accessTokenResponse: responseData, | ||
shop: cleanShop, | ||
// We need to keep this as an empty string as our template DB schemas have this required | ||
state: '', | ||
config, | ||
}), | ||
}; | ||
}; | ||
} |
Oops, something went wrong.