This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
224 additions
and
3 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,7 @@ | ||
--- | ||
"@shopify/shopify-api": minor | ||
--- | ||
|
||
Added support for validating Flow extension requests, using `shopify.authenticate.flow`. | ||
|
||
Please see [the `flow` object documentation](./docs/reference/flow/README.md) for more information. |
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,9 @@ | ||
# shopify.flow | ||
|
||
This object contains functions used to authenticate Flow extension requests coming from Shopify. | ||
|
||
| Property | Description | | ||
| ------------------------- | ------------------------------------------------------------------- | | ||
| [validate](./validate.md) | Verify whether a request is a valid Shopify Flow extension request. | | ||
|
||
[Back to shopifyApi](../shopifyApi.md) |
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,64 @@ | ||
# shopify.flow.validate | ||
|
||
Takes in a raw request and the raw body for that request, and validates that it's a legitimate Shopify Flow extension request. | ||
|
||
## Example | ||
|
||
```ts | ||
app.post('/flow', express.text({type: '*/*'}), async (req, res) => { | ||
const {valid, topic, domain} = await shopify.flow.validate({ | ||
rawBody: req.body, // is a string | ||
rawRequest: req, | ||
rawResponse: res, | ||
}); | ||
|
||
if (!result.valid) { | ||
console.log(`Received invalid Flow extension request: ${result.reason}`); | ||
res.send(400); | ||
} | ||
|
||
res.send(200); | ||
}); | ||
``` | ||
|
||
## Parameters | ||
|
||
Receives an object containing: | ||
|
||
### rawBody | ||
|
||
`string` | :exclamation: required | ||
|
||
The raw body of the request received by the app. | ||
|
||
### rawRequest | ||
|
||
`AdapterRequest` | :exclamation: required | ||
|
||
The HTTP Request object used by your runtime. | ||
|
||
### rawResponse | ||
|
||
`AdapterResponse` | :exclamation: required for Node.js | ||
|
||
The HTTP Response object used by your runtime. Required for Node.js. | ||
|
||
## Return | ||
|
||
Returns an object containing: | ||
|
||
### valid | ||
|
||
`boolean` | ||
|
||
Whether the request is a valid Flow extension request from Shopify. | ||
|
||
### If valid is `false`: | ||
|
||
#### reason | ||
|
||
`FlowValidationErrorReason` | ||
|
||
The reason why the check was considered invalid. | ||
|
||
[Back to shopify.flow](./README.md) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import {shopifyApi} from '../..'; | ||
import {ShopifyHeader} from '../../types'; | ||
import { | ||
createSHA256HMAC, | ||
HashFormat, | ||
type NormalizedRequest, | ||
} from '../../../runtime'; | ||
import {testConfig} from '../../__tests__/test-config'; | ||
import {FlowValidationErrorReason} from '../types'; | ||
|
||
describe('flow', () => { | ||
describe('validate', () => { | ||
describe('failure cases', () => { | ||
it('fails if the HMAC header is missing', async () => { | ||
// GIVEN | ||
const shopify = shopifyApi(testConfig()); | ||
|
||
const payload = {field: 'value'}; | ||
const req: NormalizedRequest = { | ||
method: 'GET', | ||
url: 'https://my-app.my-domain.io', | ||
headers: {}, | ||
}; | ||
|
||
// WHEN | ||
const result = await shopify.flow.validate({ | ||
rawBody: JSON.stringify(payload), | ||
rawRequest: req, | ||
}); | ||
|
||
// THEN | ||
expect(result).toMatchObject({ | ||
valid: false, | ||
reason: FlowValidationErrorReason.MissingHmac, | ||
}); | ||
}); | ||
|
||
it('fails if the HMAC header is invalid', async () => { | ||
// GIVEN | ||
const shopify = shopifyApi(testConfig()); | ||
|
||
const payload = {field: 'value'}; | ||
const req: NormalizedRequest = { | ||
method: 'GET', | ||
url: 'https://my-app.my-domain.io', | ||
headers: {[ShopifyHeader.Hmac]: 'invalid'}, | ||
}; | ||
|
||
// WHEN | ||
const result = await shopify.flow.validate({ | ||
rawBody: JSON.stringify(payload), | ||
rawRequest: req, | ||
}); | ||
|
||
// THEN | ||
expect(result).toMatchObject({ | ||
valid: false, | ||
reason: FlowValidationErrorReason.InvalidHmac, | ||
}); | ||
}); | ||
|
||
it('fails if the body is empty', async () => { | ||
// GIVEN | ||
const shopify = shopifyApi(testConfig()); | ||
|
||
const req: NormalizedRequest = { | ||
method: 'GET', | ||
url: 'https://my-app.my-domain.io', | ||
headers: { | ||
[ShopifyHeader.Hmac]: await createSHA256HMAC( | ||
shopify.config.apiSecretKey, | ||
'', | ||
HashFormat.Base64, | ||
), | ||
}, | ||
}; | ||
|
||
// WHEN | ||
const result = await shopify.flow.validate({ | ||
rawBody: '', | ||
rawRequest: req, | ||
}); | ||
|
||
// THEN | ||
expect(result).toMatchObject({ | ||
valid: false, | ||
reason: FlowValidationErrorReason.MissingBody, | ||
}); | ||
}); | ||
}); | ||
|
||
it('succeeds if the body and HMAC header are correct', async () => { | ||
// GIVEN | ||
const shopify = shopifyApi(testConfig()); | ||
|
||
const payload = {field: 'value'}; | ||
const req: NormalizedRequest = { | ||
method: 'GET', | ||
url: 'https://my-app.my-domain.io', | ||
headers: { | ||
[ShopifyHeader.Hmac]: await createSHA256HMAC( | ||
shopify.config.apiSecretKey, | ||
JSON.stringify(payload), | ||
HashFormat.Base64, | ||
), | ||
}, | ||
}; | ||
|
||
// WHEN | ||
const result = await shopify.flow.validate({ | ||
rawBody: JSON.stringify(payload), | ||
rawRequest: req, | ||
}); | ||
|
||
// THEN | ||
expect(result).toMatchObject({valid: true}); | ||
}); | ||
}); | ||
}); |
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
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