UNPKG

4.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const utils_fs_1 = require("@ionic/utils-fs");
5const os = require("os");
6const path = require("path");
7const guards_1 = require("../guards");
8const http_1 = require("./http");
9exports.ERROR_SSH_MISSING_PRIVKEY = 'SSH_MISSING_PRIVKEY';
10exports.ERROR_SSH_INVALID_PUBKEY = 'SSH_INVALID_PUBKEY';
11exports.ERROR_SSH_INVALID_PRIVKEY = 'SSH_INVALID_PRIVKEY';
12function getGeneratedPrivateKeyPath(userId = 0) {
13 return tslib_1.__awaiter(this, void 0, void 0, function* () {
14 return path.resolve(os.homedir(), '.ssh', 'ionic', String(userId));
15 });
16}
17exports.getGeneratedPrivateKeyPath = getGeneratedPrivateKeyPath;
18function parsePublicKeyFile(pubkeyPath) {
19 return tslib_1.__awaiter(this, void 0, void 0, function* () {
20 return parsePublicKey((yield utils_fs_1.readFile(pubkeyPath, { encoding: 'utf8' })).trim());
21 });
22}
23exports.parsePublicKeyFile = parsePublicKeyFile;
24/**
25 * @return Promise<[full pubkey, algorithm, public numbers, annotation]>
26 */
27function parsePublicKey(pubkey) {
28 return tslib_1.__awaiter(this, void 0, void 0, function* () {
29 const r = /^(ssh-[A-z0-9]+)\s([A-z0-9+\/=]+)\s?(.+)?$/.exec(pubkey);
30 if (!r) {
31 throw exports.ERROR_SSH_INVALID_PUBKEY;
32 }
33 if (!r[3]) {
34 r[3] = '';
35 }
36 r[1] = r[1].trim();
37 r[2] = r[2].trim();
38 r[3] = r[3].trim();
39 return [pubkey, r[1], r[2], r[3]];
40 });
41}
42exports.parsePublicKey = parsePublicKey;
43function validatePrivateKey(keyPath) {
44 return tslib_1.__awaiter(this, void 0, void 0, function* () {
45 try {
46 yield utils_fs_1.stat(keyPath);
47 }
48 catch (e) {
49 if (e.code === 'ENOENT') {
50 throw exports.ERROR_SSH_MISSING_PRIVKEY;
51 }
52 throw e;
53 }
54 const f = yield utils_fs_1.readFile(keyPath, { encoding: 'utf8' });
55 const lines = f.split('\n');
56 if (!lines[0].match(/^\-{5}BEGIN [R|D]SA PRIVATE KEY\-{5}$/)) {
57 throw exports.ERROR_SSH_INVALID_PRIVKEY;
58 }
59 });
60}
61exports.validatePrivateKey = validatePrivateKey;
62class SSHKeyClient extends http_1.ResourceClient {
63 constructor({ client, token, user }) {
64 super();
65 this.client = client;
66 this.token = token;
67 this.user = user;
68 }
69 create({ pubkey }) {
70 return tslib_1.__awaiter(this, void 0, void 0, function* () {
71 const { req } = yield this.client.make('POST', `/users/${this.user.id}/sshkeys`);
72 this.applyAuthentication(req, this.token);
73 req.send({ pubkey });
74 const res = yield this.client.do(req);
75 if (!guards_1.isSSHKeyResponse(res)) {
76 throw http_1.createFatalAPIFormat(req, res);
77 }
78 return res.data;
79 });
80 }
81 load(id) {
82 return tslib_1.__awaiter(this, void 0, void 0, function* () {
83 const { req } = yield this.client.make('GET', `/users/${this.user.id}/sshkeys/${id}`);
84 this.applyAuthentication(req, this.token);
85 const res = yield this.client.do(req);
86 if (!guards_1.isSSHKeyResponse(res)) {
87 throw http_1.createFatalAPIFormat(req, res);
88 }
89 return res.data;
90 });
91 }
92 delete(id) {
93 return tslib_1.__awaiter(this, void 0, void 0, function* () {
94 const { req } = yield this.client.make('DELETE', `/users/${this.user.id}/sshkeys/${id}`);
95 this.applyAuthentication(req, this.token);
96 yield this.client.do(req);
97 });
98 }
99 paginate(args = {}) {
100 return this.client.paginate({
101 reqgen: () => tslib_1.__awaiter(this, void 0, void 0, function* () {
102 const { req } = yield this.client.make('GET', `/users/${this.user.id}/sshkeys`);
103 this.applyAuthentication(req, this.token);
104 return { req };
105 }),
106 guard: guards_1.isSSHKeyListResponse,
107 });
108 }
109}
110exports.SSHKeyClient = SSHKeyClient;