newo 1.4.0 → 1.5.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/src/auth.js DELETED
@@ -1,92 +0,0 @@
1
- import fs from 'fs-extra';
2
- import path from 'path';
3
- import axios from 'axios';
4
- import dotenv from 'dotenv';
5
- dotenv.config();
6
-
7
- const {
8
- NEWO_BASE_URL,
9
- NEWO_API_KEY,
10
- NEWO_ACCESS_TOKEN,
11
- NEWO_REFRESH_TOKEN,
12
- NEWO_REFRESH_URL
13
- } = process.env;
14
-
15
- const STATE_DIR = path.join(process.cwd(), '.newo');
16
- const TOKENS_PATH = path.join(STATE_DIR, 'tokens.json');
17
-
18
- async function saveTokens(tokens) {
19
- await fs.ensureDir(STATE_DIR);
20
- await fs.writeJson(TOKENS_PATH, tokens, { spaces: 2 });
21
- }
22
-
23
- async function loadTokens() {
24
- if (await fs.pathExists(TOKENS_PATH)) {
25
- return fs.readJson(TOKENS_PATH);
26
- }
27
- if (NEWO_ACCESS_TOKEN || NEWO_REFRESH_TOKEN) {
28
- const t = {
29
- access_token: NEWO_ACCESS_TOKEN || '',
30
- refresh_token: NEWO_REFRESH_TOKEN || '',
31
- expires_at: Date.now() + 10 * 60 * 1000
32
- };
33
- await saveTokens(t);
34
- return t;
35
- }
36
- return null;
37
- }
38
-
39
- function isExpired(tokens) {
40
- if (!tokens?.expires_at) return false;
41
- return Date.now() >= tokens.expires_at - 10_000;
42
- }
43
-
44
- export async function exchangeApiKeyForToken() {
45
- if (!NEWO_API_KEY) throw new Error('NEWO_API_KEY not set. Provide an API key in .env');
46
- const url = `${NEWO_BASE_URL}/api/v1/auth/api-key/token`;
47
- const res = await axios.post(url, {}, { headers: { 'x-api-key': NEWO_API_KEY, 'accept': 'application/json' } });
48
- const data = res.data || {};
49
- const access = data.access_token || data.token || data.accessToken;
50
- const refresh = data.refresh_token || data.refreshToken || '';
51
- const expiresInSec = data.expires_in || data.expiresIn || 3600;
52
- const tokens = { access_token: access, refresh_token: refresh, expires_at: Date.now() + expiresInSec * 1000 };
53
- await saveTokens(tokens);
54
- return tokens;
55
- }
56
-
57
- export async function refreshWithEndpoint(refreshToken) {
58
- if (!NEWO_REFRESH_URL) throw new Error('NEWO_REFRESH_URL not set');
59
- const res = await axios.post(NEWO_REFRESH_URL, { refresh_token: refreshToken }, { headers: { 'accept': 'application/json' } });
60
- const data = res.data || {};
61
- const access = data.access_token || data.token || data.accessToken;
62
- const refresh = data.refresh_token ?? refreshToken;
63
- const expiresInSec = data.expires_in || 3600;
64
- const tokens = { access_token: access, refresh_token: refresh, expires_at: Date.now() + expiresInSec * 1000 };
65
- await saveTokens(tokens);
66
- return tokens;
67
- }
68
-
69
- export async function getValidAccessToken() {
70
- let tokens = await loadTokens();
71
- if (!tokens || !tokens.access_token) {
72
- tokens = await exchangeApiKeyForToken();
73
- return tokens.access_token;
74
- }
75
- if (!isExpired(tokens)) return tokens.access_token;
76
-
77
- if (NEWO_REFRESH_URL && tokens.refresh_token) {
78
- try {
79
- tokens = await refreshWithEndpoint(tokens.refresh_token);
80
- return tokens.access_token;
81
- } catch (e) {
82
- console.warn('Refresh failed, falling back to API key exchange…');
83
- }
84
- }
85
- tokens = await exchangeApiKeyForToken();
86
- return tokens.access_token;
87
- }
88
-
89
- export async function forceReauth() {
90
- const tokens = await exchangeApiKeyForToken();
91
- return tokens.access_token;
92
- }
package/src/hash.js DELETED
@@ -1,17 +0,0 @@
1
- import crypto from 'crypto';
2
- import fs from 'fs-extra';
3
- import { ensureState, HASHES_PATH } from './fsutil.js';
4
-
5
- export function sha256(str) {
6
- return crypto.createHash('sha256').update(str, 'utf8').digest('hex');
7
- }
8
-
9
- export async function loadHashes() {
10
- await ensureState();
11
- if (await fs.pathExists(HASHES_PATH)) return fs.readJson(HASHES_PATH);
12
- return {};
13
- }
14
-
15
- export async function saveHashes(h) {
16
- await fs.writeJson(HASHES_PATH, h, { spaces: 2 });
17
- }