Skip to content

Commit

Permalink
GH-3397: added custom askpass logic
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta committed Nov 6, 2018
1 parent 104f356 commit 7f9ee3e
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 74 deletions.
6 changes: 5 additions & 1 deletion packages/git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@types/fs-extra": "^4.0.2",
"@types/keytar": "^4.0.1",
"diff": "^3.4.0",
"dugite-extra": "0.1.7",
"dugite-extra": "0.1.9",
"find-git-repositories": "^0.1.0",
"fs-extra": "^4.0.2",
"keytar": "^4.3.0",
Expand All @@ -28,6 +28,10 @@
{
"frontend": "lib/browser/git-frontend-module",
"backend": "lib/node/git-backend-module"
},
{
"backend": "lib/node/env/git-env-module",
"backendElectron": "lib/electron-node/env/electron-git-env-module"
}
],
"keywords": [
Expand Down
2 changes: 2 additions & 0 deletions packages/git/src/electron-node/askpass/askpass-empty.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
echo ''
6 changes: 6 additions & 0 deletions packages/git/src/electron-node/askpass/askpass.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
# Based on: https://github.com/Microsoft/vscode/blob/77f0e95307675c3936c05d641f72b8b32dc8e274/extensions/git/src/askpass.sh
THEIA_GIT_ASKPASS_PIPE=`mktemp`
THEIA_GIT_ASKPASS_PIPE="$THEIA_GIT_ASKPASS_PIPE" "$THEIA_GIT_ASKPASS_NODE" "$THEIA_GIT_ASKPASS_MAIN" $*
cat $THEIA_GIT_ASKPASS_PIPE
rm $THEIA_GIT_ASKPASS_PIPE
36 changes: 23 additions & 13 deletions packages/git/src/electron-node/askpass/askpass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*--------------------------------------------------------------------------------------------*/
// Based on: https://github.com/Microsoft/THEIA/blob/dd3e2d94f81139f9d18ba15a24c16c6061880b93/extensions/git/src/askpass.ts

import { injectable, postConstruct } from 'inversify';
import * as path from 'path';
import * as http from 'http';
import * as os from 'os';
Expand All @@ -36,14 +37,16 @@ export interface AskpassEnvironment {
readonly THEIA_GIT_ASKPASS_HANDLE?: string;
}

@injectable()
export class Askpass implements Disposable {

private server: http.Server;
private ipcHandlePathPromise: Promise<string>;
private ipcHandlePath: string | undefined;
private enabled = true;
protected server: http.Server;
protected ipcHandlePathPromise: Promise<string>;
protected ipcHandlePath: string | undefined;
protected enabled = true;

constructor() {
@postConstruct()
protected init(): void {
this.server = http.createServer((req, res) => this.onRequest(req, res));
this.ipcHandlePathPromise = this.setup().catch(err => {
console.error(err);
Expand Down Expand Up @@ -99,14 +102,21 @@ export class Askpass implements Disposable {
}

protected async prompt(host: string, request: string): Promise<string> {
const options: InputBoxOptions = {
password: /password/i.test(request),
placeHolder: request,
prompt: `Git: ${host}`,
ignoreFocusOut: true
};
// const options: InputBoxOptions = {
// password: /password/i.test(request),
// placeHolder: request,
// prompt: `Git: ${host}`,
// ignoreFocusOut: true
// };
if (/password/i.test(request)) {
}

if (/username/i.test(request)) {
}

return await window.showInputBox(options) || '';
// return await window.showInputBox(options) || '';
console.log('host', host, 'request', request);
return '';
}

protected async randomBytes(size: number): Promise<Buffer> {
Expand All @@ -130,7 +140,7 @@ export class Askpass implements Disposable {

return {
ELECTRON_RUN_AS_NODE: '1',
GIT_ASKPASS: path.join(__dirname, 'askpass.sh'),
GIT_ASKPASS: path.join(__dirname, '..', '..', '..', 'src', 'electron-node', 'askpass', 'askpass.sh'),
THEIA_GIT_ASKPASS_NODE: process.execPath,
THEIA_GIT_ASKPASS_MAIN: path.join(__dirname, 'askpass-main.js'),
THEIA_GIT_ASKPASS_HANDLE: await this.ipcHandlePathPromise
Expand Down
2 changes: 2 additions & 0 deletions packages/git/src/electron-node/env/electron-git-env-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import { ContainerModule } from 'inversify';
import { GitEnvProvider } from '../../node/env/git-env-provider';
import { Askpass } from '../askpass/askpass';
import { ElectronGitEnvProvider } from './electron-git-env-provider';

export default new ContainerModule(bind => {
bind(ElectronGitEnvProvider).toSelf().inSingletonScope();
bind(Askpass).toSelf();
bind(GitEnvProvider).toService(ElectronGitEnvProvider);
});
23 changes: 20 additions & 3 deletions packages/git/src/electron-node/env/electron-git-env-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,34 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable } from 'inversify';
import { inject, injectable, postConstruct } from 'inversify';
import { DefaultGitEnvProvider } from '../../node/env/git-env-provider';
import { Askpass } from '../askpass/askpass';

/**
* Git environment provider for Electron.
*
* TODO: describe this!!!
* This Git environment provider is customized for the Electron-based application. It sets the `GIT_ASKPASS` environment variable, to run
* a custom script for the authentication.
*/
@injectable()
export class ElectronGitEnvProvider extends DefaultGitEnvProvider {

// TODO ASKPASS
@inject(Askpass)
protected readonly askpass: Askpass;
protected _env: Object | undefined;

@postConstruct()
protected init(): void {
super.init();
this.toDispose.push(this.askpass);
}

async getEnv(): Promise<Object> {
if (!this._env) {
this._env = this.askpass.getEnv();
}
return this._env;
}

}
Loading

0 comments on commit 7f9ee3e

Please sign in to comment.