'use strict' ; import * as vscode from 'vscode' ; import { Logger } from '../util/logger' ; import { Constants } from '../constants' ; import { IAppManager , IConfig } from '../spgo' ; import { CredentialDao } from '../dao/credentialDao' ; import { RequestHelper } from '../util/requestHelper' ; export class AuthenticationService { static verifyCredentials ( appManager , config , payload ? ) { appManager = appManager || vscode . window . spgo ; try { if ( ! appManager . credentials ) { appManager . credentials = { } ; if ( config . authenticationType && config . authenticationType === Constants . SECURITY_ADDIN ) { return getClientId ( appManager ) . then ( ( mgr ) => getClientSecret ( mgr ) ) . then ( ( mgr ) => getRealm ( mgr ) ) . then ( ( mgr ) => verify ( mgr ) ) . then ( ( mgr ) => processNextCommand ( mgr ) ) ; } else { return getUserName ( appManager ) . then ( ( mgr ) => getPassword ( mgr ) ) . then ( ( mgr ) => verify ( mgr ) ) . then ( ( mgr ) => processNextCommand ( mgr ) ) ; } } else { return processNextCommand ( appManager ) ; } } catch ( err ) { Logger . outputError ( err , vscode . window . spgo . outputChannel ) ; appManager . credentials = { } ; } function getClientId ( appManager ) { let options = { ignoreFocusOut : true , placeHolder : '<client Id>' , value : appManager . credentials . clientId || '' , prompt : 'Please enter the Client Id for this extension' , } ; return vscode . window . showInputBox ( options ) . then ( result => { appManager . credentials . clientId = result || appManager . credentials . clientId || '' ; if ( ! appManager . credentials . clientId ) { appManager . credentials = null ; } ; return appManager ; } ) ; } function getClientSecret ( appManager ) { return new Promise ( ( resolve , reject ) => { let options = { ignoreFocusOut : true , password : true , placeHolder : '<client secret>' , value : appManager . credentials . clientSecret || '' , prompt : 'Please enter the Client Secret for this extension' , } ; vscode . window . showInputBox ( options ) . then ( result => { appManager . credentials . clientSecret = result || appManager . credentials . clientSecret || '' ; if ( ! appManager . credentials . clientSecret ) { reject ( 'No Client Secret' ) ; appManager . credentials = null ; } ; resolve ( appManager ) ; } ) ; } ) ; } function getRealm ( appManager ) { return new Promise ( ( resolve ) => { let options = { ignoreFocusOut : true , placeHolder : '<realm>' , value : appManager . credentials . realm || '' , prompt : '(Optional) Please enter the Realm for this extension' , } ; vscode . window . showInputBox ( options ) . then ( result => { appManager . credentials . realm = result || appManager . credentials . realm || '' ; resolve ( appManager ) ; } ) ; } ) ; } function getUserName ( appManager ) { return new Promise ( ( resolve , reject ) => { let options = { ignoreFocusOut : true , placeHolder : 'user@domain.com [or domain\\user]' , value : appManager . credentials . username || '' , prompt : 'Please enter your SharePoint username' , } ; vscode . window . showInputBox ( options ) . then ( result => { appManager . credentials . username = result || appManager . credentials . username || '' ; if ( ! appManager . credentials . username ) { reject ( 'No Username' ) ; appManager . credentials = null ; } ; resolve ( appManager ) ; } ) ; } ) ; } function getPassword ( appManager ) { return new Promise ( ( resolve , reject ) => { let options = { ignoreFocusOut : true , password : true , value : appManager . credentials . password || '' , placeHolder : 'password' , prompt : 'Please enter your SharePoint password' , } ; return vscode . window . showInputBox ( options ) . then ( ( result ) => { appManager . credentials . password = result || appManager . credentials . password || '' ; if ( ! appManager . credentials . password ) { reject ( 'No Password' ) ; appManager . credentials = null ; } ; resolve ( appManager ) ; } ) ; } ) ; } function verify ( appManager ) { return new Promise ( ( resolve , reject ) => { let spr = RequestHelper . createRequest ( appManager , config ) ; spr . requestDigest ( config . sharePointSiteUrl ) . then ( ( ) => { if ( config . storeCredentials ) { CredentialDao . setCredentials ( config . sharePointSiteUrl , appManager . credentials ) ; } resolve ( appManager ) ; } , err => { appManager . credentials = null ; reject ( err ) ; } ) ; } ) ; } function processNextCommand ( appManager ) { return new Promise ( ( resolve , reject ) => { if ( appManager && appManager . credentials ) { resolve ( payload ) ; } else { reject ( 'null credentials.' ) ; } } ) ; } } }