oauth2-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/LICENSE.md +675 -0
- package/README.md +13 -0
- package/dist/FileStorage.d.ts +9 -0
- package/dist/FileStorage.js +29 -0
- package/dist/Localhost.d.ts +13 -0
- package/dist/Localhost.js +65 -0
- package/dist/Token.d.ts +18 -0
- package/dist/Token.js +26 -0
- package/dist/TokenManager.d.ts +18 -0
- package/dist/TokenManager.js +79 -0
- package/dist/TokenStorage.d.ts +5 -0
- package/dist/TokenStorage.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/package.json +51 -0
- package/src/FileStorage.ts +40 -0
- package/src/Localhost.ts +92 -0
- package/src/Token.ts +37 -0
- package/src/TokenManager.ts +111 -0
- package/src/TokenStorage.ts +6 -0
- package/src/index.ts +4 -0
- package/tsconfig.json +8 -0
- package/views/complete.ejs +43 -0
- package/views/error.ejs +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Token } from './Token.js';
|
|
2
|
+
import { TokenStorage } from './TokenStorage.js';
|
|
3
|
+
export declare class FileStorage implements TokenStorage {
|
|
4
|
+
private fileLock;
|
|
5
|
+
private readonly filePath;
|
|
6
|
+
constructor(filePath: string);
|
|
7
|
+
load(): Promise<Token | undefined>;
|
|
8
|
+
save(tokens: Token): Promise<Token>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Mutex } from 'async-mutex';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { Token } from './Token.js';
|
|
5
|
+
export class FileStorage {
|
|
6
|
+
fileLock = new Mutex();
|
|
7
|
+
filePath;
|
|
8
|
+
constructor(filePath) {
|
|
9
|
+
this.filePath = path.resolve(process.cwd(), filePath);
|
|
10
|
+
}
|
|
11
|
+
async load() {
|
|
12
|
+
return await this.fileLock.runExclusive((async () => {
|
|
13
|
+
if (fs.existsSync(this.filePath)) {
|
|
14
|
+
return Token.fromResponse(JSON.parse(fs.readFileSync(this.filePath).toString()));
|
|
15
|
+
}
|
|
16
|
+
return undefined;
|
|
17
|
+
}).bind(this));
|
|
18
|
+
}
|
|
19
|
+
async save(tokens) {
|
|
20
|
+
await this.fileLock.runExclusive((async () => {
|
|
21
|
+
const dirPath = path.dirname(this.filePath);
|
|
22
|
+
if (!fs.existsSync(dirPath)) {
|
|
23
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
24
|
+
}
|
|
25
|
+
fs.writeFileSync(this.filePath, JSON.stringify(tokens));
|
|
26
|
+
}).bind(this));
|
|
27
|
+
return tokens;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as Configuration from '@battis/oauth2-configure';
|
|
2
|
+
type Options = Configuration.Options & {
|
|
3
|
+
authorization_url: string;
|
|
4
|
+
redirect_uri: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
code_verifier?: string;
|
|
7
|
+
state?: string;
|
|
8
|
+
resolve: Function;
|
|
9
|
+
reject: Function;
|
|
10
|
+
views?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function redirectServer(options: Options): Promise<void>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as Configuration from '@battis/oauth2-configure';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import open from 'open';
|
|
6
|
+
import * as client from 'openid-client';
|
|
7
|
+
export async function redirectServer(options) {
|
|
8
|
+
const { authorization_url, redirect_uri, code_verifier, state, headers, resolve, reject, views = '../views' } = options;
|
|
9
|
+
const redirectUrl = new URL(redirect_uri);
|
|
10
|
+
const configuration = await Configuration.acquire(options);
|
|
11
|
+
const app = express();
|
|
12
|
+
const port = redirectUrl.port !== '' ? redirectUrl.port : 80;
|
|
13
|
+
const server = app.listen(port);
|
|
14
|
+
const ejs = await import('ejs');
|
|
15
|
+
let view = 'complete.ejs';
|
|
16
|
+
let tokens = undefined;
|
|
17
|
+
let error = undefined;
|
|
18
|
+
app.get('/authorize', async (req, res) => {
|
|
19
|
+
let viewPath = path.resolve(import.meta.dirname, views, 'authorize');
|
|
20
|
+
if (ejs && fs.existsSync(viewPath)) {
|
|
21
|
+
res.send(await ejs.renderFile(viewPath));
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
res.redirect(authorization_url);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
app.get(redirectUrl.pathname, async (req, res) => {
|
|
28
|
+
try {
|
|
29
|
+
const currentUrl = new URL(req.originalUrl, redirect_uri);
|
|
30
|
+
tokens = await client.authorizationCodeGrant(configuration, currentUrl, {
|
|
31
|
+
pkceCodeVerifier: code_verifier,
|
|
32
|
+
expectedState: state
|
|
33
|
+
},
|
|
34
|
+
// @ts-ignore FIXME undocumented arg pass-through to oauth4webapi
|
|
35
|
+
{ headers });
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
error = e;
|
|
39
|
+
view = 'error.ejs';
|
|
40
|
+
}
|
|
41
|
+
if (!tokens && !error) {
|
|
42
|
+
error = 'No tokens received in response to authorization code';
|
|
43
|
+
}
|
|
44
|
+
if (ejs) {
|
|
45
|
+
res.send(await ejs.renderFile(path.resolve(import.meta.dirname, views, view), {
|
|
46
|
+
tokens,
|
|
47
|
+
error
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
res.send(error || 'You may close this window.');
|
|
52
|
+
}
|
|
53
|
+
server.close();
|
|
54
|
+
if (error) {
|
|
55
|
+
reject(error);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
resolve(tokens);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
app.get('*', (_, res) => {
|
|
62
|
+
res.status(404).send();
|
|
63
|
+
});
|
|
64
|
+
open(`http://localhost:${port}/authorize`);
|
|
65
|
+
}
|
package/dist/Token.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as client from 'openid-client';
|
|
2
|
+
interface TokenResponse extends client.TokenEndpointResponse {
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
}
|
|
5
|
+
export declare class Token implements TokenResponse {
|
|
6
|
+
readonly access_token: string;
|
|
7
|
+
readonly token_type: Lowercase<string>;
|
|
8
|
+
readonly timestamp?: any;
|
|
9
|
+
readonly refresh_token?: string;
|
|
10
|
+
readonly refresh_token_expires_in?: number;
|
|
11
|
+
readonly scope?: string;
|
|
12
|
+
readonly id_token?: string;
|
|
13
|
+
readonly expires_in?: number;
|
|
14
|
+
private constructor();
|
|
15
|
+
static fromResponse(response?: client.TokenEndpointResponse): Token | undefined;
|
|
16
|
+
hasExpired(): boolean;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/Token.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class Token {
|
|
2
|
+
access_token;
|
|
3
|
+
token_type;
|
|
4
|
+
timestamp;
|
|
5
|
+
refresh_token;
|
|
6
|
+
refresh_token_expires_in;
|
|
7
|
+
scope;
|
|
8
|
+
id_token;
|
|
9
|
+
expires_in;
|
|
10
|
+
constructor(response) {
|
|
11
|
+
this.access_token = response.access_token;
|
|
12
|
+
this.token_type = response.token_type;
|
|
13
|
+
this.timestamp = response.timestamp;
|
|
14
|
+
Object.assign(this, response);
|
|
15
|
+
}
|
|
16
|
+
static fromResponse(response) {
|
|
17
|
+
if (response) {
|
|
18
|
+
return new Token({ timestamp: Date.now(), ...response });
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
hasExpired() {
|
|
23
|
+
return (this.expires_in === undefined ||
|
|
24
|
+
Date.now() > this.timestamp + this.expires_in);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as Configuration from '@battis/oauth2-configure';
|
|
2
|
+
import { Token } from './Token.js';
|
|
3
|
+
import { TokenStorage } from './TokenStorage.js';
|
|
4
|
+
export type Options = Configuration.Options & {
|
|
5
|
+
scope?: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
parameters?: Record<string, string>;
|
|
8
|
+
store?: TokenStorage | string;
|
|
9
|
+
};
|
|
10
|
+
export declare class TokenManager {
|
|
11
|
+
private options;
|
|
12
|
+
private tokenMutex;
|
|
13
|
+
private store?;
|
|
14
|
+
constructor(options: Options);
|
|
15
|
+
getToken(): Promise<Token | undefined>;
|
|
16
|
+
private refreshToken;
|
|
17
|
+
private authorize;
|
|
18
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import * as Configuration from '@battis/oauth2-configure';
|
|
2
|
+
import { Mutex } from 'async-mutex';
|
|
3
|
+
import * as client from 'openid-client';
|
|
4
|
+
import { FileStorage } from './FileStorage.js';
|
|
5
|
+
import * as Localhost from './Localhost.js';
|
|
6
|
+
import { Token } from './Token.js';
|
|
7
|
+
export class TokenManager {
|
|
8
|
+
options;
|
|
9
|
+
tokenMutex = new Mutex();
|
|
10
|
+
store;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
if (this.options.store) {
|
|
14
|
+
if (typeof this.options.store === 'string') {
|
|
15
|
+
this.store = new FileStorage(this.options.store);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this.store = this.options.store;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async getToken() {
|
|
23
|
+
return await this.tokenMutex.runExclusive((async () => {
|
|
24
|
+
let token = await this.store?.load();
|
|
25
|
+
if (token?.hasExpired()) {
|
|
26
|
+
token = await this.refreshToken(token);
|
|
27
|
+
}
|
|
28
|
+
return token || (await this.authorize());
|
|
29
|
+
}).bind(this));
|
|
30
|
+
}
|
|
31
|
+
async refreshToken(token) {
|
|
32
|
+
if (token.refresh_token) {
|
|
33
|
+
const { headers, parameters } = this.options;
|
|
34
|
+
let freshTokens;
|
|
35
|
+
if ((freshTokens = Token.fromResponse(await client.refreshTokenGrant(await Configuration.acquire(this.options), token.refresh_token, parameters,
|
|
36
|
+
// @ts-ignore FIXME undocumented arg pass-through to oauth4webapi
|
|
37
|
+
{ headers })))) {
|
|
38
|
+
return this.store?.save(freshTokens) || freshTokens;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return this.authorize();
|
|
42
|
+
}
|
|
43
|
+
async authorize() {
|
|
44
|
+
const { scope, redirect_uri, parameters: additionalParameters } = this.options;
|
|
45
|
+
return new Promise(async (resolve, reject) => {
|
|
46
|
+
const configuration = await Configuration.acquire(this.options);
|
|
47
|
+
const code_verifier = client.randomPKCECodeVerifier();
|
|
48
|
+
const code_challenge = await client.calculatePKCECodeChallenge(code_verifier);
|
|
49
|
+
let state = undefined;
|
|
50
|
+
const parameters = {
|
|
51
|
+
...additionalParameters,
|
|
52
|
+
redirect_uri,
|
|
53
|
+
code_challenge,
|
|
54
|
+
code_challenge_method: 'S256' // TODO make code challenge method configurable?
|
|
55
|
+
};
|
|
56
|
+
if (scope) {
|
|
57
|
+
parameters.scope = scope;
|
|
58
|
+
}
|
|
59
|
+
if (!configuration.serverMetadata().supportsPKCE()) {
|
|
60
|
+
state = client.randomState();
|
|
61
|
+
parameters.state = state;
|
|
62
|
+
}
|
|
63
|
+
await Localhost.redirectServer({
|
|
64
|
+
...this.options,
|
|
65
|
+
authorization_url: client.buildAuthorizationUrl(configuration, parameters).href,
|
|
66
|
+
code_verifier,
|
|
67
|
+
state,
|
|
68
|
+
resolve: (async (response) => {
|
|
69
|
+
const token = Token.fromResponse(response);
|
|
70
|
+
if (token && this.store) {
|
|
71
|
+
await this.store.save(token);
|
|
72
|
+
}
|
|
73
|
+
resolve(token);
|
|
74
|
+
}).bind(this),
|
|
75
|
+
reject
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "oauth2-cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Acquire API access tokens via OAuth 2.0 within CLI tools",
|
|
5
|
+
"homepage": "https://github.com/battis/oauth-cli/tree/main/packages/oauth2-cli#readme",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/battis/oauth-cli.git",
|
|
9
|
+
"directory": "packages/oauth2-cli"
|
|
10
|
+
},
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Seth Battis",
|
|
13
|
+
"url": "https://github.com/battis"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"async-mutex": "^0.5.0",
|
|
20
|
+
"express": "^4.21.1",
|
|
21
|
+
"openid-client": "^6.1.3",
|
|
22
|
+
"@battis/oauth2-configure": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@tsconfig/node20": "^20.1.4",
|
|
26
|
+
"@types/ejs": "^3.1.5",
|
|
27
|
+
"@types/express": "^5.0.0",
|
|
28
|
+
"del-cli": "^6.0.0",
|
|
29
|
+
"npm-run-all": "^4.1.5",
|
|
30
|
+
"open": "^10.1.0",
|
|
31
|
+
"typescript": "^5.6.3"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"ejs": "*"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencyMeta": {
|
|
37
|
+
"ejs": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"repo": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/battis/oauth2-cli.git"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"clean": "del ./dist",
|
|
47
|
+
"build": "run-s build:*",
|
|
48
|
+
"build:clean": "run-s clean",
|
|
49
|
+
"build:compile": "tsc"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Mutex } from 'async-mutex';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { Token } from './Token.js';
|
|
5
|
+
import { TokenStorage } from './TokenStorage.js';
|
|
6
|
+
|
|
7
|
+
export class FileStorage implements TokenStorage {
|
|
8
|
+
private fileLock = new Mutex();
|
|
9
|
+
private readonly filePath: string;
|
|
10
|
+
|
|
11
|
+
public constructor(filePath: string) {
|
|
12
|
+
this.filePath = path.resolve(process.cwd(), filePath);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async load() {
|
|
16
|
+
return await this.fileLock.runExclusive(
|
|
17
|
+
(async () => {
|
|
18
|
+
if (fs.existsSync(this.filePath)) {
|
|
19
|
+
return Token.fromResponse(
|
|
20
|
+
JSON.parse(fs.readFileSync(this.filePath).toString())
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}).bind(this)
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public async save(tokens: Token) {
|
|
29
|
+
await this.fileLock.runExclusive(
|
|
30
|
+
(async () => {
|
|
31
|
+
const dirPath = path.dirname(this.filePath);
|
|
32
|
+
if (!fs.existsSync(dirPath)) {
|
|
33
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
34
|
+
}
|
|
35
|
+
fs.writeFileSync(this.filePath, JSON.stringify(tokens));
|
|
36
|
+
}).bind(this)
|
|
37
|
+
);
|
|
38
|
+
return tokens;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/Localhost.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as Configuration from '@battis/oauth2-configure';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import open from 'open';
|
|
6
|
+
import * as client from 'openid-client';
|
|
7
|
+
|
|
8
|
+
type Options = Configuration.Options & {
|
|
9
|
+
authorization_url: string;
|
|
10
|
+
redirect_uri: string;
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
code_verifier?: string;
|
|
13
|
+
state?: string;
|
|
14
|
+
resolve: Function;
|
|
15
|
+
reject: Function;
|
|
16
|
+
views?: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export async function redirectServer(options: Options) {
|
|
20
|
+
const {
|
|
21
|
+
authorization_url,
|
|
22
|
+
redirect_uri,
|
|
23
|
+
code_verifier,
|
|
24
|
+
state,
|
|
25
|
+
headers,
|
|
26
|
+
resolve,
|
|
27
|
+
reject,
|
|
28
|
+
views = '../views'
|
|
29
|
+
} = options;
|
|
30
|
+
const redirectUrl = new URL(redirect_uri);
|
|
31
|
+
|
|
32
|
+
const configuration = await Configuration.acquire(options);
|
|
33
|
+
|
|
34
|
+
const app = express();
|
|
35
|
+
const port = redirectUrl.port !== '' ? redirectUrl.port : 80;
|
|
36
|
+
const server = app.listen(port);
|
|
37
|
+
const ejs = await import('ejs');
|
|
38
|
+
let view = 'complete.ejs';
|
|
39
|
+
let tokens: client.TokenEndpointResponse | undefined = undefined;
|
|
40
|
+
let error: any = undefined;
|
|
41
|
+
|
|
42
|
+
app.get('/authorize', async (req, res) => {
|
|
43
|
+
let viewPath = path.resolve(import.meta.dirname, views, 'authorize');
|
|
44
|
+
if (ejs && fs.existsSync(viewPath)) {
|
|
45
|
+
res.send(await ejs.renderFile(viewPath));
|
|
46
|
+
} else {
|
|
47
|
+
res.redirect(authorization_url);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
app.get(redirectUrl.pathname, async (req, res) => {
|
|
51
|
+
try {
|
|
52
|
+
const currentUrl = new URL(req.originalUrl, redirect_uri);
|
|
53
|
+
tokens = await client.authorizationCodeGrant(
|
|
54
|
+
configuration,
|
|
55
|
+
currentUrl,
|
|
56
|
+
{
|
|
57
|
+
pkceCodeVerifier: code_verifier,
|
|
58
|
+
expectedState: state
|
|
59
|
+
},
|
|
60
|
+
// @ts-ignore FIXME undocumented arg pass-through to oauth4webapi
|
|
61
|
+
{ headers }
|
|
62
|
+
);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
error = e;
|
|
65
|
+
view = 'error.ejs';
|
|
66
|
+
}
|
|
67
|
+
if (!tokens && !error) {
|
|
68
|
+
error = 'No tokens received in response to authorization code';
|
|
69
|
+
}
|
|
70
|
+
if (ejs) {
|
|
71
|
+
res.send(
|
|
72
|
+
await ejs.renderFile(path.resolve(import.meta.dirname, views, view), {
|
|
73
|
+
tokens,
|
|
74
|
+
error
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
} else {
|
|
78
|
+
res.send(error || 'You may close this window.');
|
|
79
|
+
}
|
|
80
|
+
server.close();
|
|
81
|
+
if (error) {
|
|
82
|
+
reject(error);
|
|
83
|
+
} else {
|
|
84
|
+
resolve(tokens);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
app.get('*', (_, res) => {
|
|
88
|
+
res.status(404).send();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
open(`http://localhost:${port}/authorize`);
|
|
92
|
+
}
|
package/src/Token.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as client from 'openid-client';
|
|
2
|
+
|
|
3
|
+
interface TokenResponse extends client.TokenEndpointResponse {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export class Token implements TokenResponse {
|
|
8
|
+
public readonly access_token: string;
|
|
9
|
+
public readonly token_type: Lowercase<string>;
|
|
10
|
+
public readonly timestamp?: any;
|
|
11
|
+
public readonly refresh_token?: string;
|
|
12
|
+
public readonly refresh_token_expires_in?: number;
|
|
13
|
+
public readonly scope?: string;
|
|
14
|
+
public readonly id_token?: string;
|
|
15
|
+
public readonly expires_in?: number;
|
|
16
|
+
|
|
17
|
+
private constructor(response: client.TokenEndpointResponse) {
|
|
18
|
+
this.access_token = response.access_token;
|
|
19
|
+
this.token_type = response.token_type;
|
|
20
|
+
this.timestamp = response.timestamp;
|
|
21
|
+
Object.assign(this, response);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static fromResponse(response?: client.TokenEndpointResponse) {
|
|
25
|
+
if (response) {
|
|
26
|
+
return new Token({ timestamp: Date.now(), ...response });
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public hasExpired() {
|
|
32
|
+
return (
|
|
33
|
+
this.expires_in === undefined ||
|
|
34
|
+
Date.now() > this.timestamp + this.expires_in
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import * as Configuration from '@battis/oauth2-configure';
|
|
2
|
+
import { Mutex } from 'async-mutex';
|
|
3
|
+
import open from 'open';
|
|
4
|
+
import * as client from 'openid-client';
|
|
5
|
+
import { FileStorage } from './FileStorage.js';
|
|
6
|
+
import * as Localhost from './Localhost.js';
|
|
7
|
+
import { Token } from './Token.js';
|
|
8
|
+
import { TokenStorage } from './TokenStorage.js';
|
|
9
|
+
|
|
10
|
+
export type Options = Configuration.Options & {
|
|
11
|
+
scope?: string;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
parameters?: Record<string, string>;
|
|
14
|
+
store?: TokenStorage | string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export class TokenManager {
|
|
18
|
+
private tokenMutex = new Mutex();
|
|
19
|
+
private store?: TokenStorage;
|
|
20
|
+
|
|
21
|
+
public constructor(private options: Options) {
|
|
22
|
+
if (this.options.store) {
|
|
23
|
+
if (typeof this.options.store === 'string') {
|
|
24
|
+
this.store = new FileStorage(this.options.store);
|
|
25
|
+
} else {
|
|
26
|
+
this.store = this.options.store;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async getToken() {
|
|
32
|
+
return await this.tokenMutex.runExclusive(
|
|
33
|
+
(async () => {
|
|
34
|
+
let token = await this.store?.load();
|
|
35
|
+
if (token?.hasExpired()) {
|
|
36
|
+
token = await this.refreshToken(token);
|
|
37
|
+
}
|
|
38
|
+
return token || (await this.authorize());
|
|
39
|
+
}).bind(this)
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private async refreshToken(token: Token): Promise<Token | undefined> {
|
|
44
|
+
if (token.refresh_token) {
|
|
45
|
+
const { headers, parameters } = this.options;
|
|
46
|
+
let freshTokens;
|
|
47
|
+
if (
|
|
48
|
+
(freshTokens = Token.fromResponse(
|
|
49
|
+
await client.refreshTokenGrant(
|
|
50
|
+
await Configuration.acquire(this.options),
|
|
51
|
+
token.refresh_token,
|
|
52
|
+
parameters,
|
|
53
|
+
// @ts-ignore FIXME undocumented arg pass-through to oauth4webapi
|
|
54
|
+
{ headers }
|
|
55
|
+
)
|
|
56
|
+
))
|
|
57
|
+
) {
|
|
58
|
+
return this.store?.save(freshTokens) || freshTokens;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return this.authorize();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private async authorize(): Promise<Token | undefined> {
|
|
65
|
+
const {
|
|
66
|
+
scope,
|
|
67
|
+
redirect_uri,
|
|
68
|
+
parameters: additionalParameters
|
|
69
|
+
} = this.options;
|
|
70
|
+
|
|
71
|
+
return new Promise(async (resolve, reject) => {
|
|
72
|
+
const configuration = await Configuration.acquire(this.options);
|
|
73
|
+
const code_verifier = client.randomPKCECodeVerifier();
|
|
74
|
+
const code_challenge =
|
|
75
|
+
await client.calculatePKCECodeChallenge(code_verifier);
|
|
76
|
+
let state: string | undefined = undefined;
|
|
77
|
+
const parameters: Record<string, string> = {
|
|
78
|
+
...additionalParameters,
|
|
79
|
+
redirect_uri,
|
|
80
|
+
code_challenge,
|
|
81
|
+
code_challenge_method: 'S256' // TODO make code challenge method configurable?
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (scope) {
|
|
85
|
+
parameters.scope = scope;
|
|
86
|
+
}
|
|
87
|
+
if (!configuration.serverMetadata().supportsPKCE()) {
|
|
88
|
+
state = client.randomState();
|
|
89
|
+
parameters.state = state;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
await Localhost.redirectServer({
|
|
93
|
+
...this.options,
|
|
94
|
+
authorization_url: client.buildAuthorizationUrl(
|
|
95
|
+
configuration,
|
|
96
|
+
parameters
|
|
97
|
+
).href,
|
|
98
|
+
code_verifier,
|
|
99
|
+
state,
|
|
100
|
+
resolve: (async (response: client.TokenEndpointResponse) => {
|
|
101
|
+
const token = Token.fromResponse(response);
|
|
102
|
+
if (token && this.store) {
|
|
103
|
+
await this.store.save(token);
|
|
104
|
+
}
|
|
105
|
+
resolve(token);
|
|
106
|
+
}).bind(this),
|
|
107
|
+
reject
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
package/src/index.ts
ADDED