Package Exports
- @octokit/auth
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@octokit/auth) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
auth.js
GitHub API authentication library for browsers and Node.js
GitHub supports 4 authentication strategies. They are all implemented in @octokit/auth.
Example usage
| Browsers |
Load <script type="module">
import {
createAppAuth,
createOAuthAppAuth,
createTokenAuth,
} from "https://cdn.skypack.dev/@octokit/auth";
</script> |
|---|---|
| Node |
Install with const {
createAppAuth,
createOAuthAppAuth,
createTokenAuth,
createActionAuth,
} = require("@octokit/auth");
// or:
// import {
// createAppAuth,
// createOAuthAppAuth,
// createTokenAuth,
// createActionAuth
// } from "@octokit/auth"; |
const auth = createAppAuth({
appId: 12345,
privateKey: "...",
});Each function exported by @octokit/auth returns an async auth function.
The auth function resolves with an authentication object. If multiple authentication types are supported, a type parameter can be passed.
const { token } = await auth({ type: "app" });Additionally, auth.hook() can be used to directly hook into @octokit/request. If multiple authentication types are supported, the right authentication type will be applied automatically based on the request URL.
const requestWithAuth = request.defaults({
request: {
hook: auth.hook,
},
});
const { data: authorizations } = await requestWithAuth("GET /authorizations");Official Strategies
Comparison
| Module | Strategy Options | Auth Options | Authentication objects | ||
|---|---|---|---|---|---|
|
|
|
|
| ||
|
|
|
|
|
| |
|
|
|
|
|
| |
|
|
|
|
| ||
Token authentication
Example
const auth = createTokenAuth("1234567890abcdef1234567890abcdef12345678");
const { token, tokenType } = await auth();See @octokit/auth-token for more details.
GitHub App or installation authentication
Example
const auth = createAppAuth({
appId: 1,
privateKey: "-----BEGIN RSA PRIVATE KEY-----\n...",
});
const appAuthentication = await auth({ type: "auth" });
const installationAuthentication = await auth({
type: "installation",
installationId: 123,
});See @octokit/auth-app for more details.
OAuth app and OAuth access token authentication
Example
const auth = createOAuthAppAuth({
clientId: "1234567890abcdef1234",
clientSecret: "1234567890abcdef1234567890abcdef12345678",
code: "random123", // code from OAuth web flow, see https://git.io/fhd1D
});
const appAuthentication = await auth({
type: "oauth-app",
url: "/orgs/{org}/repos",
});
const tokenAuthentication = await auth({ type: "token" });See @octokit/auth-oauth-app for more details.
GitHub Action authentication
Example
// expects process.env.GITHUB_ACTION and process.env.GITHUB_TOKEN to be set
const auth = createActionAuth();
const { token } = await auth();See @octokit/auth-action for more details.
Community Strategies
.netrc authentication
Similar to token authentication, but reads the token from your ~/.netrc file
Example
// expects a personal access token to be set as `login` in the `~/.netrc` file for `api.github.com`
const { createNetrcAuth } = require("octokit-netrc-auth");
const auth = createNetrcAuth();
const { token } = await auth();See octokit-auth-netrc for more details.