Browse Source

feat: implement the oauth strategy

pull/4995/head
mehmet-erim 6 years ago
parent
commit
a67c552393
  1. 7
      npm/ng-packs/apps/dev-app/src/app/home/home.component.ts
  2. 3
      npm/ng-packs/packages/account/src/lib/account-routing.module.ts
  3. 13
      npm/ng-packs/packages/account/src/lib/guards/authentication-flow.guard.ts
  4. 5
      npm/ng-packs/packages/core/src/lib/handlers/oauth-configuration.handler.ts
  5. 36
      npm/ng-packs/packages/core/src/lib/services/auth.service.ts
  6. 7
      npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts
  7. 8
      npm/ng-packs/packages/theme-basic/src/lib/components/nav-items/current-user.component.ts
  8. 7
      templates/app/angular/src/app/home/home.component.ts

7
npm/ng-packs/apps/dev-app/src/app/home/home.component.ts

@ -1,4 +1,5 @@
import { Component } from '@angular/core'; import { OAUTH_STRATEGY } from '@abp/ng.core';
import { Component, Injector } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
@Component({ @Component({
@ -10,9 +11,9 @@ export class HomeComponent {
return this.oAuthService.hasValidAccessToken(); return this.oAuthService.hasValidAccessToken();
} }
constructor(private oAuthService: OAuthService) {} constructor(private oAuthService: OAuthService, private injector: Injector) {}
login() { login() {
this.oAuthService.initCodeFlow(); OAUTH_STRATEGY.NavigateToLogin(this.injector);
} }
} }

3
npm/ng-packs/packages/account/src/lib/account-routing.module.ts

@ -17,11 +17,11 @@ const routes: Routes = [
{ {
path: '', path: '',
component: DynamicLayoutComponent, component: DynamicLayoutComponent,
canActivate: [AuthenticationFlowGuard],
children: [ children: [
{ {
path: 'login', path: 'login',
component: ReplaceableRouteContainerComponent, component: ReplaceableRouteContainerComponent,
canActivate: [AuthenticationFlowGuard],
data: { data: {
replaceableComponent: { replaceableComponent: {
key: eAccountComponents.Login, key: eAccountComponents.Login,
@ -32,6 +32,7 @@ const routes: Routes = [
{ {
path: 'register', path: 'register',
component: ReplaceableRouteContainerComponent, component: ReplaceableRouteContainerComponent,
canActivate: [AuthenticationFlowGuard],
data: { data: {
replaceableComponent: { replaceableComponent: {
key: eAccountComponents.Register, key: eAccountComponents.Register,

13
npm/ng-packs/packages/account/src/lib/guards/authentication-flow.guard.ts

@ -1,17 +1,12 @@
import { Injectable } from '@angular/core'; import { Injectable, Injector } from '@angular/core';
import { CanActivate } from '@angular/router'; import { CanActivate } from '@angular/router';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAUTH_STRATEGY } from '@abp/ng.core';
@Injectable() @Injectable()
export class AuthenticationFlowGuard implements CanActivate { export class AuthenticationFlowGuard implements CanActivate {
constructor(private oauthService: OAuthService) {} constructor(private injector: Injector) {}
canActivate() { canActivate() {
if (this.oauthService.responseType === 'code') { return OAUTH_STRATEGY.CanActivate(this.injector);
this.oauthService.initCodeFlow();
return false;
}
return true;
} }
} }

5
npm/ng-packs/packages/core/src/lib/handlers/oauth-configuration.handler.ts

@ -16,11 +16,6 @@ export class OAuthConfigurationHandler {
private oAuthService: OAuthService, private oAuthService: OAuthService,
@Inject(CORE_OPTIONS) private options: ABP.Root, @Inject(CORE_OPTIONS) private options: ABP.Root,
) { ) {
this.oAuthService.configure(this.options.environment.oAuthConfig);
if (this.oAuthService.responseType === 'code') {
this.oAuthService.setupAutomaticSilentRefresh();
}
this.listenToSetEnvironment(); this.listenToSetEnvironment();
} }

36
npm/ng-packs/packages/core/src/lib/services/auth.service.ts

@ -1,14 +1,15 @@
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import { Inject, Injectable, Optional } from '@angular/core'; import { Inject, Injectable, Injector, Optional } from '@angular/core';
import { Navigate } from '@ngxs/router-plugin'; import { Navigate } from '@ngxs/router-plugin';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
import { from, Observable, of } from 'rxjs'; import { from, Observable } from 'rxjs';
import { switchMap, take, tap } from 'rxjs/operators'; import { switchMap, take, tap } from 'rxjs/operators';
import snq from 'snq'; import snq from 'snq';
import { GetAppConfiguration } from '../actions/config.actions'; import { GetAppConfiguration } from '../actions/config.actions';
import { ConfigState } from '../states/config.state'; import { ConfigState } from '../states/config.state';
import { SessionState } from '../states/session.state'; import { SessionState } from '../states/session.state';
import { OAUTH_STRATEGY } from '../strategies/oauth.strategy';
import { RestService } from './rest.service'; import { RestService } from './rest.service';
@Injectable({ @Injectable({
@ -16,6 +17,7 @@ import { RestService } from './rest.service';
}) })
export class AuthService { export class AuthService {
constructor( constructor(
private injector: Injector,
private rest: RestService, private rest: RestService,
private oAuthService: OAuthService, private oAuthService: OAuthService,
private store: Store, private store: Store,
@ -45,28 +47,16 @@ export class AuthService {
); );
} }
logout(): Observable<void> { /**
const issuer = this.store.selectSnapshot(ConfigState.getDeep('environment.oAuthConfig.issuer')); * @deprecated use LogOut prop of OAUTH_STRATEGY instead, will be deleted in v3.3
*/
if (this.oAuthService.responseType === 'code') { logout(): Observable<any> {
this.oAuthService.logOut(); if (!this.store.selectSnapshot(ConfigState.getDeep('environment.production'))) {
return of(null); console.warn(
'The logout method of AuthService is depracated. Use LogOut prop of OAUTH_STRATEGY instead.',
);
} }
return this.rest return OAUTH_STRATEGY.LogOut(this.injector);
.request(
{
method: 'GET',
url: '/api/account/logout',
},
null,
issuer,
)
.pipe(
switchMap(() => {
this.oAuthService.logOut();
return this.store.dispatch(new GetAppConfiguration());
}),
);
} }
} }

7
npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts

@ -9,19 +9,16 @@ import { ConfigState } from '../states/config.state';
import { CORE_OPTIONS } from '../tokens/options.token'; import { CORE_OPTIONS } from '../tokens/options.token';
import { getRemoteEnv } from './environment-utils'; import { getRemoteEnv } from './environment-utils';
import { parseTenantFromUrl } from './multi-tenancy-utils'; import { parseTenantFromUrl } from './multi-tenancy-utils';
import { OAUTH_STRATEGY } from '../strategies/oauth.strategy';
export function getInitialData(injector: Injector) { export function getInitialData(injector: Injector) {
const fn = async () => { const fn = async () => {
const store: Store = injector.get(Store); const store: Store = injector.get(Store);
const options = injector.get(CORE_OPTIONS) as ABP.Root; const options = injector.get(CORE_OPTIONS) as ABP.Root;
const oAuthService = injector.get(OAuthService);
if (oAuthService.responseType === 'code') {
await oAuthService.loadDiscoveryDocumentAndTryLogin();
}
await getRemoteEnv(injector, options.environment); await getRemoteEnv(injector, options.environment);
await parseTenantFromUrl(injector); await parseTenantFromUrl(injector);
await OAUTH_STRATEGY.Init(injector);
if (options.skipGetAppConfiguration) return; if (options.skipGetAppConfiguration) return;

8
npm/ng-packs/packages/theme-basic/src/lib/components/nav-items/current-user.component.ts

@ -1,5 +1,5 @@
import { ApplicationConfiguration, AuthService, ConfigState } from '@abp/ng.core'; import { ApplicationConfiguration, OAUTH_STRATEGY, ConfigState } from '@abp/ng.core';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit, Injector } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { Select } from '@ngxs/store'; import { Select } from '@ngxs/store';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
@ -55,12 +55,12 @@ export class CurrentUserComponent implements OnInit {
return window.innerWidth < 992; return window.innerWidth < 992;
} }
constructor(private authService: AuthService, private router: Router) {} constructor(private injector: Injector, private router: Router) {}
ngOnInit() {} ngOnInit() {}
logout() { logout() {
this.authService.logout().subscribe(() => { OAUTH_STRATEGY.LogOut(this.injector).subscribe(() => {
this.router.navigate(['/'], { state: { redirectUrl: this.router.url } }); this.router.navigate(['/'], { state: { redirectUrl: this.router.url } });
}); });
} }

7
templates/app/angular/src/app/home/home.component.ts

@ -1,4 +1,5 @@
import { Component } from '@angular/core'; import { OAUTH_STRATEGY } from '@abp/ng.core';
import { Component, Injector } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc'; import { OAuthService } from 'angular-oauth2-oidc';
@Component({ @Component({
@ -10,9 +11,9 @@ export class HomeComponent {
return this.oAuthService.hasValidAccessToken(); return this.oAuthService.hasValidAccessToken();
} }
constructor(private oAuthService: OAuthService) {} constructor(private oAuthService: OAuthService, private injector: Injector) {}
login() { login() {
this.oAuthService.initCodeFlow(); OAUTH_STRATEGY.NavigateToLogin(this.injector);
} }
} }

Loading…
Cancel
Save