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 @octokit/auth directly from cdn.pika.dev
<script type="module">
import {
createBasicAuth,
createAppAuth,
createOAuthAppAuth,
createTokenAuth,
createActionAuth
} from "https://cdn.pika.dev/@octokit/auth";
</script>
|
---|
Node
|
Install with npm install @octokit/auth
const {
createBasicAuth,
createAppAuth,
createOAuthAppAuth,
createTokenAuth,
createActionAuth
} = require("@octokit/auth");
|
---|
const auth = createBasicAuth({
username: "monatheoctocat",
password: "secret",
on2fa() {
return prompt("Two-factor authentication Code:");
}
});
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: "token" });
Additionally, auth.hook()
can be used to directly hook into @octokit/request
.
const requestWithAuth = request.defaults({
request: {
hook: auth.hook
}
});
const { data: authorizations } = await requestWithAuth("GET /authorizations");
Basic and personal access token authentication
Example
const auth = createBasicAuth({
username: "octocat",
password: "secret",
async on2Fa() {
return prompt("Two-factor authentication Code:");
}
});
const { token } = await auth();
const { totp } = await auth({
type: "basic"
});
See @octokit/auth-basic
for more details.
GitHub App or installation authentication
Example
const auth = createAppAuth({
id: 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"
});
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.
Token authentication
Example
const auth = createTokenAuth("1234567890abcdef1234567890abcdef12345678");
const { token, tokenType } = await auth();
See @octokit/auth-token for more details.
GitHub Action authentication
Example
const auth = createActionAuth();
const { token } = await auth();
See @octokit/auth-action for more details.
License
MIT