Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions cli/script/command-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,12 @@ function login(command: cli.ILoginCommand): Promise<void> {
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);
Expand All @@ -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);
});
});
}
Expand Down Expand Up @@ -1172,7 +1170,10 @@ export var release = (command: cli.IReleaseCommand): Promise<void> => {

return getPackageFilePromise
.then((file: IPackageFile): Promise<void> => {
return sdk.release(command.appName, command.deploymentName, file.path, command.appStoreVersion, updateMetadata, uploadProgress)
return sdk.ensureAuthenticated()
.then((isAuth: boolean): Promise<void> => {
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.");
})
Expand Down
11 changes: 8 additions & 3 deletions sdk/script/management-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class AccountManager {
return this._accessKey;
}

public isAuthenticated(): Promise<boolean> {
return Promise<boolean>((resolve, reject, notify) => {
public ensureAuthenticated(): Promise<boolean> {
return Promise<any>((resolve, reject, notify) => {
var request: superagent.Request<any> = superagent.get(this._serverUrl + urlEncode `/authenticated`);
if (this._proxy) (<any>request).proxy(this._proxy);
this.attachCredentials(request);
Expand All @@ -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<AccessKey> {
if (!friendlyName) {
throw new Error("A name must be specified when adding an access key.");
Expand Down
19 changes: 11 additions & 8 deletions sdk/test/management-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
Expand Down