
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
@octokit/auth-token
Advanced tools
Octokit library implementing the token authentication strategy for browsers and Node.js
The @octokit/auth-token package is designed to simplify the process of authenticating GitHub API requests. It provides a straightforward way to create an authentication token that can be used with Octokit clients or any other HTTP client to interact with the GitHub API securely.
Creating an authentication token
This feature allows you to create an authentication token using a personal access token. The created auth object can then be used to authenticate GitHub API requests.
const { createTokenAuth } = require('@octokit/auth-token');
const auth = createTokenAuth('personal-access-token123');
auth().then((authentication) => {
console.log(authentication);
});
Similar to @octokit/auth-token, @octokit/auth-app focuses on authenticating GitHub Apps instead of personal access tokens. It provides more complex authentication mechanisms required for GitHub Apps, including handling JWT and installation access tokens.
This package offers basic authentication for GitHub using a username and password combination. It's simpler and less secure compared to @octokit/auth-token, which uses tokens for authentication. @octokit/auth-basic is primarily used for legacy projects or in scenarios where token-based authentication cannot be used.
The @octokit/auth-oauth-app package is designed for OAuth-based authentication for GitHub applications. It differs from @octokit/auth-token by providing a complete OAuth flow, including redirecting users to GitHub for authentication and handling the OAuth tokens, making it suitable for web applications that need to authenticate users via GitHub.
Octokit library implementing the token authentication strategy for browsers and Node.js
@octokit/auth-token
is the simplest of GitHub’s authentication strategies.
A string is passed to the createTokenAuth
function which returns the async auth
function.
The auth
function validates the passed token and resolves with the correct authorization
header.
import { createTokenAuth } from "@octokit/auth-token";
import { request } from "@octokit/request";
(async () => {
const auth = createTokenAuth("1234567890abcdef1234567890abcdef12345678");
const authentication = await auth();
// {
// type: 'token',
// token: '1234567890abcdef1234567890abcdef12345678',
// tokenType: 'oauth',
// headers: {
// authorization: 'token 1234567890abcdef1234567890abcdef12345678'
// }
// }
// `authentication.headers` can be directly passed to a request
const result = await request("GET /orgs/:org/repos", {
headers: authentication.headers,
org: "octokit",
type: "private"
});
})();
createTokenAuth(token)
The createTokenAuth
method accepts a single argument of type string, which is the token. The passed token can be one of the following:
Examples
// Personal/OAuth access token
createTokenAuth("1234567890abcdef1234567890abcdef12345678");
// Installation access token or GitHub Action token
createTokenAuth("v1.d3d433526f780fbcc3129004e2731b3904ad0b86");
It returns the asynchronous auth()
method.
auth()
The auth()
method has no options. It returns the authentication object.
name | type | description |
---|---|---|
type
|
string
|
"token"
|
token
|
string
| The provided token. |
tokenType
|
string
|
"oauth" for personal access tokens and OAuth tokens, or "installation" for installation access tokens
|
headers
|
object
|
{ authorization } - value for the "Authorization" header.
|
query
|
object
|
{} - not used
|
createTokenAuth
does not send any requests, it only transforms the provided token string into an authentication object.
Here is a list of things you can do to retrieve further information
Note that this does not work for installations. There is no way to retrieve permissions based on an installation access tokens.
import { createTokenAuth } from "@octokit/auth-token";
import { request } from "@octokit/request";
const TOKEN = "1234567890abcdef1234567890abcdef12345678";
(async () => {
const auth = createTokenAuth(TOKEN);
const authentication = await auth();
const response = await request("HEAD /", {
headers: authentication.headers
});
const scopes = response.headers["x-oauth-scopes"].split(/,\s+/);
if (scopes.length) {
console.log(
`"${TOKEN}" has ${scopes.length} scopes enabled: ${scopes.join(", ")}`
);
} else {
console.log(`"${TOKEN}" has no scopes enabled`);
}
})();
import { createTokenAuth } from "@octokit/auth-token";
import { request } from "@octokit/request";
const TOKEN = "1234567890abcdef1234567890abcdef12345678";
(async () => {
const auth = createTokenAuth(TOKEN);
const authentication = await auth();
const response = await request("HEAD /", {
headers: authentication.headers
});
const clientId = response.headers["x-oauth-client-id"];
if (clientId) {
console.log(
`"${token}" is an OAuth token, its app’s client_id is ${clientId}.`
);
} else {
console.log(`"${token}" is a personal access token`);
}
})();
Note that the permissions
key is not set when authenticated using an installation access token.
import { createTokenAuth } from "@octokit/auth-token";
import { request } from "@octokit/request";
const TOKEN = "1234567890abcdef1234567890abcdef12345678";
(async () => {
const auth = createTokenAuth(TOKEN);
const authentication = await auth();
const response = await request("GET /repos/:owner/:repo", {
owner: 'octocat',
repo: 'hello-world'
headers: authentication.headers
});
console.log(response.data.permissions)
// {
// admin: true,
// push: true,
// pull: true
// }
})();
Both OAuth and installation access tokens can be used for git operations. However when using with an installation, the token must be prefixed with x-access-token
.
import { createTokenAuth } from "@octokit/auth-token";
import { request } from "execa";
const TOKEN = "1234567890abcdef1234567890abcdef12345678";
(async () => {
const auth = createTokenAuth(TOKEN);
const { token, tokenType } = await auth();
const tokenWithPrefix =
tokenType === "installation" ? `x-access-token:${token}` : token;
const repositoryUrl = `https://${tokenWithPrefix}@github.com/octocat/hello-world.git`;
const { stdout } = await execa("git", ["push", repositoryUrl]);
console.log(stdout);
})();
FAQs
GitHub API token authentication for browsers and Node.js
The npm package @octokit/auth-token receives a total of 8,512,854 weekly downloads. As such, @octokit/auth-token popularity was classified as popular.
We found that @octokit/auth-token demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.