npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@octokit-next/auth-token

v2.7.1

Published

Core SDK module for upcoming Octokit

Downloads

263

Readme

auth-token.js

GitHub API token authentication for browsers and Node.js

@latest Build Status

@octokit-next/auth-token is the simplest of GitHub’s authentication strategies.

It is useful if you want to support multiple authentication strategies, as it’s API is compatible with its sibling packages for basic, GitHub App and OAuth app authentication.

Usage

<script type="module">
import { endpoint } from "https://cdn.skypack.dev/@octokit-next/auth-token";
</script>

Install with npm install @octokit-next/auth-token

import { endpoint } from "@octokit-next/auth-token";

Load @octokit-next/auth-token directly from cdn.skypack.dev, including types.

import { endpoint } from "https://cdn.skypack.dev/@octokit-next/auth-token?dts";
const auth = createTokenAuth("ghp_PersonalAccessToken01245678900000000");
const authentication = await auth();
// {
//   type: 'token',
//   token: 'ghp_PersonalAccessToken01245678900000000',
//   tokenType: 'oauth'
// }

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 access token or OAuth access token
createTokenAuth("ghp_PersonalAccessToken01245678900000000");
// {
//   type: 'token',
//   token: 'ghp_PersonalAccessToken01245678900000000',
//   tokenType: 'oauth'
// }

// Installation access token or GitHub Action token
createTokenAuth("ghs_InstallallationOrActionToken00000000");
// {
//   type: 'token',
//   token: 'ghs_InstallallationOrActionToken00000000',
//   tokenType: 'installation'
// }

// Installation access token or GitHub Action token
createTokenAuth("ghu_InstallationUserToServer000000000000");
// {
//   type: 'token',
//   token: 'ghu_InstallationUserToServer000000000000',
//   tokenType: 'user-to-server'
// }

auth()

The auth() method has no options. It returns a promise which resolves with the the authentication object.

Authentication object

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

auth.hook() hooks directly into the request life cycle. It authenticates the request using the provided token.

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: authorizations } = await auth.hook(
  request,
  "GET /authorizations"
);

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

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

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

Find more information

auth() 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

Find out what scopes are enabled for oauth tokens

Note that this does not work for installations. There is no way to retrieve permissions based on an installation access tokens.

const TOKEN = "ghp_PersonalAccessToken01245678900000000";

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`);
}

Find out if token is a personal access token or if it belongs to an OAuth app

const TOKEN = "ghp_PersonalAccessToken01245678900000000";

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`);
}

Find out what permissions are enabled for a repository

Note that the permissions key is not set when authenticated using an installation access token.

const TOKEN = "ghp_PersonalAccessToken01245678900000000";

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
// }

Use token for git operations

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.

This example is using the execa package to run a git push command.

const TOKEN = "ghp_PersonalAccessToken01245678900000000";

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);

License

MIT