-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AAE-26163 Logout user after 3 login attempts failed, avoiding infinit…
…e loop when an authentication error occured, like when a user machine clock is significantly out of sync
- Loading branch information
Showing
4 changed files
with
148 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { TestBed } from '@angular/core/testing'; | ||
import { OAuthService } from 'angular-oauth2-oidc'; | ||
import { RetryLoginService } from './retry-login.service'; | ||
|
||
describe('RetryLoginService', () => { | ||
let service: RetryLoginService; | ||
let oauthService: jasmine.SpyObj<OAuthService>; | ||
|
||
beforeEach(() => { | ||
const oauthServiceSpy = jasmine.createSpyObj('OAuthService', ['tryLogin']); | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [ | ||
RetryLoginService, | ||
{ provide: OAuthService, useValue: oauthServiceSpy } | ||
] | ||
}); | ||
|
||
service = TestBed.inject(RetryLoginService); | ||
oauthService = TestBed.inject(OAuthService) as jasmine.SpyObj<OAuthService>; | ||
}); | ||
|
||
it('should login successfully on the first attempt', async () => { | ||
oauthService.tryLogin.and.returnValue(Promise.resolve(true)); | ||
|
||
const result = await service.tryToLoginTimes({}); | ||
|
||
expect(result).toBeTrue(); | ||
expect(oauthService.tryLogin).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should retry login up to maxRetries times', async () => { | ||
oauthService.tryLogin.and.returnValues( | ||
Promise.reject(new Error('error')), | ||
Promise.reject(new Error('error')), | ||
Promise.resolve(true) | ||
); | ||
|
||
const result = await service.tryToLoginTimes({}, 2); | ||
|
||
expect(result).toBeTrue(); | ||
expect(oauthService.tryLogin).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should fail after maxRetries attempts providing maxRetries', async () => { | ||
oauthService.tryLogin.and.rejectWith('error'); | ||
|
||
try { | ||
await service.tryToLoginTimes({}, 2); | ||
fail('Expected to throw an error'); | ||
} catch (error) { | ||
expect(error).toBe('error'); | ||
expect(oauthService.tryLogin).toHaveBeenCalledTimes(2); | ||
} | ||
}); | ||
|
||
it('should fail after maxRetries attempts using default maxRetries value', async () => { | ||
oauthService.tryLogin.and.rejectWith('error'); | ||
|
||
try { | ||
await service.tryToLoginTimes({}); | ||
fail('Expected to throw an error'); | ||
} catch (error) { | ||
expect(error).toBe('error'); | ||
expect(oauthService.tryLogin).toHaveBeenCalledTimes(3); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/*! | ||
* @license | ||
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { inject, Injectable } from '@angular/core'; | ||
import { LoginOptions, OAuthService } from 'angular-oauth2-oidc'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class RetryLoginService { | ||
|
||
private oauthService = inject(OAuthService); | ||
|
||
/** | ||
* Attempts to log in a specified number of times if the initial login attempt fails. | ||
* | ||
* @param loginOptions - The options to be used for the login attempt. | ||
* @param maxLoginAttempts - The maximum number of login attempts. Defaults to 3. | ||
* @returns A promise that resolves to `true` if the login is successful, or rejects with an error if all attempts fail. | ||
*/ | ||
tryToLoginTimes(loginOptions: LoginOptions, maxLoginAttempts = 3): Promise<boolean> { | ||
let retryCount = 0; | ||
const maxRetries = maxLoginAttempts - 1; | ||
|
||
const attemptLogin = (): Promise<boolean> => this.oauthService.tryLogin({ ...loginOptions }) | ||
.catch((error) => { | ||
if (retryCount < maxRetries) { | ||
console.error(`Login attempt ${retryCount + 1} of ${maxLoginAttempts} failed. ${retryCount < maxLoginAttempts - 1 ? 'Retrying...' : ''}`); | ||
retryCount++; | ||
return attemptLogin(); | ||
} else { | ||
throw new Error(`Login failed after ${maxLoginAttempts} attempts. ${error.message}`); | ||
} | ||
}); | ||
|
||
return attemptLogin(); | ||
} | ||
|
||
} |