mirror of https://github.com/abpframework/abp.git
Browse Source
* Create abstarct-auth-filter * Create auth-events model * Create oauth-error-filter.servicepull/18577/head
14 changed files with 185 additions and 20 deletions
@ -0,0 +1,34 @@ |
|||||
|
import { Signal, signal } from '@angular/core'; |
||||
|
import { AuthErrorEvent, AuthErrorFilter } from '../models'; |
||||
|
|
||||
|
export abstract class AbstractAuthErrorFilter<T, E> { |
||||
|
readonly #filters = signal<Array<T>>([]); |
||||
|
filters = this.#filters.asReadonly(); |
||||
|
|
||||
|
abstract get(id: string): T; |
||||
|
abstract add(filter: T): void; |
||||
|
abstract patch(item: Partial<T>): void; |
||||
|
abstract remove(id: string): void; |
||||
|
abstract run(event: E): boolean; |
||||
|
} |
||||
|
|
||||
|
export class AuthErrorFilterService< |
||||
|
T = AuthErrorEvent, |
||||
|
E = AuthErrorFilter, |
||||
|
> extends AbstractAuthErrorFilter<T, E> { |
||||
|
get(id: string): T { |
||||
|
throw new Error('Import AbpOAuthModule from @abp/ng.oauth or custom implementation'); |
||||
|
} |
||||
|
add(filter: T): void { |
||||
|
throw new Error('Import AbpOAuthModule from @abp/ng.oauth or custom implementation'); |
||||
|
} |
||||
|
patch(item: Partial<T>): void { |
||||
|
throw new Error('Import AbpOAuthModule from @abp/ng.oauth or custom implementation'); |
||||
|
} |
||||
|
remove(id: string): void { |
||||
|
throw new Error('Import AbpOAuthModule from @abp/ng.oauth or custom implementation'); |
||||
|
} |
||||
|
run(event: E): boolean { |
||||
|
throw new Error('Import AbpOAuthModule from @abp/ng.oauth or custom implementation'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
export type EventType = |
||||
|
| 'discovery_document_loaded' |
||||
|
| 'jwks_load_error' |
||||
|
| 'invalid_nonce_in_state' |
||||
|
| 'discovery_document_load_error' |
||||
|
| 'discovery_document_validation_error' |
||||
|
| 'user_profile_loaded' |
||||
|
| 'user_profile_load_error' |
||||
|
| 'token_received' |
||||
|
| 'token_error' |
||||
|
| 'code_error' |
||||
|
| 'token_refreshed' |
||||
|
| 'token_refresh_error' |
||||
|
| 'silent_refresh_error' |
||||
|
| 'silently_refreshed' |
||||
|
| 'silent_refresh_timeout' |
||||
|
| 'token_validation_error' |
||||
|
| 'token_expires' |
||||
|
| 'session_changed' |
||||
|
| 'session_error' |
||||
|
| 'session_terminated' |
||||
|
| 'session_unchanged' |
||||
|
| 'logout' |
||||
|
| 'popup_closed' |
||||
|
| 'popup_blocked' |
||||
|
| 'token_revoke_error'; |
||||
|
|
||||
|
export abstract class AuthEvent { |
||||
|
readonly type: EventType; |
||||
|
constructor(type: EventType) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export class AuthSuccessEvent extends AuthEvent { |
||||
|
readonly info: any; |
||||
|
constructor(type: EventType, info?: any) { |
||||
|
super(type); |
||||
|
this.info = info; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export class AuthInfoEvent extends AuthEvent { |
||||
|
readonly info: any; |
||||
|
|
||||
|
constructor(type: EventType, info?: any) { |
||||
|
super(type); |
||||
|
this.info = info; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export class AuthErrorEvent extends AuthEvent { |
||||
|
readonly reason: object; |
||||
|
readonly params: object; |
||||
|
constructor(type: EventType, reason: object, params?: object) { |
||||
|
super(type); |
||||
|
this.reason = reason; |
||||
|
this.params = params; |
||||
|
} |
||||
|
} |
||||
@ -1 +1,2 @@ |
|||||
export * from './oauth.service'; |
export * from './oauth.service'; |
||||
|
export * from './oauth-error-filter.service'; |
||||
|
|||||
@ -0,0 +1,46 @@ |
|||||
|
import { AbstractAuthErrorFilter, AuthErrorFilter } from '@abp/ng.core'; |
||||
|
import { Injectable, signal } from '@angular/core'; |
||||
|
import { OAuthErrorEvent } from 'angular-oauth2-oidc'; |
||||
|
|
||||
|
@Injectable({ providedIn: 'root' }) |
||||
|
export class OAuthErrorFilterService extends AbstractAuthErrorFilter< |
||||
|
AuthErrorFilter<OAuthErrorEvent>, |
||||
|
OAuthErrorEvent |
||||
|
> { |
||||
|
readonly #filters = signal<Array<AuthErrorFilter<OAuthErrorEvent>>>([]); |
||||
|
|
||||
|
filters = this.#filters.asReadonly(); |
||||
|
|
||||
|
get(id: string): AuthErrorFilter<OAuthErrorEvent> { |
||||
|
return this.#filters().find(({ id: _id }) => _id === id); |
||||
|
} |
||||
|
|
||||
|
add(filter: AuthErrorFilter<OAuthErrorEvent>): void { |
||||
|
this.#filters.update(items => [...items, filter]); |
||||
|
} |
||||
|
|
||||
|
patch(item: Partial<AuthErrorFilter<OAuthErrorEvent>>): void { |
||||
|
const _item = this.filters().find(({ id }) => id === item.id); |
||||
|
if (!_item) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Object.assign(_item, item); |
||||
|
} |
||||
|
|
||||
|
remove(id: string): void { |
||||
|
const item = this.filters().find(({ id: _id }) => _id === id); |
||||
|
if (!item) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
this.#filters.update(items => items.filter(({ id: _id }) => _id !== id)); |
||||
|
} |
||||
|
|
||||
|
run(event: OAuthErrorEvent): boolean { |
||||
|
return this.filters() |
||||
|
.filter(({ executable }) => !!executable) |
||||
|
.map(({ execute }) => execute(event)) |
||||
|
.some(item => item); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue