-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Yuri-Tiofilo/feature/add-key-and-token-gif…
…tard-protocol feature: add auth key and token in provider
- Loading branch information
Showing
3 changed files
with
77 additions
and
11 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
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,49 @@ | ||
export async function validateCredentials(ctx: Context, next: () => Promise<any>) { | ||
if (!ctx.headers['x-provider-api-appkey'] || !ctx.headers['x-provider-api-apptoken']) { | ||
ctx.status = 401 | ||
ctx.body = { | ||
message: 'Unauthorized request', | ||
status: 401 | ||
} | ||
return | ||
} | ||
|
||
let appSettings: Record<string, unknown>; | ||
|
||
try { | ||
const appId = process.env.VTEX_APP_ID || ctx.vtex | ||
|
||
appSettings = await ctx.clients.apps.getAppSettings(appId) | ||
|
||
if (!appSettings) { | ||
ctx.status = 400; | ||
ctx.body = { | ||
message: 'Empty app settings giftcard protocol configuration', | ||
status: 400 | ||
} | ||
return | ||
} | ||
} catch (error) { | ||
ctx.status = 500; | ||
ctx.body = { | ||
message: 'error loading app settings', | ||
status: 500 | ||
} | ||
return | ||
} | ||
|
||
const { giftCardHubAppKey, giftCardHubAppToken } = appSettings | ||
|
||
if (ctx.headers['x-provider-api-appkey'] !== giftCardHubAppKey || | ||
ctx.headers['x-provider-api-apptoken'] !== giftCardHubAppToken | ||
) { | ||
ctx.status = 403 | ||
ctx.body = { | ||
message: 'Forbidden request', | ||
status: 403 | ||
} | ||
return | ||
} | ||
|
||
return await next() | ||
} |
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