-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
1 parent
cb57705
commit e595b2a
Showing
4 changed files
with
51 additions
and
23 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
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,41 @@ | ||
import { isObject } from '@klasa/utils'; | ||
import { ApiRequest } from '@lib/structures/api/ApiRequest'; | ||
import { ApiResponse } from '@lib/structures/api/ApiResponse'; | ||
import { createFunctionInhibitor } from '@skyra/decorators'; | ||
import { createHmac } from 'crypto'; | ||
|
||
export function hubSignature(secret: string) { | ||
return createFunctionInhibitor( | ||
(request: ApiRequest, response: ApiResponse) => { | ||
if (!isObject(request.body)) { | ||
response.badRequest('Malformed data received'); | ||
return false; | ||
} | ||
|
||
const xHubSignature = request.headers['x-hub-signature']; | ||
if (typeof xHubSignature === 'undefined') { | ||
response.badRequest('Missing "x-hub-signature" header'); | ||
return false; | ||
} | ||
|
||
const [algo, sig] = xHubSignature.toString().split('=', 2); | ||
if (!checkSig(algo, sig, secret, request.body)) { | ||
response.forbidden('Invalid Hub signature'); | ||
return false; | ||
} | ||
|
||
return true; | ||
}, | ||
(_request: ApiRequest, response: ApiResponse) => { | ||
response.badRequest('x-hub-signature'); | ||
} | ||
); | ||
} | ||
|
||
export function checkSig(algorithm: string, signature: string, secret: string, data: any) { | ||
const hash = createHmac(algorithm, secret) | ||
.update(JSON.stringify(data)) | ||
.digest('hex'); | ||
|
||
return hash === signature; | ||
} |
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