diff --git a/cli/script/command-executor.ts b/cli/script/command-executor.ts index 857a190f..86fd8189 100644 --- a/cli/script/command-executor.ts +++ b/cli/script/command-executor.ts @@ -592,13 +592,12 @@ function login(command: cli.ILoginCommand): Promise { if (command.accessKey) { var proxy = getProxy(command.proxy, command.noProxy); sdk = getSdk(command.accessKey, CLI_HEADERS, command.serverUrl, proxy); - return sdk.isAuthenticated() - .then((isAuthenticated: boolean): void => { - if (isAuthenticated) { - serializeConnectionInfo(command.accessKey, /*preserveAccessKeyOnLogout*/ true, command.serverUrl, command.proxy, command.noProxy); - } else { - throw new Error("Invalid access key."); - } + return sdk.ensureAuthenticated() + .catch((err: CodePushError) =>{ + throw new Error("Invalid access key."); + }) + .then((): void => { + serializeConnectionInfo(command.accessKey, /*preserveAccessKeyOnLogout*/ true, command.serverUrl, command.proxy, command.noProxy); }); } else { return loginWithExternalAuthentication("login", command.serverUrl, command.proxy, command.noProxy); @@ -618,13 +617,12 @@ function loginWithExternalAuthentication(action: string, serverUrl?: string, pro sdk = getSdk(accessKey, CLI_HEADERS, serverUrl, getProxy(proxy, noProxy)); - return sdk.isAuthenticated() - .then((isAuthenticated: boolean): void => { - if (isAuthenticated) { - serializeConnectionInfo(accessKey, /*preserveAccessKeyOnLogout*/ false, serverUrl, proxy, noProxy); - } else { - throw new Error("Invalid access key."); - } + return sdk.ensureAuthenticated() + .catch((err: CodePushError) => { + throw new Error("Invalid access key."); + }) + .then((): void => { + serializeConnectionInfo(accessKey, /*preserveAccessKeyOnLogout*/ false, serverUrl, proxy, noProxy); }); }); } @@ -1172,7 +1170,10 @@ export var release = (command: cli.IReleaseCommand): Promise => { return getPackageFilePromise .then((file: IPackageFile): Promise => { - return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress) + return sdk.ensureAuthenticated() + .then((isAuth: boolean): Promise => { + return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress); + }) .then((): void => { log("Successfully released an update containing the \"" + command.package + "\" " + (isSingleFilePackage ? "file" : "directory") + " to the \"" + command.deploymentName + "\" deployment of the \"" + command.appName + "\" app."); }) diff --git a/sdk/script/management-sdk.ts b/sdk/script/management-sdk.ts index c913a794..7fb22691 100644 --- a/sdk/script/management-sdk.ts +++ b/sdk/script/management-sdk.ts @@ -76,8 +76,8 @@ class AccountManager { return this._accessKey; } - public isAuthenticated(): Promise { - return Promise((resolve, reject, notify) => { + public ensureAuthenticated(): Promise { + return Promise((resolve, reject, notify) => { var request: superagent.Request = superagent.get(this._serverUrl + urlEncode `/authenticated`); if (this._proxy) (request).proxy(this._proxy); this.attachCredentials(request); @@ -91,11 +91,16 @@ class AccountManager { var authenticated: boolean = status === 200; + if (!authenticated ){ + reject(this.getCodePushError(err, res)); + return; + } + resolve(authenticated); }); }); } - + public addAccessKey(friendlyName: string, ttl?: number): Promise { if (!friendlyName) { throw new Error("A name must be specified when adding an access key."); diff --git a/sdk/test/management-sdk.ts b/sdk/test/management-sdk.ts index 96cc35bc..9f4fd78f 100644 --- a/sdk/test/management-sdk.ts +++ b/sdk/test/management-sdk.ts @@ -70,29 +70,32 @@ describe("Management SDK", () => { } }); - it("isAuthenticated handles successful auth", (done: MochaDone) => { + it("ensureAuthenticated handles successful auth", (done: MochaDone) => { mockReturn(JSON.stringify({ authenticated: true }), 200, {}); - manager.isAuthenticated() + manager.ensureAuthenticated() .done((authenticated: boolean) => { assert(authenticated, "Should be authenticated"); done(); }); }); - it("isAuthenticated handles unsuccessful auth", (done: MochaDone) => { + it("ensureAuthenticated handles unsuccessful auth", (done: MochaDone) => { mockReturn("Unauthorized", 401, {}); - manager.isAuthenticated() + manager.ensureAuthenticated() .done((authenticated: boolean) => { - assert(!authenticated, "Should not be authenticated"); + assert.fail("ensureAuthenticated should have rejected the promise"); + done(); + }, (err) => { + assert.equal(err.message, "Unauthorized", "Error message should be 'Unauthorized'"); done(); }); }); - it("isAuthenticated handles unexpected status codes", (done: MochaDone) => { + it("ensureAuthenticated handles unexpected status codes", (done: MochaDone) => { mockReturn("Not Found", 404, {}); - manager.isAuthenticated() + manager.ensureAuthenticated() .done((authenticated: boolean) => { - assert.fail("isAuthenticated should have rejected the promise"); + assert.fail("ensureAuthenticated should have rejected the promise"); done(); }, (err) => { assert.equal(err.message, "Not Found", "Error message should be 'Not Found'");