ts / dist / cjs / auth.js
627 words
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.deviceLogin = exports.revokeApiKey = exports.createApiKey = exports.listApiKeys = exports.deviceComplete = exports.devicePoll = exports.deviceStart = void 0;
const errors_js_1 = require("./errors.js");
const http = __importStar(require("./http.js"));
/** POST /auth/device/start — Start a device login. Anonymous allowed. */
const deviceStart = (client) => http.post(client, '/auth/device/start');
exports.deviceStart = deviceStart;
/** POST /auth/device/poll — Poll a device login. Anonymous allowed. */
const devicePoll = (client, deviceCode) => http.post(client, '/auth/device/poll', { body: { device_code: deviceCode } });
exports.devicePoll = devicePoll;
/** POST /auth/device/complete — Complete a device login. Anonymous allowed. */
const deviceComplete = (client, deviceCode, confirmationCode) => http.post(client, '/auth/device/complete', {
    body: { device_code: deviceCode, confirmation_code: confirmationCode },
});
exports.deviceComplete = deviceComplete;
/** GET /auth/api-keys — List API keys (account JWT only). */
const listApiKeys = (client) => http.get(client, '/auth/api-keys');
exports.listApiKeys = listApiKeys;
/** POST /auth/api-keys — Create an API key (account JWT only). */
const createApiKey = (client, body) => http.post(client, '/auth/api-keys', { body });
exports.createApiKey = createApiKey;
/** POST /auth/api-keys/{id}/revoke — Revoke an API key (account JWT only). */
const revokeApiKey = (client, id) => http.post(client, '/auth/api-keys/{id}/revoke', { params: { id } });
exports.revokeApiKey = revokeApiKey;
const defaultPrompt = (start) => {
    console.log(`To sign in, open: ${start.verification_uri_complete ?? start.verification_uri}`);
    console.log(`Code: ${start.user_code}`);
};
const defaultConfirm = async () => {
    const { createInterface } = await Promise.resolve().then(() => __importStar(require('node:readline/promises')));
    const rl = createInterface({ input: process.stdin, output: process.stdout });
    try {
        return (await rl.question('Enter the confirmation code shown after approving: ')).trim();
    }
    finally {
        rl.close();
    }
};
/**
 * Run the whole device login flow: start, prompt, poll until approved,
 * complete. Returns the completion body; feed its `access_token` to
 * `createClient` as `apiKey`. Throws a timeout error when the login is not
 * approved in time.
 */
const deviceLogin = async (client, options = {}) => {
    const start = await (0, exports.deviceStart)(client);
    const { onPrompt = defaultPrompt, confirm = defaultConfirm } = options;
    const intervalMs = options.intervalMs ?? 1000 * (start.interval ?? 5);
    const timeoutMs = options.timeoutMs ?? 1000 * (start.expires_in ?? 600);
    // While the user hasn't approved, the API answers 400 authorization-pending;
    // 429 slow-down asks for a longer interval (pollUntil's backoff grows it).
    // Anything else (401 expired, 403 denied) is terminal and rethrown.
    const poll = async () => {
        try {
            return await (0, exports.devicePoll)(client, start.device_code);
        }
        catch (e) {
            if ((0, errors_js_1.isApiError)(e) && (e.status === 400 || e.status === 429))
                return { approved: false };
            throw e;
        }
    };
    await onPrompt(start);
    await http.pollUntil(poll, (r) => r.approved, {
        intervalMs,
        timeoutMs,
        message: 'Device login timed out before approval',
        sleep: client.sleep,
    });
    return (0, exports.deviceComplete)(client, start.device_code, await confirm(start));
};
exports.deviceLogin = deviceLogin;

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/ts.git