Skip to content

Commit

Permalink
Added aindows claims Guard into angular application.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Kulik committed Jul 25, 2019
1 parent 3324526 commit 2820f59
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, UrlTree } from '@angular/router';
import { AuthorizeService } from "./authorize.service";
import { ApplicationPaths, QueryParameterNames } from './api-authorization.constants';

@Injectable({
providedIn: 'root'
})
export class AuthorizeWindowsGroupGuardGuard implements CanActivate {
constructor(private authorize: AuthorizeService, private router: Router) { }

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> |
Promise<boolean | UrlTree> |
boolean |
UrlTree {
return this.authorize.getUser().pipe(map((u: any) => !!u && !!u.hasUsersGroup)).pipe(tap((isAuthorized: boolean) => this.handleAuthorization(isAuthorized, state)));;
}

private handleAuthorization(isAuthenticated: boolean, state: RouterStateSnapshot) {
if (!isAuthenticated) {
window.location.href = "/Identity/Account/Login?" + QueryParameterNames.ReturnUrl + "=/";
}
}
}
3 changes: 2 additions & 1 deletion src/ClientApp/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { HomeComponent } from "./home/home.component";
import { DataComponent } from "./data/data.component";
import { AuthorizeGuard } from "./api-authorization/authorize.guard";
import { InternalDataComponent } from "./internal-data/internal-data.component";
import { AuthorizeWindowsGroupGuardGuard } from "./api-authorization/authorize-windows-group-guard.guard";


const routes: Routes = [
{ path: '', component: HomeComponent, pathMatch: 'full' },
{ path: 'data', component: DataComponent, canActivate: [AuthorizeGuard] },
{ path: 'internaldata', component: InternalDataComponent, canActivate: [AuthorizeGuard] }
{ path: 'internaldata', component: InternalDataComponent, canActivate: [AuthorizeWindowsGroupGuardGuard] }
];


Expand Down
11 changes: 11 additions & 0 deletions src/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Models;
using IdentityServer4WebApp.Data;
using IdentityServer4WebApp.Models;
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -39,6 +41,15 @@ public void ConfigureServices(IServiceCollection services)
{
var apiResource = options.ApiResources.First();
apiResource.UserClaims = new[] { "hasUsersGroup" };

var identityResource = new IdentityResource
{
Name = "customprofile",
DisplayName = "Custom profile",
UserClaims = new[] { "hasUsersGroup" },
};
identityResource.Properties.Add(ApplicationProfilesPropertyNames.Clients, "*");
options.IdentityResources.Add(identityResource);
}
);

Expand Down

0 comments on commit 2820f59

Please sign in to comment.