6.1.0 • Published 1 month ago

@octokit/auth-unauthenticated v6.1.0

Weekly downloads
8,543
License
MIT
Repository
github
Last release
1 month ago

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

Load @octokit/auth-unauthenticated directly from esm.sh

<script type="module">
  import { createUnauthenticatedAuth } from "https://esm.sh/@octokit/auth-unauthenticated";
</script>

Install with npm install @octokit/auth-unauthenticated

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

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

6.1.0

1 month ago

6.0.1

1 month ago

6.0.0-beta.1

2 months ago

6.0.0

2 months ago

5.0.1

8 months ago

5.0.0

10 months ago

3.0.5

12 months ago

4.0.1

11 months ago

4.0.0

11 months ago

3.0.4

1 year ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.5

3 years ago

2.1.0

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

1.0.1

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago