Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 4b551a7

Browse files
author
Richard Hua
committed
Revert "Rename sdk.isAuthrnticated method to ensureAuthenticated and make behavior consistent."
We decided that the solution with `isAuthenticated(boolean)` was better because it preserved backward compatibility.
1 parent d8c4cd3 commit 4b551a7

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

cli/script/command-executor.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,13 @@ function login(command: cli.ILoginCommand): Promise<void> {
592592
if (command.accessKey) {
593593
var proxy = getProxy(command.proxy, command.noProxy);
594594
sdk = getSdk(command.accessKey, CLI_HEADERS, command.serverUrl, proxy);
595-
return sdk.ensureAuthenticated()
596-
.catch((err: CodePushError) =>{
597-
throw new Error("Invalid access key.");
598-
})
599-
.then((): void => {
600-
serializeConnectionInfo(command.accessKey, /*preserveAccessKeyOnLogout*/ true, command.serverUrl, command.proxy, command.noProxy);
595+
return sdk.isAuthenticated()
596+
.then((isAuthenticated: boolean): void => {
597+
if (isAuthenticated) {
598+
serializeConnectionInfo(command.accessKey, /*preserveAccessKeyOnLogout*/ true, command.serverUrl, command.proxy, command.noProxy);
599+
} else {
600+
throw new Error("Invalid access key.");
601+
}
601602
});
602603
} else {
603604
return loginWithExternalAuthentication("login", command.serverUrl, command.proxy, command.noProxy);
@@ -617,12 +618,13 @@ function loginWithExternalAuthentication(action: string, serverUrl?: string, pro
617618

618619
sdk = getSdk(accessKey, CLI_HEADERS, serverUrl, getProxy(proxy, noProxy));
619620

620-
return sdk.ensureAuthenticated()
621-
.catch((err: CodePushError) => {
622-
throw new Error("Invalid access key.");
623-
})
624-
.then((): void => {
625-
serializeConnectionInfo(accessKey, /*preserveAccessKeyOnLogout*/ false, serverUrl, proxy, noProxy);
621+
return sdk.isAuthenticated()
622+
.then((isAuthenticated: boolean): void => {
623+
if (isAuthenticated) {
624+
serializeConnectionInfo(accessKey, /*preserveAccessKeyOnLogout*/ false, serverUrl, proxy, noProxy);
625+
} else {
626+
throw new Error("Invalid access key.");
627+
}
626628
});
627629
});
628630
}
@@ -1170,7 +1172,7 @@ export var release = (command: cli.IReleaseCommand): Promise<void> => {
11701172

11711173
return getPackageFilePromise
11721174
.then((file: IPackageFile): Promise<void> => {
1173-
return sdk.ensureAuthenticated()
1175+
return sdk.isAuthenticated(true)
11741176
.then((isAuth: boolean): Promise<void> => {
11751177
return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress);
11761178
})

sdk/script/management-sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class AccountManager {
7676
return this._accessKey;
7777
}
7878

79-
public ensureAuthenticated(): Promise<boolean> {
79+
public isAuthenticated(throwIfUnauthorized?: boolean): Promise<boolean> {
8080
return Promise<any>((resolve, reject, notify) => {
8181
var request: superagent.Request<any> = superagent.get(this._serverUrl + urlEncode `/authenticated`);
8282
if (this._proxy) (<any>request).proxy(this._proxy);
@@ -91,7 +91,7 @@ class AccountManager {
9191

9292
var authenticated: boolean = status === 200;
9393

94-
if (!authenticated ){
94+
if (!authenticated && throwIfUnauthorized){
9595
reject(this.getCodePushError(err, res));
9696
return;
9797
}

sdk/test/management-sdk.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,43 @@ describe("Management SDK", () => {
7070
}
7171
});
7272

73-
it("ensureAuthenticated handles successful auth", (done: MochaDone) => {
73+
it("isAuthenticated handles successful auth", (done: MochaDone) => {
7474
mockReturn(JSON.stringify({ authenticated: true }), 200, {});
75-
manager.ensureAuthenticated()
75+
manager.isAuthenticated()
7676
.done((authenticated: boolean) => {
7777
assert(authenticated, "Should be authenticated");
7878
done();
7979
});
8080
});
8181

82-
it("ensureAuthenticated handles unsuccessful auth", (done: MochaDone) => {
82+
it("isAuthenticated handles unsuccessful auth", (done: MochaDone) => {
8383
mockReturn("Unauthorized", 401, {});
84-
manager.ensureAuthenticated()
84+
manager.isAuthenticated()
8585
.done((authenticated: boolean) => {
86-
assert.fail("ensureAuthenticated should have rejected the promise");
86+
assert(!authenticated, "Should not be authenticated");
87+
done();
88+
});
89+
});
90+
91+
it("isAuthenticated handles unsuccessful auth with promise rejection", (done: MochaDone) => {
92+
mockReturn("Unauthorized", 401, {});
93+
94+
// use optional parameter to ask for rejection of the promise if not authenticated
95+
manager.isAuthenticated(true)
96+
.done((authenticated: boolean) => {
97+
assert.fail("isAuthenticated should have rejected the promise");
8798
done();
8899
}, (err) => {
89100
assert.equal(err.message, "Unauthorized", "Error message should be 'Unauthorized'");
90101
done();
91102
});
92103
});
93104

94-
it("ensureAuthenticated handles unexpected status codes", (done: MochaDone) => {
105+
it("isAuthenticated handles unexpected status codes", (done: MochaDone) => {
95106
mockReturn("Not Found", 404, {});
96-
manager.ensureAuthenticated()
107+
manager.isAuthenticated()
97108
.done((authenticated: boolean) => {
98-
assert.fail("ensureAuthenticated should have rejected the promise");
109+
assert.fail("isAuthenticated should have rejected the promise");
99110
done();
100111
}, (err) => {
101112
assert.equal(err.message, "Not Found", "Error message should be 'Not Found'");

0 commit comments

Comments
 (0)