git-documentdb-plugin-remote-nodegit
Version:
GitDocumentDB plugin for remote connection using NodeGit
116 lines • 3.97 kB
JavaScript
;
/**
* GitDocumentDB plugin for remote connection using NodeGit
* Copyright (c) Hidekazu Kubota
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCredentialCallback = void 0;
const fs_1 = __importDefault(require("fs"));
// @ts-ignore
const nodegit_1 = __importDefault(require("@sosuisen/nodegit"));
const git_documentdb_remote_errors_1 = require("git-documentdb-remote-errors");
/**
* Insert credential options for GitHub
*
* @throws {@link InvalidURLFormatError}
* @throws {@link InvalidRepositoryURLError}
*
* @internal
*/
function createCredentialForGitHub(options) {
if (!options.remoteUrl.match(/^https?:\/\//)) {
throw new git_documentdb_remote_errors_1.InvalidURLFormatError('http protocol required in createCredentialForGitHub');
}
const connection = options.connection;
if (!connection.personalAccessToken) {
return undefined;
}
const urlArray = options.remoteUrl.replace(/^https?:\/\//, '').split('/');
// github.com/account_name/repository_name
if (urlArray.length !== 3) {
throw new git_documentdb_remote_errors_1.InvalidRepositoryURLError(options.remoteUrl);
}
const owner = urlArray[urlArray.length - 2];
const credentials = () => {
return nodegit_1.default.Cred.userpassPlaintextNew(owner, connection.personalAccessToken);
};
return credentials;
}
/**
* Create credential options for SSH
*
* @throws InvalidSSHKeyPathError
*
* @internal
*/
function createCredentialForSSH(options) {
var _a;
const connection = options.connection;
if (connection.privateKeyPath === undefined ||
!fs_1.default.existsSync(connection.privateKeyPath)) {
throw new git_documentdb_remote_errors_1.InvalidSSHKeyPathError();
}
if (connection.publicKeyPath === undefined ||
!fs_1.default.existsSync(connection.publicKeyPath)) {
throw new git_documentdb_remote_errors_1.InvalidSSHKeyPathError();
}
(_a = connection.passPhrase) !== null && _a !== void 0 ? _a : (connection.passPhrase = '');
const credentials = (url, userName) => {
return nodegit_1.default.Cred.sshKeyNew(userName, connection.publicKeyPath, connection.privateKeyPath, connection.passPhrase);
};
return credentials;
}
/**
* Create credential options
*
* @throws {@link InvalidAuthenticationTypeError}
*
* @throws # Error from createCredentialForGitHub
* @throws - {@link InvalidURLFormatError}
* @throws - {@link InvalidRepositoryURLError}
*
* @throws # Error from createCredentialForSSH
* @throws - {@link InvalidSSHKeyPathError}
*
* @internal
*/
function createCredentialCallback(options) {
var _a;
(_a = options.connection) !== null && _a !== void 0 ? _a : (options.connection = { type: 'none' });
let cred;
if (options.connection.type === 'github') {
cred = createCredentialForGitHub(options);
}
else if (options.connection.type === 'ssh') {
cred = createCredentialForSSH(options);
}
else if (options.connection.type === 'none') {
// nop
}
else {
// @ts-ignore
throw new git_documentdb_remote_errors_1.InvalidAuthenticationTypeError(options.connection.type);
}
let callbacks;
if (cred !== undefined) {
callbacks = {
credentials: cred,
};
}
else {
callbacks = new nodegit_1.default.RemoteCallbacks();
}
if (process.platform === 'darwin') {
// @ts-ignore
callbacks.certificateCheck = () => 0;
}
return callbacks;
}
exports.createCredentialCallback = createCredentialCallback;
//# sourceMappingURL=authentication.js.map