-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(endpoint-syndicate): support multiple methods of supplying a token
- Loading branch information
1 parent
7128915
commit 364ac14
Showing
14 changed files
with
235 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import process from "node:process"; | ||
import { IndiekitError } from "@indiekit/error"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
export const findBearerToken = (request) => { | ||
if (request.headers?.["x-webhook-signature"]) { | ||
const signature = request.headers["x-webhook-signature"]; | ||
const verifiedToken = verifyToken(signature); | ||
const bearerToken = signToken(verifiedToken, request.body.url); | ||
return bearerToken; | ||
} | ||
|
||
if (request.body?.access_token) { | ||
const bearerToken = request.body.access_token; | ||
delete request.body.access_token; | ||
return bearerToken; | ||
} | ||
|
||
if (request.query?.token) { | ||
const bearerToken = request.query.token; | ||
return bearerToken; | ||
} | ||
|
||
throw IndiekitError.invalidRequest("No bearer token provided by request"); | ||
}; | ||
|
||
/** | ||
* Generate short-lived bearer token with update scope | ||
* | ||
* @param {object} verifiedToken - JSON Web Token | ||
* @param {string} url - Publication URL | ||
* @returns {string} Signed JSON Web Token | ||
*/ | ||
export const signToken = (verifiedToken, url) => | ||
jwt.sign( | ||
{ | ||
me: url, | ||
scope: "update", | ||
}, | ||
process.env.SECRET, | ||
{ | ||
expiresIn: "10m", | ||
} | ||
); | ||
|
||
/** | ||
* Verify that token provided by signature was issued by Netlify | ||
* | ||
* @param {string} signature - JSON Web Signature | ||
* @returns {object} JSON Web Token | ||
* @see {@link https://docs.netlify.com/site-deploys/notifications/#payload-signature} | ||
*/ | ||
export const verifyToken = (signature) => | ||
jwt.verify(signature, process.env.WEBHOOK_SECRET, { | ||
algorithms: ["HS256"], | ||
issuer: ["netlify"], | ||
}); |
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
Oops, something went wrong.