Deleted angular-oauth2-oidc and shared module

This commit is contained in:
2021-03-15 18:46:30 +01:00
parent e29a5c8654
commit 43902b62f0
14 changed files with 17 additions and 346 deletions

13
package-lock.json generated
View File

@@ -3798,14 +3798,6 @@
"integrity": "sha1-l6ERlkmyEa0zaR2fn0hqjsn74KM=",
"dev": true
},
"angular-oauth2-oidc": {
"version": "9.2.2",
"resolved": "https://registry.npmjs.org/angular-oauth2-oidc/-/angular-oauth2-oidc-9.2.2.tgz",
"integrity": "sha512-aMQXeujzhubvxGw3ujw9FGwTC+L8m7CXnzVntpNJRkJsgMiuZFrXzgeiG87tvAE61J+PlOVIb/UkJjYDgDVU6Q==",
"requires": {
"js-sha256": "^0.9.0"
}
},
"ansi-colors": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.4.tgz",
@@ -8483,11 +8475,6 @@
}
}
},
"js-sha256": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz",
"integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA=="
},
"js-tokens": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",

View File

@@ -23,7 +23,6 @@
"@angular/platform-browser": "~11.1.1",
"@angular/platform-browser-dynamic": "~11.1.1",
"@angular/router": "~11.1.1",
"angular-oauth2-oidc": "^9.2.2",
"moment": "^2.29.1",
"rxjs": "~6.6.0",
"tslib": "^1.14.1",

View File

@@ -1,15 +1,11 @@
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgxMatDatetimePickerModule, NgxMatTimepickerModule } from '@angular-material-components/datetime-picker';
import { NgxMatDatetimePickerModule } from '@angular-material-components/datetime-picker';
import { AppComponent } from './app.component';
import { USER_API_URL, TRAVEL_API_URL } from './app.config';
import { SharedModule } from './shared/shared.module';
// import { useHash } from '../flags';
import { MaterialModule } from './material/material.module';
import { HomeComponent } from './pages/home/home.component';
@@ -25,24 +21,12 @@ import { NgxMatMomentModule } from '@angular-material-components/moment-adapter'
import { AppRouterModule } from './router.module';
import { AvailablePlacesPipe } from './pipes/available-places.pipe';
// We need a factory, since localStorage is not available during AOT build time.
export function storageFactory(): OAuthStorage {
return localStorage;
}
@NgModule({
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
SharedModule.forRoot(),
OAuthModule.forRoot({
resourceServer: {
allowedUrls: [USER_API_URL, TRAVEL_API_URL],
sendAccessToken: true,
},
}),
BrowserAnimationsModule,
AppRouterModule,
MaterialModule,
@@ -62,7 +46,6 @@ export function storageFactory(): OAuthStorage {
AvailablePlacesPipe
],
providers: [
{ provide: OAuthStorage, useFactory: storageFactory }
// (useHash) ? { provide: LocationStrategy, useClass: HashLocationStrategy } : [],
// {provide: AuthConfig, useValue: authConfig },
// { provide: OAuthStorage, useValue: localStorage },

View File

@@ -1,8 +1,5 @@
import { Injectable } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { BehaviorSubject } from 'rxjs';
import { filter } from 'rxjs/operators';
import { authConfig } from '../auth.config';
import { User } from '../entities/user';
import { ChatService } from './chat.service';
@@ -20,23 +17,25 @@ export class AuthService {
public logged$ = this.loggedSubject.asObservable();
constructor(
private oauthService: OAuthService,
private chatService: ChatService
) { }
login(): void {
this.oauthService.initLoginFlow();
}
logout(): void {
this.oauthService.revokeTokenAndLogout();
}
currentUser(): User {
const claims = this.oauthService.getIdentityClaims();
// const claims = this.oauthService.getIdentityClaims();
const claims = null;
return User.fromClaims(claims);
}
stopAutomaticRefresh(): void {
// this.oauthService.stopAutomaticRefresh();
}
async configureAndTryLogin(chatLoginToken: string): Promise<AuthState> {
this.configureOauth();
@@ -67,43 +66,11 @@ export class AuthService {
return AuthState.NotLogged;
}
stopAutomaticRefresh(): void {
this.oauthService.stopAutomaticRefresh();
}
private configureOauth(): void {
this.oauthService.configure(authConfig);
this.oauthService.setupAutomaticSilentRefresh();
this.oauthService.events
.pipe(filter((e) => e.type === 'token_received'))
.subscribe((_) => {
console.log('auth.service token_received state=', this.oauthService.state);
});
}
private async tryOauthLogin(): Promise<boolean> {
await this.oauthService.loadDiscoveryDocumentAndTryLogin();
if (this.oauthService.getIdentityClaims() != null) {
console.log('IDENTITY CLAIMS');
console.log(this.oauthService.getIdentityClaims());
if (this.oauthService.hasValidIdToken() && this.oauthService.hasValidAccessToken()) {
console.log('NO NEED FOR TOKEN REFRESH');
return true;
}
else {
try {
await this.oauthService.refreshToken();
console.log('TOKEN REFRESHED');
return true;
}
catch (e) {
console.log('ERROR REFRESHING TOKEN: ' + e);
return false;
}
}
}
console.log('NOT LOGGED!');
return false;
}
}

View File

@@ -17,7 +17,7 @@ export class ChatService {
login(loginToken: string): Promise<void | object> {
console.log('MATRIXCS');
console.log(matrixcs);
// console.log(matrixcs);
// console.log('SDK');
// console.log(sdk);
return this.http.post(
@@ -31,15 +31,15 @@ export class ChatService {
console.log('LOGGED TO CHAT');
console.log(res);
const client = matrixcs.createClient({
baseUrl: "http://localhost:8008",
accessToken: res.access_token,
userId: res.user_id
});
// const client = matrixcs.createClient({
// baseUrl: "http://localhost:8008",
// accessToken: res.access_token,
// userId: res.user_id
// });
client.publicRooms(function (err, data) {
console.log("Public Rooms: %s", JSON.stringify(data));
});
// client.publicRooms(function (err, data) {
// console.log("Public Rooms: %s", JSON.stringify(data));
// });
});
}
}

View File

@@ -1,20 +0,0 @@
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private oauthService: OAuthService) {}
canActivate(): boolean {
if (
this.oauthService.hasValidAccessToken() &&
this.oauthService.hasValidIdToken()
) {
return true;
} else {
this.router.navigate(['/home', { login: true }]);
return false;
}
}
}

View File

@@ -1,38 +0,0 @@
import { Component, Input, OnInit, OnChanges } from '@angular/core';
@Component({
selector: 'date-component',
template: `
<div>{{ day }}.{{ month }}.{{ year }} {{ hour }}:{{ minute }}</div>
`
})
export class DateComponent implements OnInit, OnChanges {
@Input() date: string;
day;
month;
year;
hour;
minute;
constructor() {
console.debug('ctrl');
}
ngOnInit() {}
ngOnChanges(change) {
// if(change.date) { ... }
console.debug('change', change);
let date = new Date(this.date);
this.day = date.getDate();
this.month = date.getMonth() + 1;
this.year = date.getFullYear();
this.hour = date.getHours();
this.minute = date.getMinutes();
}
}

View File

@@ -1,15 +0,0 @@
import {
CanDeactivate,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
export class LeaveComponentGuard implements CanDeactivate<any> {
canDeactivate(
component: any,
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Promise<boolean> {
return component.canDeactivate();
}
}

View File

@@ -1,28 +0,0 @@
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'city',
pure: true
})
export class CityPipe implements PipeTransform {
transform(value: any, ...args: any[]): any {
let fmt = args[0]; // short, long
let short, long;
switch (value) {
case 'Graz':
long = 'Flughafen Graz Thalerhof';
short = 'GRZ';
break;
case 'Hamburg':
long = 'Airport Hamburg Fuhlsbüttl Helmut Schmidt';
short = 'HAM';
break;
default:
long = short = 'ROM';
}
if (fmt == 'short') return short;
return long;
}
}

View File

@@ -1,12 +0,0 @@
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable } from 'rxjs';
export class CustomPreloadingStrategy implements PreloadingStrategy {
preload(route: Route, fn: () => Observable<any>): Observable<any> {
//return Observable.of(true).delay(7000).flatMap(_ => fn());
if (true) {
return fn();
}
}
}

View File

@@ -1,41 +0,0 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { CityPipe } from './pipes/city.pipe';
import { CityValidatorDirective } from './validation/city.validator';
import { RoundTrip } from './validation/roundtrip.validator';
import { AsyncCityValidatorDirective } from './validation/async-city.validator';
import { DateComponent } from './date/date.component';
import { AuthGuard } from './auth/auth.guard';
import { LeaveComponentGuard } from './deactivation/LeaveComponentGuard';
import { CustomPreloadingStrategy } from './preload/custom-preloading.strategy';
@NgModule({
imports: [
FormsModule, // [(ngModel)]
CommonModule // ngFor, ngIf, ngStyle, ngClass, date, json
],
providers: [],
declarations: [
CityPipe,
CityValidatorDirective,
AsyncCityValidatorDirective,
RoundTrip,
DateComponent
],
exports: [
CityPipe,
CityValidatorDirective,
AsyncCityValidatorDirective,
RoundTrip,
DateComponent
]
})
export class SharedModule {
static forRoot(): ModuleWithProviders<SharedModule> {
return {
providers: [AuthGuard, LeaveComponentGuard, CustomPreloadingStrategy],
ngModule: SharedModule
};
}
}

View File

@@ -1,27 +0,0 @@
import { Directive } from '@angular/core';
import { NG_ASYNC_VALIDATORS, AbstractControl } from '@angular/forms';
@Directive({
selector: 'input[async-city]',
providers: [
{
provide: NG_ASYNC_VALIDATORS,
useExisting: AsyncCityValidatorDirective,
multi: true
}
]
})
export class AsyncCityValidatorDirective {
validate(ctrl: AbstractControl): Promise<any> {
return new Promise((resolve: Function) => {
setTimeout(() => {
if (ctrl.value == 'Graz' || ctrl.value == 'Hamburg') {
resolve({});
return;
}
resolve({ 'async-city': false });
}, 100);
});
}
}

View File

@@ -1,50 +0,0 @@
import { Directive, Input, Attribute } from '@angular/core';
import {
NG_VALIDATORS,
Validator,
AbstractControl,
FormGroup
} from '@angular/forms';
@Directive({
selector: 'input[city]', // <input city>
providers: [
{
provide: NG_VALIDATORS,
useExisting: CityValidatorDirective,
multi: true
}
]
})
export class CityValidatorDirective implements Validator {
// @Input() city: string;
constructor(@Attribute('city') private city: string) {}
validate(c: AbstractControl): any {
let formGroup = <FormGroup>c.root;
let otherValueCtrl = formGroup.controls['to'];
if (!otherValueCtrl) return {};
let otherValue = otherValueCtrl.value;
if (otherValue == c.value) {
return {
city: 'rundflug'
};
}
if (!this.city) return {};
let allowed = this.city.split(','); //['Graz', 'Hamburg', 'Wien', 'Frankfurt'];
if (allowed.indexOf(c.value) == -1) {
return {
city: true
};
}
return {};
}
}

View File

@@ -1,34 +0,0 @@
import { Directive } from '@angular/core';
import {
FormGroup,
Validator,
AbstractControl,
NG_VALIDATORS,
FormGroupDirective
} from '@angular/forms';
@Directive({
selector: 'form[round-trip]',
providers: [{ provide: NG_VALIDATORS, useExisting: RoundTrip, multi: true }]
})
export class RoundTrip implements Validator {
validate(control: AbstractControl): any {
let formGroup = <FormGroup>control;
let fromCtrl = formGroup.controls['from'];
let toCtrl = formGroup.controls['to'];
if (!fromCtrl || !toCtrl) return {};
let from = fromCtrl.value;
let to = toCtrl.value;
if (from == to) {
return {
'round-trip': {
city: from
}
};
}
return {};
}
}