GithubHelp home page GithubHelp logo

isabella232 / auth-unauthenticated.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from octokit/auth-unauthenticated.js

0.0 0.0 0.0 788 KB

strategy for explicitly unauthenticated Octokit instances

License: MIT License

TypeScript 100.00%

auth-unauthenticated.js's Introduction

auth-unauthenticated.js

strategy for explicitly unauthenticated Octokit instances

@latest Build Status

@octokit/auth-unauthenticated is useful for cases when an Octokit constructor has a default authentication strategy, but you require an explicitly unauthenticated instance.

One use cases is when building a GitHub App using @octokit/auth-app and handling webhooks using @octokit/webhooks. While all webhook events provide an installation ID in its payload, in case of the installation.deleted event, the app can no longer create an installation access token, because the app's access has been revoked.

Usage

Browsers

Load @octokit/auth-unauthenticated directly from cdn.pika.dev

<script type="module">
  import { createUnauthenticatedAuth } from "https://cdn.pika.dev/@octokit/auth-unauthenticated";
</script>
Node

Install with npm install @octokit/auth-unauthenticated

const { createUnauthenticatedAuth } = require("@octokit/auth-unauthenticated");
// or: import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated";
const auth = createUnauthenticatedAuth({
  reason:
    "Handling an installation.deleted event (The app's access has been revoked)",
});
const authentication = await auth();
// {
//   type: 'unauthenticated',
//   reason: 'Handling an installation.deleted event (The app's access has been revoked)'
// }

createUnauthenticatedAuth() options

The createUnauthenticatedAuth method requires an options.reason argument which will be used when returning an error due to a lack of authentication or when logging a warning in case of a 404 error.

Examples

createUnauthenticatedAuth({
  reason:
    "Handling an installation.deleted event: The app's access has been revoked from @octokit (id: 12345)",
});

auth()

The auth() method accepts any options, but it doesn't do anything with it. That makes it a great drop-in replacement for any other authentication strategy.

Authentication object

name type description
type string "unauthenticated"

auth.hook(request, route, options) or auth.hook(request, options)

auth.hook() hooks directly into the request life cycle. If a mutating request is attempted to be sent (DELETE, PATCH, POST, or PUT), the request is failed immediately and returning an error that contains the reason passed to createUnauthenticatedAuth({ reason }).

If a request fails with a 404 or due to hitting a rate/abuse limit, the returned error is amended that it might be caused due to a lack of authentication and will include the reason passed to createUnauthenticatedAuth({ reason }).

The request option is an instance of @octokit/request. The route/options parameters are the same as for the request() method.

auth.hook() can be called directly to send an authenticated request

const { data } = await auth.hook(request, "GET /");

Or it can be passed as option to request().

const requestWithAuth = request.defaults({
  request: {
    hook: auth.hook,
  },
});

const { data } = await requestWithAuth("GET /");

License

MIT

auth-unauthenticated.js's People

Contributors

renovate[bot] avatar dependabot[bot] avatar gr2m avatar oscard0m avatar wolfy1339 avatar nickfloyd avatar christophehurpeau avatar

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.