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

Commit d8c4cd3

Browse files
authored
Merge pull request #327 from Microsoft/fix-check-authentication
Fix check authentication on releases upload.
2 parents edf726e + 9b2c9a8 commit d8c4cd3

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

cli/script/command-executor.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,12 @@ 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.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-
}
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);
602601
});
603602
} else {
604603
return loginWithExternalAuthentication("login", command.serverUrl, command.proxy, command.noProxy);
@@ -618,13 +617,12 @@ function loginWithExternalAuthentication(action: string, serverUrl?: string, pro
618617

619618
sdk = getSdk(accessKey, CLI_HEADERS, serverUrl, getProxy(proxy, noProxy));
620619

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-
}
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);
628626
});
629627
});
630628
}
@@ -1172,7 +1170,10 @@ export var release = (command: cli.IReleaseCommand): Promise<void> => {
11721170

11731171
return getPackageFilePromise
11741172
.then((file: IPackageFile): Promise<void> => {
1175-
return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress)
1173+
return sdk.ensureAuthenticated()
1174+
.then((isAuth: boolean): Promise<void> => {
1175+
return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress);
1176+
})
11761177
.then((): void => {
11771178
log("Successfully released an update containing the \"" + command.package + "\" " + (isSingleFilePackage ? "file" : "directory") + " to the \"" + command.deploymentName + "\" deployment of the \"" + command.appName + "\" app.");
11781179
})

sdk/script/management-sdk.ts

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

79-
public isAuthenticated(): Promise<boolean> {
80-
return Promise<boolean>((resolve, reject, notify) => {
79+
public ensureAuthenticated(): Promise<boolean> {
80+
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);
8383
this.attachCredentials(request);
@@ -91,11 +91,16 @@ class AccountManager {
9191

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

94+
if (!authenticated ){
95+
reject(this.getCodePushError(err, res));
96+
return;
97+
}
98+
9499
resolve(authenticated);
95100
});
96101
});
97102
}
98-
103+
99104
public addAccessKey(friendlyName: string, ttl?: number): Promise<AccessKey> {
100105
if (!friendlyName) {
101106
throw new Error("A name must be specified when adding an access key.");

sdk/test/management-sdk.ts

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

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

82-
it("isAuthenticated handles unsuccessful auth", (done: MochaDone) => {
82+
it("ensureAuthenticated handles unsuccessful auth", (done: MochaDone) => {
8383
mockReturn("Unauthorized", 401, {});
84-
manager.isAuthenticated()
84+
manager.ensureAuthenticated()
8585
.done((authenticated: boolean) => {
86-
assert(!authenticated, "Should not be authenticated");
86+
assert.fail("ensureAuthenticated should have rejected the promise");
87+
done();
88+
}, (err) => {
89+
assert.equal(err.message, "Unauthorized", "Error message should be 'Unauthorized'");
8790
done();
8891
});
8992
});
9093

91-
it("isAuthenticated handles unexpected status codes", (done: MochaDone) => {
94+
it("ensureAuthenticated handles unexpected status codes", (done: MochaDone) => {
9295
mockReturn("Not Found", 404, {});
93-
manager.isAuthenticated()
96+
manager.ensureAuthenticated()
9497
.done((authenticated: boolean) => {
95-
assert.fail("isAuthenticated should have rejected the promise");
98+
assert.fail("ensureAuthenticated should have rejected the promise");
9699
done();
97100
}, (err) => {
98101
assert.equal(err.message, "Not Found", "Error message should be 'Not Found'");

0 commit comments

Comments
 (0)