Keycloak - quan1997ap/angular-app-note GitHub Wiki

Authentication (xác thực) là quá trình kiểm tra danh tính một tài khoản đang vào hệ thống hiện tại thông qua một hệ thống xác thực.
Authorization (ủy quyền) là quá trình để xác định người dùng có được xác thực có quyền truy cập vào các tài nguyên cụ thể hay không
Authentication (xác thực) và Authorization (ủy quyền) là hai quá trình khác nhau trong quản lý an toàn thông tin. Ví dụ, khi bạn đăng nhập vào một trang web, quá trình đăng nhập sẽ xác thực tài khoản của bạn (trong trường hợp này chính là quá trình Authencation). Sau đó, quá trình ủy quyền (Authorization) sẽ xác định những chức năng mà bạn có thể truy cập và sử dụng trong trang web đó.
2.1.1 Install
"oidc-client": "^1.5.0",
2.1.2. Cấu hình
// Keycloak
authority: "http://10.1.40.172:8000/realms/VDS",
client_id: "ConnectionManagement",
redirect_uri: location.protocol + "//" + location.host + "/auth-callback",
post_logout_redirect_uri: location.protocol + "//" + location.host,
response_type: "id_token token",
scope: "email profile",
2.1.3. Setup Routing auth-callback.rar.txt
{
path: 'auth-callback',
loadChildren: () => import('./modules/auth-callback/auth-callback.module').then((m) => m.AuthCallbackModule),
},
2.1.4. Setup auth-guard + auth service: auth-guard.ts.txt , auth.service.ts.txt
import { Injectable } from '@angular/core';
import { CanActivate, CanActivateChild, Router } from '@angular/router';
import { NgxPermissionsService } from 'ngx-permissions';
import { Observable, of } from 'rxjs';
import { map, tap, catchError } from 'rxjs/operators';
import { AuthService } from '../services/data/auth.service';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate, CanActivateChild {
constructor(
private authService: AuthService,
private ps: NgxPermissionsService,
private router: Router,
) {
}
canActivate(): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isLoggedIn()) {
return this.authService.getLoggedUserRole().pipe(
// Load user role in Test Management System : 'ADMIN', USER'
tap( (userSystemRole: string) => {
// console.log(`User System Role`, userSystemRole);
this.ps.loadPermissions([userSystemRole]);
}
),
// allow access router ( get member role successfully )
map( (userSystemRole: string) => true ),
catchError((err) => {
this.router.navigate(['error/500']);
return of(false);
})
)
} else {
this.authService.startAuthentication().then();
return false;
}
}
canActivateChild(): boolean {
if (this.authService.isLoggedIn()) {
return true;
}
this.authService.startAuthentication().then();
return false;
}
}
const routes: Routes = [
{
path: '',
canActivate: [AuthGuard],
canActivateChild: [AuthGuard],
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'project-management',
},
...

https://www.keycloak.org/docs/latest/authorization_services/index.html#_overview
- Resource: Có thể là web page, a RESTFul resource, a file in your file system.
- Scopes: Các hành động có thể thực hiện với resource. Có thể có 1 hoặc nhiều hành động với 1 resource( CRUD )
- Phạm vi của tài nguyên là phạm vi truy cập bị giới hạn có thể thực hiện trên tài nguyên
- Permission and policy management: Quyền và chính sách với tài nguyên (Resource) và Scope
- Các bước thực hiện: Create Policy -> Define Permission -> Apply Policy to Permissions
- Policies : Các chính sách xác định các điều kiện phải được thỏa mãn để truy cập hoặc thực hiện các hoạt động trên một thứ gì đó (tài nguyên hoặc phạm vi). Keycloak đã cung cấp 1 số chính sách tích hợp sẵn. Có thể tạo mới chính sách dựa trên rule.
- Khi bạn đã xác định chính sách của mình, bạn có thể bắt đầu xác định quyền của mình. Quyền được kết hợp với tài nguyên mà chúng đang bảo vệ. Tại đây, bạn chỉ định những gì bạn muốn bảo vệ (tài nguyên hoặc phạm vi) và các chính sách phải được đáp ứng để cấp hoặc từ chối cấp phép.
- Authorization services bao gồm ( RESTFul endpoints )
- Token Endpoint ( OAuth2 )
- Resource Management Endpoint
- Permission Management Endpoint

- Tạo Client Application
- Enabling authorization services
-
Trong tab Authorization chứa các thông tin
Settings General settings for your resource server. For more details about this page see the Resource Server Settings section.Resource From this page, you can manage your application’s resources.
Authorization Scopes From this page, you can manage scopes.
Policies From this page, you can manage authorization policies and define the conditions that must be met to grant a permission.
Permissions From this page, you can manage the permissions for your protected resources and scopes by linking them with the policies you created.
Evaluate From this page, you can simulate authorization requests and view the result of the evaluation of the permissions and authorization policies you have defined.
Export Settings From this page, you can export the authorization settings to a JSON file.
-