Coder Social home page Coder Social logo

webhooks-methods.js's Introduction

webhooks-methods.js

Methods to handle GitHub Webhook requests

@latest Build Status

Table of contents

usage

Browsers

๐Ÿšง @octokit/webhooks-methods is not meant to be used in browsers. The webhook secret is a sensitive credential that must not be exposed to users.

Load @octokit/webhooks-methods directly from esm.sh

<script type="module">
  import {
    sign,
    verify,
    verifyWithFallback,
  } from "https://esm.sh/@octokit/webhooks-methods";
</script>

Node

Install with npm install @octokit/core @octokit/webhooks-methods

import { sign, verify, verifyWithFallback } from "@octokit/webhooks-methods";
await sign("mysecret", eventPayloadString);
// resolves with a string like "sha256=4864d2759938a15468b5df9ade20bf161da9b4f737ea61794142f3484236bda3"

await sign({ secret: "mysecret", algorithm: "sha1" }, eventPayloadString);
// resolves with a string like "sha1=d03207e4b030cf234e3447bac4d93add4c6643d8"

await verify("mysecret", eventPayloadString, "sha256=486d27...");
// resolves with true or false

await verifyWithFallback("mysecret", eventPayloadString, "sha256=486d27...", ["oldsecret", ...]);
// resolves with true or false

Methods

sign()

await sign(secret, eventPayloadString);
await sign({ secret, algorithm }, eventPayloadString);
secret (String) Required. Secret as configured in GitHub Settings.
algorithm (String)

Algorithm to calculate signature. Can be set to sha1 or sha256. sha1 is supported for legacy reasons. GitHub Enterprise Server 2.22 and older do not send the X-Hub-Signature-256 header. Defaults to sha256.

Learn more at Validating payloads from GitHub

eventPayloadString (String) Required. Webhook request payload as received from GitHub.

If you have only access to an already parsed object, stringify it with JSON.stringify(payload)

Resolves with a signature string. Throws an error if an argument is missing.

verify()

await verify(secret, eventPayloadString, signature);
secret (String) Required. Secret as configured in GitHub Settings.
eventPayloadString (String) Required. Webhook request payload as received from GitHub.

If you have only access to an already parsed object, stringify it with JSON.stringify(payload)
signature (String) Required. Signature string as calculated by sign().

Resolves with true or false. Throws error if an argument is missing.

verifyWithFallback()

await verifyWithFallback(
  secret,
  eventPayloadString,
  signature,
  additionalSecrets,
);
secret (String) Required. Secret as configured in GitHub Settings.
eventPayloadString (String) Required. Webhook request payload as received from GitHub.

If you have only access to an already parsed object, stringify it with JSON.stringify(payload)
signature (String) Required. Signature string as calculated by sign().
additionalSecrets (Array of String) If given, each additional secret will be tried in turn.

This is a thin wrapper around verify() that is intended to ease callers' support for key rotation. Resolves with true or false. Throws error if a required argument is missing.

Contributing

See CONTRIBUTING.md

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.