feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { DOCUMENT } from '../../dom';
|
||||
import { inject, Injectable } from 'injection-js';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { AuthStateService } from '../../auth-state/auth-state.service';
|
||||
import { OpenIdConfiguration } from '../../config/openid-configuration';
|
||||
import { LoggerService } from '../../logging/logger.service';
|
||||
import { StateValidationResult } from '../../validation/state-validation-result';
|
||||
import { StateValidationService } from '../../validation/state-validation.service';
|
||||
import { CallbackContext } from '../callback-context';
|
||||
import { ResetAuthDataService } from '../reset-auth-data.service';
|
||||
|
||||
@Injectable()
|
||||
export class StateValidationCallbackHandlerService {
|
||||
private readonly loggerService = inject(LoggerService);
|
||||
|
||||
private readonly stateValidationService = inject(StateValidationService);
|
||||
|
||||
private readonly authStateService = inject(AuthStateService);
|
||||
|
||||
private readonly resetAuthDataService = inject(ResetAuthDataService);
|
||||
|
||||
private readonly document = inject(DOCUMENT);
|
||||
|
||||
// STEP 4 All flows
|
||||
callbackStateValidation(
|
||||
callbackContext: CallbackContext,
|
||||
configuration: OpenIdConfiguration,
|
||||
allConfigs: OpenIdConfiguration[]
|
||||
): Observable<CallbackContext> {
|
||||
return this.stateValidationService
|
||||
.getValidatedStateResult(callbackContext, configuration)
|
||||
.pipe(
|
||||
map((validationResult: StateValidationResult) => {
|
||||
callbackContext.validationResult = validationResult;
|
||||
|
||||
if (validationResult.authResponseIsValid) {
|
||||
this.authStateService.setAuthorizationData(
|
||||
validationResult.accessToken,
|
||||
callbackContext.authResult,
|
||||
configuration,
|
||||
allConfigs
|
||||
);
|
||||
|
||||
return callbackContext;
|
||||
} else {
|
||||
const errorMessage = `authorizedCallback, token(s) validation failed, resetting. Hash: ${this.document.location.hash}`;
|
||||
|
||||
this.loggerService.logWarning(configuration, errorMessage);
|
||||
this.resetAuthDataService.resetAuthorizationData(
|
||||
configuration,
|
||||
allConfigs
|
||||
);
|
||||
this.publishUnauthorizedState(
|
||||
callbackContext.validationResult,
|
||||
callbackContext.isRenewProcess
|
||||
);
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private publishUnauthorizedState(
|
||||
stateValidationResult: StateValidationResult,
|
||||
isRenewProcess: boolean
|
||||
): void {
|
||||
this.authStateService.updateAndPublishAuthState({
|
||||
isAuthenticated: false,
|
||||
validationResult: stateValidationResult.state,
|
||||
isRenewProcess,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user