protected getCredentials()

in app/lib/tfcommand.ts [353:410]


	protected getCredentials(serviceUrl: string, useCredStore: boolean = true): Promise<BasicCredentialHandler> {
		return Promise.all([
			this.commandArgs.authType.val(),
			this.commandArgs.token.val(true),
			this.commandArgs.username.val(true),
			this.commandArgs.password.val(true),
		]).then(values => {
			const [authType, token, username, password] = values;
			if (username && password) {
				return getBasicHandler(username, password);
			} else {
				if (token) {
					return getBasicHandler("OAuth", token);
				} else {
					let getCredentialPromise;
					if (useCredStore) {
						getCredentialPromise = getCredentialStore("tfx").getCredential(serviceUrl, "allusers");
					} else {
						getCredentialPromise = Promise.reject("not using cred store.");
					}
					return getCredentialPromise
						.then((credString: string) => {
							if (credString.length <= 6) {
								throw "Could not get credentials from credential store.";
							}
							if (credString.substr(0, 3) === "pat") {
								return getBasicHandler("OAuth", credString.substr(4));
							} else if (credString.substr(0, 5) === "basic") {
								let rest = credString.substr(6);
								let unpwDividerIndex = rest.indexOf(":");
								let username = rest.substr(0, unpwDividerIndex);
								let password = rest.substr(unpwDividerIndex + 1);
								if (username && password) {
									return getBasicHandler(username, password);
								} else {
									throw "Could not get credentials from credential store.";
								}
							}
						})
						.catch(() => {
							if (authType.toLowerCase() === "pat") {
								return this.commandArgs.token.val().then(token => {
									return getBasicHandler("OAuth", token);
								});
							} else if (authType.toLowerCase() === "basic") {
								return this.commandArgs.username.val().then(username => {
									return this.commandArgs.password.val().then(password => {
										return getBasicHandler(username, password);
									});
								});
							} else {
								throw new Error("Unsupported auth type. Currently, 'pat' and 'basic' auth are supported.");
							}
						});
				}
			}
		});
	}