@octokit/auth-callback
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -6,3 +6,11 @@ 'use strict'; | ||
async function auth(callback) { | ||
const token = (await callback()).replace(/^(token|bearer) +/i, ""); | ||
const result = await callback(); | ||
if (!result) { | ||
return { | ||
type: "unauthenticated" | ||
}; | ||
} | ||
const token = result.replace(/^(token|bearer) +/i, ""); | ||
const tokenType = token.split(/\./).length === 3 ? "app" : /^v\d+\./.test(token) ? "installation" : "oauth"; | ||
@@ -31,3 +39,9 @@ return { | ||
const endpoint = request.endpoint.merge(route, parameters); | ||
const token = (await callback()).replace(/^(token|bearer) +/i, ""); | ||
const result = await callback(); | ||
if (!result) { | ||
return request(endpoint); | ||
} | ||
const token = result.replace(/^(token|bearer) +/i, ""); | ||
endpoint.headers.authorization = withAuthorizationPrefix(token); | ||
@@ -34,0 +48,0 @@ return request(endpoint); |
export async function auth(callback) { | ||
const token = (await callback()).replace(/^(token|bearer) +/i, ""); | ||
const result = await callback(); | ||
if (!result) { | ||
return { | ||
type: "unauthenticated", | ||
}; | ||
} | ||
const token = result.replace(/^(token|bearer) +/i, ""); | ||
const tokenType = token.split(/\./).length === 3 | ||
@@ -4,0 +10,0 @@ ? "app" |
import { withAuthorizationPrefix } from "./with-authorization-prefix"; | ||
export async function hook(callback, request, route, parameters) { | ||
const endpoint = request.endpoint.merge(route, parameters); | ||
const token = (await callback()).replace(/^(token|bearer) +/i, ""); | ||
const result = await callback(); | ||
if (!result) { | ||
return request(endpoint); | ||
} | ||
const token = result.replace(/^(token|bearer) +/i, ""); | ||
endpoint.headers.authorization = withAuthorizationPrefix(token); | ||
return request(endpoint); | ||
} |
@@ -13,3 +13,6 @@ import * as OctokitTypes from "@octokit/types"; | ||
export declare type Token = string; | ||
export declare type Callback = () => Token | Promise<Token>; | ||
export declare type Callback = () => Token | undefined | Promise<Token | undefined>; | ||
export declare type UnauthenticatedAuthentication = { | ||
type: "unauthenticated"; | ||
}; | ||
export declare type OAuthTokenAuthentication = { | ||
@@ -30,2 +33,2 @@ type: "token"; | ||
}; | ||
export declare type Authentication = OAuthTokenAuthentication | InstallationTokenAuthentication | AppAuthentication; | ||
export declare type Authentication = UnauthenticatedAuthentication | OAuthTokenAuthentication | InstallationTokenAuthentication | AppAuthentication; |
async function auth(callback) { | ||
const token = (await callback()).replace(/^(token|bearer) +/i, ""); | ||
const result = await callback(); | ||
if (!result) { | ||
return { | ||
type: "unauthenticated", | ||
}; | ||
} | ||
const token = result.replace(/^(token|bearer) +/i, ""); | ||
const tokenType = token.split(/\./).length === 3 | ||
@@ -29,3 +35,7 @@ ? "app" | ||
const endpoint = request.endpoint.merge(route, parameters); | ||
const token = (await callback()).replace(/^(token|bearer) +/i, ""); | ||
const result = await callback(); | ||
if (!result) { | ||
return request(endpoint); | ||
} | ||
const token = result.replace(/^(token|bearer) +/i, ""); | ||
endpoint.headers.authorization = withAuthorizationPrefix(token); | ||
@@ -32,0 +42,0 @@ return request(endpoint); |
{ | ||
"name": "@octokit/auth-callback", | ||
"description": "GitHub API authentication using a callback method", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "files": [ |
@@ -44,30 +44,17 @@ # auth-callback.js | ||
Use callback to rotate through a set of tokens. | ||
```js | ||
const tokens = ["token1", "token2"]; | ||
let token; | ||
const auth = createCallbackAuth(() => { | ||
const token = tokens.shift(); | ||
tokens.push(token); | ||
return token; | ||
}); | ||
const auth = createCallbackAuth(() => token); | ||
await auth(); | ||
// { | ||
// type: 'token', | ||
// token: 'token1', | ||
// tokenType: 'oauth' | ||
// type: 'unauthenticated' | ||
// } | ||
token = "secret123"; | ||
await auth(); | ||
// { | ||
// type: 'token', | ||
// token: 'token2', | ||
// token: 'secret123', | ||
// tokenType: 'oauth' | ||
// } | ||
await auth(); | ||
// { | ||
// type: 'token', | ||
// token: 'token1', | ||
// tokenType: 'oauth' | ||
// } | ||
``` | ||
@@ -114,2 +101,9 @@ | ||
The async `auth()` method resolves to one of two possible authentication objects | ||
1. **Unauthenticated** if the `callback()` returns or resolves a falsy value | ||
2. **Token authentication** if the `callback()` returns or resolves with a string value | ||
### Unauthenticated | ||
<table width="100%"> | ||
@@ -138,2 +132,33 @@ <thead align=left> | ||
<td> | ||
<code>"unauthenticated"</code> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
### Token authentication | ||
<table width="100%"> | ||
<thead align=left> | ||
<tr> | ||
<th width=150> | ||
name | ||
</th> | ||
<th width=70> | ||
type | ||
</th> | ||
<th> | ||
description | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody align=left valign=top> | ||
<tr> | ||
<th> | ||
<code>type</code> | ||
</th> | ||
<th> | ||
<code>string</code> | ||
</th> | ||
<td> | ||
<code>"token"</code> | ||
@@ -140,0 +165,0 @@ </td> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
21221
208
226