Incluido angular-oauth2-oidc en app-component

This commit is contained in:
2021-03-15 19:24:41 +01:00
parent 43902b62f0
commit fb4ebbb1f8
8 changed files with 74 additions and 89 deletions

View File

@@ -1,37 +1,40 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { AuthService, AuthState } from './services/auth.service';
import { OidcSecurityService } from 'angular-auth-oidc-client';
@Component({
// tslint:disable-next-line:component-selector
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
showOverlay = true;
constructor(
private route: ActivatedRoute,
private authService: AuthService,
private oidcSecurityService: OidcSecurityService,
// private authService: AuthService,
private router: Router
) { }
async ngOnInit(): Promise<void> {
console.log('AppComponent.ngInit()');
const chatLoginToken = await this.waitChatToken();
const authState = await this.authService.configureAndTryLogin(chatLoginToken);
this.oidcSecurityService.checkAuth().subscribe((auth) => console.log('is authenticated', auth));
if (authState !== AuthState.ChatLoggingInProcess) {
// const chatLoginToken = await this.waitChatToken();
// const authState = await this.authService.configureAndTryLogin(chatLoginToken);
// if (authState !== AuthState.ChatLoggingInProcess) {
this.showOverlay = false;
}
// }
}
ngOnDestroy(): void {
// TODO no se ejecuta
console.log('AppComponent.ngDestroy()');
this.authService.stopAutomaticRefresh();
// this.authService.stopAutomaticRefresh();
}
private async waitChatToken(): Promise<string> {

View File

@@ -20,6 +20,7 @@ import { MatDatepickerModule } from '@angular/material/datepicker';
import { NgxMatMomentModule } from '@angular-material-components/moment-adapter';
import { AppRouterModule } from './router.module';
import { AvailablePlacesPipe } from './pipes/available-places.pipe';
import { AuthConfigModule } from './auth/auth-config.module';
@NgModule({
imports: [
@@ -32,7 +33,8 @@ import { AvailablePlacesPipe } from './pipes/available-places.pipe';
MaterialModule,
MatDatepickerModule,
NgxMatDatetimePickerModule,
NgxMatMomentModule
NgxMatMomentModule,
AuthConfigModule
],
declarations: [
AppComponent,

View File

@@ -1,49 +0,0 @@
import { AuthConfig } from 'angular-oauth2-oidc';
import { useSilentRefreshForCodeFlow } from '../flags';
export const authConfig: AuthConfig = {
// issuer: 'http://localhost:8081/auth/realms/fosil',
issuer: 'https://auth.fosil.eu/auth/realms/test',
// URL of the SPA to redirect the user to after login
redirectUri:
window.location.origin +
(localStorage.getItem('useHashLocationStrategy') === 'true'
? '/#/index.html'
: '/index.html'),
// The SPA's id. The SPA is registerd with this id at the auth-server
// clientId: 'server.code',
clientId: 'okupamicoche-frontend-angular',
responseType: 'code',
// set the scope for the permissions the client should request
// The first four are defined by OIDC.
// Important: Request offline_access to get a refresh token
// The api scope is a usecase specific one
scope: useSilentRefreshForCodeFlow
? 'openid profile email'
: 'openid profile email offline_access',
// ^^ Please note that offline_access is not needed for silent refresh
// At least when using idsvr, this even prevents silent refresh
// as idsvr ALWAYS prompts the user for consent when this scope is
// requested
// This is needed for silent refresh (refreshing tokens w/o a refresh_token)
// **AND** for logging in with a popup
silentRefreshRedirectUri: `${window.location.origin}/silent-refresh.html`,
useSilentRefresh: useSilentRefreshForCodeFlow,
showDebugInformation: false,
sessionChecksEnabled: true,
sessionCheckIntervall: 10000,
timeoutFactor: 0.75,
// disablePKCI: true,
clearHashAfterLogin: false
};

View File

@@ -0,0 +1,32 @@
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { AuthModule, OidcConfigService } from 'angular-auth-oidc-client';
export function configureAuth(oidcConfigService: OidcConfigService): () => Promise<any> {
return () =>
oidcConfigService.withConfig({
stsServer: 'https://auth.fosil.eu/auth/realms/test',
redirectUrl: window.location.origin,
postLogoutRedirectUri: window.location.origin,
clientId: 'okupamicoche-frontend-angular',
scope: 'openid profile offline_access',
responseType: 'code',
silentRenew: true,
useRefreshToken: true,
renewTimeBeforeTokenExpiresInSeconds: 30,
});
}
@NgModule({
imports: [AuthModule.forRoot()],
exports: [AuthModule],
providers: [
OidcConfigService,
{
provide: APP_INITIALIZER,
useFactory: configureAuth,
deps: [OidcConfigService],
multi: true,
},
],
})
export class AuthConfigModule {}

View File

@@ -32,10 +32,6 @@ export class AuthService {
return User.fromClaims(claims);
}
stopAutomaticRefresh(): void {
// this.oauthService.stopAutomaticRefresh();
}
async configureAndTryLogin(chatLoginToken: string): Promise<AuthState> {
this.configureOauth();

View File

@@ -1,27 +0,0 @@
<html>
<body>
<script>
var checks = [
/[\?|&|#]code=/,
/[\?|&|#]error=/,
/[\?|&|#]token=/,
/[\?|&|#]id_token=/
];
function isResponse(str) {
var count = 0;
if (!str) return false;
for (var i = 0; i < checks.length; i++) {
if (str.match(checks[i])) return true;
}
return false;
}
var message = isResponse(location.hash)
? location.hash
: '#' + location.search;
(window.opener || window.parent).postMessage(message, location.origin);
</script>
</body>
</html>