import { StringType } from '@radic/util'; import { db } from './db'; import { merge } from 'lodash'; import { BitbucketConnection, BitbucketServerConnection, GithubConnection, Connection, JenkinsConnection, PackagistConnection, JiraConnection } from './connections'; var auths = db.get('auths'); export class Remote extends StringType { static bitbucket: Remote|string = new Remote('bitbucket', BitbucketConnection); static bitbucket_server: Remote|string = new Remote('bitbucket_server', BitbucketServerConnection); static github: Remote|string = new Remote('github', GithubConnection); static jenkins: Remote|string = new Remote('jenkins', JenkinsConnection); static packagist: Remote|string = new Remote('packagist', PackagistConnection); static jira: Remote|string = new Remote('jira', JiraConnection); constructor( public value: string, public connectionClass: any ) { super(value); } getConnection( auth: Auth, o: any = {} ): Connection { return new this.connectionClass(auth, o); } } export class AuthMethod extends StringType { static basic: AuthMethod|string = new AuthMethod('basic'); static oauth: AuthMethod|string = new AuthMethod('oauth'); static oauth2: AuthMethod|string = new AuthMethod('oauth2'); static token: AuthMethod|string = new AuthMethod('token'); static getKeyName( method: AuthMethod|string ) { return AuthMethod.getName(method, true); } static getSecretName( method: AuthMethod|string ) { return AuthMethod.getName(method, false); } private static getName( method: AuthMethod|string, key: boolean = true ) { switch ( true ) { case method == AuthMethod.basic: return key ? 'username' : 'password' case method == AuthMethod.oauth: return key ? 'key' : 'secret' case method == AuthMethod.oauth2: return key ? 'id' : 'secret' case method == AuthMethod.token: return key ? 'username' : 'token' } } } export class Auth { setDefault( value: boolean ) { if ( value === true ) { Auth.find({ remote : this.remote }); } this.default = value; } name: string; _remote: Remote|string; _method: AuthMethod|string; default: boolean = false; key: string = null; secret: string = null; extra: string = null; constructor( options: any ) { merge(this, options); // this.name = options.name; // this.remote = options.remote; // this.method = options.method; // this.default = options.default; // this.key = options.key; // this.secret = options.secret; } set remote( value ) { this._remote = value.toString(); } get remote(): Remote { return this._remote instanceof Remote ? this._remote : Remote[ this._remote]; } set method( value ) { this._method = value.toString(); } get method(): AuthMethod { return this._method instanceof AuthMethod ? this._method : AuthMethod[ this._method]; } getConnection(): Connection { return this.remote.getConnection(this); } save( create: boolean = false ): Auth { var options: any = {} options.name = this.name; options.remote = this._remote; options.method = this._method; options.default = this.default; options.key = this.key; options.secret = this.secret; options.extra = this.extra; if ( Auth.has(this.name) ) { auths.find({ name : this.name }).assign(options).value(); } else if ( create ) { auths.push(options).value() } return this; } static create( options: any ): Auth { return new Auth(options); } static add( name: string, remote: Remote, method: AuthMethod, key: string, secret?: string ) //: Auth { if ( Auth.has(name) ) { throw new Error(`An auth with name ${name} already exists`) } var auth = Auth.create({ name, remote, method, key, secret }); auths.push(auth).value(); } static find( opts: any ) { return auths.find(opts).value(); } static filter( opts: any ) { return auths.filter(opts).value(); } static query() { return auths; } static has( name: string ): boolean { return typeof auths.find({ name }).value() !== 'undefined' } static get( name: string ): Auth | null { if ( Auth.has(name) ) return Auth.create(auths.find({ name }).value()); } static all(): Auth[] { return auths.toArray().value().map(( auth ) => Auth.create(auth)); } static del( name ) { if ( !Auth.has(name) ) return; auths.remove({ name }).value(); } static getDefault( remote: Remote | string ) { return auths.filter({ default : true, remote : remote.toString() }).value(); } }