-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: abstract ICM as identity provider (#447)
BREAKING CHANGE: login/logout handling is abstracted as identity provider service
- Loading branch information
Showing
44 changed files
with
693 additions
and
1,021 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
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 |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import { NgModule } from '@angular/core'; | ||
|
||
import { ClickOutsideDirective } from './directives/click-outside.directive'; | ||
import { IdentityProviderCapabilityDirective } from './directives/identity-provider-capability.directive'; | ||
import { IntersectionObserverDirective } from './directives/intersection-observer.directive'; | ||
import { ServerHtmlDirective } from './directives/server-html.directive'; | ||
|
||
@NgModule({ | ||
declarations: [ClickOutsideDirective, IntersectionObserverDirective, ServerHtmlDirective], | ||
exports: [ClickOutsideDirective, IntersectionObserverDirective, ServerHtmlDirective], | ||
declarations: [ | ||
ClickOutsideDirective, | ||
IdentityProviderCapabilityDirective, | ||
IntersectionObserverDirective, | ||
ServerHtmlDirective, | ||
], | ||
exports: [ | ||
ClickOutsideDirective, | ||
IdentityProviderCapabilityDirective, | ||
IntersectionObserverDirective, | ||
ServerHtmlDirective, | ||
], | ||
}) | ||
export class DirectivesModule {} |
25 changes: 25 additions & 0 deletions
25
src/app/core/directives/identity-provider-capability.directive.ts
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,25 @@ | ||
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; | ||
|
||
import { IdentityProviderFactory } from 'ish-core/identity-provider/identity-provider.factory'; | ||
import { IdentityProviderCapabilities } from 'ish-core/identity-provider/identity-provider.interface'; | ||
|
||
@Directive({ | ||
selector: '[ishIdentityProviderCapability]', | ||
}) | ||
export class IdentityProviderCapabilityDirective { | ||
constructor( | ||
private templateRef: TemplateRef<unknown>, | ||
private viewContainer: ViewContainerRef, | ||
private identityProviderFactory: IdentityProviderFactory | ||
) {} | ||
|
||
@Input() set ishIdentityProviderCapability(val: keyof IdentityProviderCapabilities) { | ||
const enabled = this.identityProviderFactory.getInstance().getCapabilities()[val]; | ||
|
||
if (enabled) { | ||
this.viewContainer.createEmbeddedView(this.templateRef); | ||
} else { | ||
this.viewContainer.clear(); | ||
} | ||
} | ||
} |
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,13 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router'; | ||
|
||
import { IdentityProviderFactory } from 'ish-core/identity-provider/identity-provider.factory'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class IdentityProviderLoginGuard implements CanActivate { | ||
constructor(private identityProviderFactory: IdentityProviderFactory) {} | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
return this.identityProviderFactory.getInstance().triggerLogin(route, state); | ||
} | ||
} |
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,13 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router'; | ||
|
||
import { IdentityProviderFactory } from 'ish-core/identity-provider/identity-provider.factory'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class IdentityProviderLogoutGuard implements CanActivate { | ||
constructor(private identityProviderFactory: IdentityProviderFactory) {} | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
return this.identityProviderFactory.getInstance().triggerLogout(route, state); | ||
} | ||
} |
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,16 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router'; | ||
|
||
import { IdentityProviderFactory } from 'ish-core/identity-provider/identity-provider.factory'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class IdentityProviderRegisterGuard implements CanActivate { | ||
constructor(private identityProviderFactory: IdentityProviderFactory) {} | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | ||
const identityProvider = this.identityProviderFactory.getInstance(); | ||
return identityProvider.triggerRegister | ||
? identityProvider.triggerRegister(route, state) | ||
: identityProvider.triggerLogin(route, state); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,40 @@ | ||
import { APP_INITIALIZER, NgModule } from '@angular/core'; | ||
import { Store, select } from '@ngrx/store'; | ||
import { first, tap } from 'rxjs/operators'; | ||
|
||
import { ICMIdentityProvider } from './identity-provider/icm.identity-provider'; | ||
import { IDENTITY_PROVIDER_IMPLEMENTOR, IdentityProviderFactory } from './identity-provider/identity-provider.factory'; | ||
import { getIdentityProvider } from './store/core/configuration'; | ||
import { whenTruthy } from './utils/operators'; | ||
|
||
export function identityProviderFactoryInitializer(store: Store, identityProviderFactory: IdentityProviderFactory) { | ||
return () => | ||
store | ||
.pipe( | ||
select(getIdentityProvider), | ||
whenTruthy(), | ||
first(), | ||
tap(config => identityProviderFactory.init(config)) | ||
) | ||
.toPromise(); | ||
} | ||
|
||
@NgModule({ | ||
providers: [ | ||
{ | ||
provide: APP_INITIALIZER, | ||
useFactory: identityProviderFactoryInitializer, | ||
deps: [Store, IdentityProviderFactory], | ||
multi: true, | ||
}, | ||
{ | ||
provide: IDENTITY_PROVIDER_IMPLEMENTOR, | ||
multi: true, | ||
useValue: { | ||
type: 'ICM', | ||
implementor: ICMIdentityProvider, | ||
}, | ||
}, | ||
], | ||
}) | ||
export class IdentityProviderModule {} |
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,59 @@ | ||
import { HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Router } from '@angular/router'; | ||
import { Store, select } from '@ngrx/store'; | ||
import { Observable, noop } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { selectQueryParam } from 'ish-core/store/core/router'; | ||
import { logoutUser } from 'ish-core/store/customer/user'; | ||
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service'; | ||
|
||
import { IdentityProvider, TriggerReturnType } from './identity-provider.interface'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class ICMIdentityProvider implements IdentityProvider { | ||
constructor(protected router: Router, protected store: Store, protected apiTokenService: ApiTokenService) {} | ||
|
||
getCapabilities() { | ||
return { | ||
editPassword: true, | ||
editEmail: true, | ||
}; | ||
} | ||
|
||
init() { | ||
this.apiTokenService.restore$().subscribe(noop); | ||
|
||
this.apiTokenService.cookieVanishes$.subscribe(type => { | ||
this.store.dispatch(logoutUser()); | ||
if (type === 'user') { | ||
this.router.navigate(['/login'], { | ||
queryParams: { returnUrl: this.router.url, messageKey: 'session_timeout' }, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
triggerLogin(): TriggerReturnType { | ||
return true; | ||
} | ||
|
||
triggerLogout(): TriggerReturnType { | ||
this.store.dispatch(logoutUser()); | ||
this.apiTokenService.removeApiToken(); | ||
return this.store.pipe( | ||
select(selectQueryParam('returnUrl')), | ||
map(returnUrl => returnUrl || '/home'), | ||
map(returnUrl => this.router.parseUrl(returnUrl)) | ||
); | ||
} | ||
|
||
triggerRegister(): TriggerReturnType { | ||
return true; | ||
} | ||
|
||
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { | ||
return this.apiTokenService.intercept(req, next); | ||
} | ||
} |
Oops, something went wrong.