Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Microsoft login #1124

Merged
merged 1 commit into from
Jan 9, 2024
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
13 changes: 13 additions & 0 deletions WebClient/src/components/externalLogins/externalLogins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ describe("ExternalLoginsComponent", () => {
let activeModal = TestBed.inject(NgbActiveModal);
spyOn(activeModal, "close");

spyOn(component.microsoftApp, "initialize").and.returnValue(Promise.resolve());

const token = "someToken";
let authResult = {
idToken: token,
Expand All @@ -334,6 +336,7 @@ describe("ExternalLoginsComponent", () => {
await fixture.whenStable();
fixture.detectChanges();

expect(component.microsoftApp.initialize).toHaveBeenCalled();
expect(component.microsoftApp.loginPopup).toHaveBeenCalledWith({ scopes: ["openid", "email"] });
expect(authenticationService.logInWithAssertion).toHaveBeenCalledWith("urn:ietf:params:oauth:grant-type:microsoft_identity_token", token, undefined);
expect(activeModal.close).toHaveBeenCalled();
Expand All @@ -350,6 +353,7 @@ describe("ExternalLoginsComponent", () => {
let activeModal = TestBed.inject(NgbActiveModal);
spyOn(activeModal, "close");

spyOn(component.microsoftApp, "initialize").and.returnValue(Promise.resolve());
spyOn(component.microsoftApp, "loginPopup").and.returnValue(Promise.reject(""));

let buttons = fixture.debugElement.queryAll(By.css("button"));
Expand All @@ -362,6 +366,7 @@ describe("ExternalLoginsComponent", () => {
await fixture.whenStable();
fixture.detectChanges();

expect(component.microsoftApp.initialize).toHaveBeenCalled();
expect(component.microsoftApp.loginPopup).toHaveBeenCalledWith({ scopes: ["openid", "email"] });
expect(authenticationService.logInWithAssertion).not.toHaveBeenCalled();
expect(activeModal.close).not.toHaveBeenCalled();
Expand All @@ -378,6 +383,8 @@ describe("ExternalLoginsComponent", () => {
let activeModal = TestBed.inject(NgbActiveModal);
spyOn(activeModal, "close");

spyOn(component.microsoftApp, "initialize").and.returnValue(Promise.resolve());

const token = "someToken";
let authResult = {
idToken: token,
Expand All @@ -394,6 +401,7 @@ describe("ExternalLoginsComponent", () => {
await fixture.whenStable();
fixture.detectChanges();

expect(component.microsoftApp.initialize).toHaveBeenCalled();
expect(component.microsoftApp.loginPopup).toHaveBeenCalledWith({ scopes: ["openid", "email"] });
expect(authenticationService.logInWithAssertion).toHaveBeenCalledWith("urn:ietf:params:oauth:grant-type:microsoft_identity_token", token, undefined);
expect(activeModal.close).not.toHaveBeenCalled();
Expand All @@ -410,6 +418,7 @@ describe("ExternalLoginsComponent", () => {
let activeModal = TestBed.inject(NgbActiveModal);
spyOn(activeModal, "close");

spyOn(component.microsoftApp, "initialize").and.returnValue(Promise.resolve());
spyOn(component.microsoftApp, "loginPopup").and.returnValue(Promise.reject("user_cancelled:User closed the popup window window and cancelled the flow"));

let buttons = fixture.debugElement.queryAll(By.css("button"));
Expand All @@ -422,6 +431,7 @@ describe("ExternalLoginsComponent", () => {
await fixture.whenStable();
fixture.detectChanges();

expect(component.microsoftApp.initialize).toHaveBeenCalled();
expect(component.microsoftApp.loginPopup).toHaveBeenCalledWith({ scopes: ["openid", "email"] });
expect(authenticationService.logInWithAssertion).not.toHaveBeenCalled();
expect(activeModal.close).not.toHaveBeenCalled();
Expand All @@ -443,6 +453,8 @@ describe("ExternalLoginsComponent", () => {
let errorResponse: IErrorResponse = { error: "account_selection_required" };
spyOn(authenticationService, "logInWithAssertion").and.returnValue(Promise.reject({ json: () => errorResponse }));

spyOn(component.microsoftApp, "initialize").and.returnValue(Promise.resolve());

// Using Microsoft login since it's easy to mock, but they should all apply equally
const token = "someToken";
let authResult = {
Expand Down Expand Up @@ -824,6 +836,7 @@ describe("ExternalLoginsComponent", () => {
}
}

spyOn(component.microsoftApp, "initialize").and.returnValue(Promise.resolve());
spyOn(component.microsoftApp, "loginPopup").and.returnValue(Promise.resolve(authResult));
expect(button).toBeDefined();
button.nativeElement.click();
Expand Down
3 changes: 2 additions & 1 deletion WebClient/src/components/externalLogins/externalLogins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ export class ExternalLoginsComponent implements OnInit, AfterViewInit {

// There is no signal when the user closes the popup. The promise just never resoles.
// See: https://github.com/AzureAD/microsoft-authentication-library-for-js/issues/129
this.microsoftApp.loginPopup({ scopes: ["openid", "email"] })
this.microsoftApp.initialize()
.then(() => this.microsoftApp.loginPopup({ scopes: ["openid", "email"] }))
.then(loginResponse => this.logIn("urn:ietf:params:oauth:grant-type:microsoft_identity_token", loginResponse.idToken))
.catch((error: string) => {
if (error && error.startsWith("user_cancelled:")) {
Expand Down