github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@octokit/auth-token/README.md (about) 1 # auth-token.js 2 3 > GitHub API token authentication for browsers and Node.js 4 5 [](https://www.npmjs.com/package/@octokit/auth-token) 6 [](https://github.com/octokit/auth-token.js/actions?query=workflow%3ATest) 7 8 `@octokit/auth-token` is the simplest of [GitHub’s authentication strategies](https://github.com/octokit/auth.js). 9 10 It 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. 11 12 <!-- toc --> 13 14 - [Usage](#usage) 15 - [`createTokenAuth(token) options`](#createtokenauthtoken-options) 16 - [`auth()`](#auth) 17 - [Authentication object](#authentication-object) 18 - [`auth.hook(request, route, options)` or `auth.hook(request, options)`](#authhookrequest-route-options-or-authhookrequest-options) 19 - [Find more information](#find-more-information) 20 - [Find out what scopes are enabled for oauth tokens](#find-out-what-scopes-are-enabled-for-oauth-tokens) 21 - [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) 22 - [Find out what permissions are enabled for a repository](#find-out-what-permissions-are-enabled-for-a-repository) 23 - [Use token for git operations](#use-token-for-git-operations) 24 - [License](#license) 25 26 <!-- tocstop --> 27 28 ## Usage 29 30 <table> 31 <tbody valign=top align=left> 32 <tr><th> 33 Browsers 34 </th><td width=100%> 35 36 Load `@octokit/auth-token` directly from [cdn.skypack.dev](https://cdn.skypack.dev) 37 38 ```html 39 <script type="module"> 40 import { createTokenAuth } from "https://cdn.skypack.dev/@octokit/auth-token"; 41 </script> 42 ``` 43 44 </td></tr> 45 <tr><th> 46 Node 47 </th><td> 48 49 Install with <code>npm install @octokit/auth-token</code> 50 51 ```js 52 const { createTokenAuth } = require("@octokit/auth-token"); 53 // or: import { createTokenAuth } from "@octokit/auth-token"; 54 ``` 55 56 </td></tr> 57 </tbody> 58 </table> 59 60 ```js 61 const auth = createTokenAuth("ghp_PersonalAccessToken01245678900000000"); 62 const authentication = await auth(); 63 // { 64 // type: 'token', 65 // token: 'ghp_PersonalAccessToken01245678900000000', 66 // tokenType: 'oauth' 67 // } 68 ``` 69 70 ## `createTokenAuth(token) options` 71 72 The `createTokenAuth` method accepts a single argument of type string, which is the token. The passed token can be one of the following: 73 74 - [Personal access token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) 75 - [OAuth access token](https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/) 76 - [GITHUB_TOKEN provided to GitHub Actions](https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#environment-variables) 77 - Installation access token ([server-to-server](https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-an-installation)) 78 - User authentication for installation ([user-to-server](https://docs.github.com/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps)) 79 80 Examples 81 82 ```js 83 // Personal access token or OAuth access token 84 createTokenAuth("ghp_PersonalAccessToken01245678900000000"); 85 // { 86 // type: 'token', 87 // token: 'ghp_PersonalAccessToken01245678900000000', 88 // tokenType: 'oauth' 89 // } 90 91 // Installation access token or GitHub Action token 92 createTokenAuth("ghs_InstallallationOrActionToken00000000"); 93 // { 94 // type: 'token', 95 // token: 'ghs_InstallallationOrActionToken00000000', 96 // tokenType: 'installation' 97 // } 98 99 // Installation access token or GitHub Action token 100 createTokenAuth("ghu_InstallationUserToServer000000000000"); 101 // { 102 // type: 'token', 103 // token: 'ghu_InstallationUserToServer000000000000', 104 // tokenType: 'user-to-server' 105 // } 106 ``` 107 108 ## `auth()` 109 110 The `auth()` method has no options. It returns a promise which resolves with the the authentication object. 111 112 ## Authentication object 113 114 <table width="100%"> 115 <thead align=left> 116 <tr> 117 <th width=150> 118 name 119 </th> 120 <th width=70> 121 type 122 </th> 123 <th> 124 description 125 </th> 126 </tr> 127 </thead> 128 <tbody align=left valign=top> 129 <tr> 130 <th> 131 <code>type</code> 132 </th> 133 <th> 134 <code>string</code> 135 </th> 136 <td> 137 <code>"token"</code> 138 </td> 139 </tr> 140 <tr> 141 <th> 142 <code>token</code> 143 </th> 144 <th> 145 <code>string</code> 146 </th> 147 <td> 148 The provided token. 149 </td> 150 </tr> 151 <tr> 152 <th> 153 <code>tokenType</code> 154 </th> 155 <th> 156 <code>string</code> 157 </th> 158 <td> 159 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. 160 </td> 161 </tr> 162 </tbody> 163 </table> 164 165 ## `auth.hook(request, route, options)` or `auth.hook(request, options)` 166 167 `auth.hook()` hooks directly into the request life cycle. It authenticates the request using the provided token. 168 169 The `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). 170 171 `auth.hook()` can be called directly to send an authenticated request 172 173 ```js 174 const { data: authorizations } = await auth.hook( 175 request, 176 "GET /authorizations" 177 ); 178 ``` 179 180 Or it can be passed as option to [`request()`](https://github.com/octokit/request.js#request). 181 182 ```js 183 const requestWithAuth = request.defaults({ 184 request: { 185 hook: auth.hook, 186 }, 187 }); 188 189 const { data: authorizations } = await requestWithAuth("GET /authorizations"); 190 ``` 191 192 ## Find more information 193 194 `auth()` does not send any requests, it only transforms the provided token string into an authentication object. 195 196 Here is a list of things you can do to retrieve further information 197 198 ### Find out what scopes are enabled for oauth tokens 199 200 Note that this does not work for installations. There is no way to retrieve permissions based on an installation access tokens. 201 202 ```js 203 const TOKEN = "ghp_PersonalAccessToken01245678900000000"; 204 205 const auth = createTokenAuth(TOKEN); 206 const authentication = await auth(); 207 208 const response = await request("HEAD /", { 209 headers: authentication.headers, 210 }); 211 const scopes = response.headers["x-oauth-scopes"].split(/,\s+/); 212 213 if (scopes.length) { 214 console.log( 215 `"${TOKEN}" has ${scopes.length} scopes enabled: ${scopes.join(", ")}` 216 ); 217 } else { 218 console.log(`"${TOKEN}" has no scopes enabled`); 219 } 220 ``` 221 222 ### Find out if token is a personal access token or if it belongs to an OAuth app 223 224 ```js 225 const TOKEN = "ghp_PersonalAccessToken01245678900000000"; 226 227 const auth = createTokenAuth(TOKEN); 228 const authentication = await auth(); 229 230 const response = await request("HEAD /", { 231 headers: authentication.headers, 232 }); 233 const clientId = response.headers["x-oauth-client-id"]; 234 235 if (clientId) { 236 console.log( 237 `"${token}" is an OAuth token, its app’s client_id is ${clientId}.` 238 ); 239 } else { 240 console.log(`"${token}" is a personal access token`); 241 } 242 ``` 243 244 ### Find out what permissions are enabled for a repository 245 246 Note that the `permissions` key is not set when authenticated using an installation access token. 247 248 ```js 249 const TOKEN = "ghp_PersonalAccessToken01245678900000000"; 250 251 const auth = createTokenAuth(TOKEN); 252 const authentication = await auth(); 253 254 const response = await request("GET /repos/{owner}/{repo}", { 255 owner: 'octocat', 256 repo: 'hello-world' 257 headers: authentication.headers 258 }); 259 260 console.log(response.data.permissions) 261 // { 262 // admin: true, 263 // push: true, 264 // pull: true 265 // } 266 ``` 267 268 ### Use token for git operations 269 270 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`](https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#http-based-git-access-by-an-installation). 271 272 This example is using the [`execa`](https://github.com/sindresorhus/execa) package to run a `git push` command. 273 274 ```js 275 const TOKEN = "ghp_PersonalAccessToken01245678900000000"; 276 277 const auth = createTokenAuth(TOKEN); 278 const { token, tokenType } = await auth(); 279 const tokenWithPrefix = 280 tokenType === "installation" ? `x-access-token:${token}` : token; 281 282 const repositoryUrl = `https://${tokenWithPrefix}@github.com/octocat/hello-world.git`; 283 284 const { stdout } = await execa("git", ["push", repositoryUrl]); 285 console.log(stdout); 286 ``` 287 288 ## License 289 290 [MIT](LICENSE)