{"name":"@octokit/auth-token","publishConfig":{"access":"public"},"type":"module","version":"5.0.1","description":"GitHub API token authentication for browsers and Node.js","repository":{"type":"git","url":"git+https://github.com/octokit/auth-token.js.git"},"keywords":["github","octokit","authentication","api"],"author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"license":"MIT","devDependencies":{"@octokit/request":"^9.0.0","@octokit/tsconfig":"^3.0.0","@octokit/types":"^12.0.0","@types/fetch-mock":"^7.3.8","@types/jest":"^29.0.0","esbuild":"^0.20.0","fetch-mock":"npm:@gr2m/fetch-mock@9.11.0-pull-request-644.1","glob":"^10.2.6","jest":"^29.0.0","prettier":"3.2.5","semantic-release":"^23.0.0","ts-jest":"^29.1.0","typescript":"^5.3.0"},"engines":{"node":">= 18"},"main":"./dist-bundle/index.js","types":"./dist-types/index.d.ts","exports":{".":{"types":"./dist-types/index.d.ts","import":"./dist-bundle/index.js"}},"sideEffects":false,"_id":"@octokit/auth-token@5.0.1","gitHead":"32168623cb0c862dd991e8bf33af5a5554b47899","bugs":{"url":"https://github.com/octokit/auth-token.js/issues"},"homepage":"https://github.com/octokit/auth-token.js#readme","_nodeVersion":"20.11.1","_npmVersion":"10.1.0","dist":{"integrity":"sha512-RTmWsLfig8SBoiSdgvCht4BXl1CHU89Co5xiQ5JF19my/sIRDFCQ1RPrmK0exgqUZuNm39C/bV8+/83+MJEjGg==","shasum":"1b882a54587d9e58b7aebb5ae5b9a08522c822f0","tarball":"http://registry-npm.ywwl.com/@octokit/auth-token/download/@octokit/auth-token-5.0.1.tgz","fileCount":14,"unpackedSize":18901,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCoi4j0EzDYSQGW+Y8trvta7ix8IkQBRasF2e353H0RcwIhAIPul+/cOc9A4T/iyIDqOrCW8Nx8oYCJacPZsoLxTtqd"}],"size":5904,"noattachment":false,"key":"/@octokit/auth-token/-/@octokit/auth-token-5.0.1.tgz"},"_npmUser":{"name":"octokitbot","email":"security+octokitbot@github.com"},"directories":{},"maintainers":[{"name":"kfcampbell","email":"kfcampbell@github.com"},{"name":"nickfloyd","email":"nicholas.floyd.info@gmail.com"},{"name":"gr2m","email":"gregor@martynus.net"},{"name":"octokitbot","email":"security+octokitbot@github.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/auth-token_5.0.1_1709177060318_0.8434691274642272"},"_hasShrinkwrap":false,"readme":"# auth-token.js\n\n> GitHub API token authentication for browsers and Node.js\n\n[![@latest](https://img.shields.io/npm/v/@octokit/auth-token.svg)](https://www.npmjs.com/package/@octokit/auth-token)\n[![Build Status](https://github.com/octokit/auth-token.js/workflows/Test/badge.svg)](https://github.com/octokit/auth-token.js/actions?query=workflow%3ATest)\n\n`@octokit/auth-token` is the simplest of [GitHub’s authentication strategies](https://github.com/octokit/auth.js).\n\nIt is useful if you want to support multiple authentication strategies, as it’s API is compatible with its sibling packages for [basic](https://github.com/octokit/auth-basic.js), [GitHub App](https://github.com/octokit/auth-app.js) and [OAuth app](https://github.com/octokit/auth.js) authentication.\n\n<!-- toc -->\n\n- [Usage](#usage)\n- [`createTokenAuth(token) options`](#createtokenauthtoken-options)\n- [`auth()`](#auth)\n- [Authentication object](#authentication-object)\n- [`auth.hook(request, route, options)` or `auth.hook(request, options)`](#authhookrequest-route-options-or-authhookrequest-options)\n- [Find more information](#find-more-information)\n  - [Find out what scopes are enabled for oauth tokens](#find-out-what-scopes-are-enabled-for-oauth-tokens)\n  - [Find out if token is a personal access token or if it belongs to an OAuth app](#find-out-if-token-is-a-personal-access-token-or-if-it-belongs-to-an-oauth-app)\n  - [Find out what permissions are enabled for a repository](#find-out-what-permissions-are-enabled-for-a-repository)\n  - [Use token for git operations](#use-token-for-git-operations)\n- [License](#license)\n\n<!-- tocstop -->\n\n## Usage\n\n<table>\n<tbody valign=top align=left>\n<tr><th>\nBrowsers\n</th><td width=100%>\n\nLoad `@octokit/auth-token` directly from [esm.sh](https://esm.sh)\n\n```html\n<script type=\"module\">\n  import { createTokenAuth } from \"https://esm.sh/@octokit/auth-token\";\n</script>\n```\n\n</td></tr>\n<tr><th>\nNode\n</th><td>\n\nInstall with <code>npm install @octokit/auth-token</code>\n\n```js\nimport { createTokenAuth } from \"@octokit/auth-token\";\n```\n\n</td></tr>\n</tbody>\n</table>\n\n```js\nconst auth = createTokenAuth(\"ghp_PersonalAccessToken01245678900000000\");\nconst authentication = await auth();\n// {\n//   type: 'token',\n//   token: 'ghp_PersonalAccessToken01245678900000000',\n//   tokenType: 'oauth'\n// }\n```\n\n## `createTokenAuth(token) options`\n\nThe `createTokenAuth` method accepts a single argument of type string, which is the token. The passed token can be one of the following:\n\n- [Personal access token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line)\n- [OAuth access token](https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/)\n- [GITHUB_TOKEN provided to GitHub Actions](https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#environment-variables)\n- Installation access token ([server-to-server](https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-an-installation))\n- User authentication for installation ([user-to-server](https://docs.github.com/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps))\n\nExamples\n\n```js\n// Personal access token or OAuth access token\ncreateTokenAuth(\"ghp_PersonalAccessToken01245678900000000\");\n// {\n//   type: 'token',\n//   token: 'ghp_PersonalAccessToken01245678900000000',\n//   tokenType: 'oauth'\n// }\n\n// Installation access token or GitHub Action token\ncreateTokenAuth(\"ghs_InstallallationOrActionToken00000000\");\n// {\n//   type: 'token',\n//   token: 'ghs_InstallallationOrActionToken00000000',\n//   tokenType: 'installation'\n// }\n\n// Installation access token or GitHub Action token\ncreateTokenAuth(\"ghu_InstallationUserToServer000000000000\");\n// {\n//   type: 'token',\n//   token: 'ghu_InstallationUserToServer000000000000',\n//   tokenType: 'user-to-server'\n// }\n```\n\n## `auth()`\n\nThe `auth()` method has no options. It returns a promise which resolves with the the authentication object.\n\n## Authentication object\n\n<table width=\"100%\">\n  <thead align=left>\n    <tr>\n      <th width=150>\n        name\n      </th>\n      <th width=70>\n        type\n      </th>\n      <th>\n        description\n      </th>\n    </tr>\n  </thead>\n  <tbody align=left valign=top>\n    <tr>\n      <th>\n        <code>type</code>\n      </th>\n      <th>\n        <code>string</code>\n      </th>\n      <td>\n        <code>\"token\"</code>\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>token</code>\n      </th>\n      <th>\n        <code>string</code>\n      </th>\n      <td>\n        The provided token.\n      </td>\n    </tr>\n    <tr>\n      <th>\n        <code>tokenType</code>\n      </th>\n      <th>\n        <code>string</code>\n      </th>\n      <td>\n        Can be either <code>\"oauth\"</code> for personal access tokens and OAuth tokens, <code>\"installation\"</code> for installation access tokens (includes <code>GITHUB_TOKEN</code> provided to GitHub Actions), <code>\"app\"</code> for a GitHub App JSON Web Token, or <code>\"user-to-server\"</code> for a user authentication token through an app installation.\n      </td>\n    </tr>\n  </tbody>\n</table>\n\n## `auth.hook(request, route, options)` or `auth.hook(request, options)`\n\n`auth.hook()` hooks directly into the request life cycle. It authenticates the request using the provided token.\n\nThe `request` option is an instance of [`@octokit/request`](https://github.com/octokit/request.js#readme). The `route`/`options` parameters are the same as for the [`request()` method](https://github.com/octokit/request.js#request).\n\n`auth.hook()` can be called directly to send an authenticated request\n\n```js\nconst { data: authorizations } = await auth.hook(\n  request,\n  \"GET /authorizations\",\n);\n```\n\nOr it can be passed as option to [`request()`](https://github.com/octokit/request.js#request).\n\n```js\nconst requestWithAuth = request.defaults({\n  request: {\n    hook: auth.hook,\n  },\n});\n\nconst { data: authorizations } = await requestWithAuth(\"GET /authorizations\");\n```\n\n## Find more information\n\n`auth()` does not send any requests, it only transforms the provided token string into an authentication object.\n\nHere is a list of things you can do to retrieve further information\n\n### Find out what scopes are enabled for oauth tokens\n\nNote that this does not work for installations. There is no way to retrieve permissions based on an installation access tokens.\n\n```js\nconst TOKEN = \"ghp_PersonalAccessToken01245678900000000\";\n\nconst auth = createTokenAuth(TOKEN);\nconst authentication = await auth();\n\nconst response = await request(\"HEAD /\");\nconst scopes = response.headers[\"x-oauth-scopes\"].split(/,\\s+/);\n\nif (scopes.length) {\n  console.log(\n    `\"${TOKEN}\" has ${scopes.length} scopes enabled: ${scopes.join(\", \")}`,\n  );\n} else {\n  console.log(`\"${TOKEN}\" has no scopes enabled`);\n}\n```\n\n### Find out if token is a personal access token or if it belongs to an OAuth app\n\n```js\nconst TOKEN = \"ghp_PersonalAccessToken01245678900000000\";\n\nconst auth = createTokenAuth(TOKEN);\nconst authentication = await auth();\n\nconst response = await request(\"HEAD /\");\nconst clientId = response.headers[\"x-oauth-client-id\"];\n\nif (clientId) {\n  console.log(\n    `\"${token}\" is an OAuth token, its app’s client_id is ${clientId}.`,\n  );\n} else {\n  console.log(`\"${token}\" is a personal access token`);\n}\n```\n\n### Find out what permissions are enabled for a repository\n\nNote that the `permissions` key is not set when authenticated using an installation access token.\n\n```js\nconst TOKEN = \"ghp_PersonalAccessToken01245678900000000\";\n\nconst auth = createTokenAuth(TOKEN);\nconst authentication = await auth();\n\nconst response = await request(\"GET /repos/{owner}/{repo}\", {\n  owner: \"octocat\",\n  repo: \"hello-world\",\n});\n\nconsole.log(response.data.permissions);\n// {\n//   admin: true,\n//   push: true,\n//   pull: true\n// }\n```\n\n### Use token for git operations\n\nBoth 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`](https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#http-based-git-access-by-an-installation).\n\nThis example is using the [`execa`](https://github.com/sindresorhus/execa) package to run a `git push` command.\n\n```js\nconst TOKEN = \"ghp_PersonalAccessToken01245678900000000\";\n\nconst auth = createTokenAuth(TOKEN);\nconst { token, tokenType } = await auth();\nconst tokenWithPrefix =\n  tokenType === \"installation\" ? `x-access-token:${token}` : token;\n\nconst repositoryUrl = `https://${tokenWithPrefix}@github.com/octocat/hello-world.git`;\n\nconst { stdout } = await execa(\"git\", [\"push\", repositoryUrl]);\nconsole.log(stdout);\n```\n\n## License\n\n[MIT](LICENSE)\n","publish_time":1709177060492,"_cnpm_publish_time":1709177060492,"dist-tags":{"beta":"5.0.0-beta.1","latest":"6.0.0","release-4.x":"4.0.0"}}