-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAngular-Security-Examples
More file actions
205 lines (168 loc) · 5.29 KB
/
Angular-Security-Examples
File metadata and controls
205 lines (168 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Angular Security Examples
// Example 1: Mock OAuth Config
// app.ts
import { Component, Injectable, NgModule, Pipe, PipeTransform } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { RouterModule, Routes, Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { HttpClientModule, HttpClient, HttpHeaders, HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HTTP_INTERCEPTORS } from '@angular/common/http';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { Observable, of } from 'rxjs';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { OAuthService } from 'angular-oauth2-oidc';
// Mock OAuth Config
const authConfig = {
issuer: 'https://mock-issuer.com',
clientId: 'mock-client-id',
redirectUri: window.location.origin + '/callback',
scope: 'openid profile email'
};
// Mock Services and Interceptors
// Example 2: Authentication Service with JWT and RBAC
@Injectable({ providedIn: 'root' })
export class AuthService {
private token = 'mock-jwt-token';
private roles = ['user'];
login() {
this.token = 'new-mock-jwt-token';
localStorage.setItem('auth_token', this.token);
}
hasRole(role: string): boolean {
return this.roles.includes(role);
}
getToken(): string {
return this.token;
}
refreshToken(): Observable<string> {
return of('new-refresh-token');
}
}
// Example 3: Role-Based Access Control Guard
@Injectable({ providedIn: 'root' })
export class RoleGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.authService.hasRole('admin')) {
return true;
}
this.router.navigate(['/access-denied']);
return false;
}
}
// Example 4: OAuth and OIDC Integration (Mocked)
@Injectable({ providedIn: 'root' })
export class OAuthMockService extends OAuthService {
configure(config: any) {
console.log('Configuring OAuth with:', config);
}
loadDiscoveryDocumentAndTryLogin() {
return Promise.resolve(true).then(() => console.log('Logged in with OIDC'));
}
initCodeFlow() {
console.log('Initiating OAuth Code Flow'); // Example 5
}
}
// Example 5: Security Interceptor
@Injectable({ providedIn: 'root' })
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = this.authService.getToken();
const cloned = req.clone({
setHeaders: { Authorization: `Bearer ${token}` }
});
return next.handle(cloned);
}
}
// Example 6: Home Component with Basic Authentication
@Component({
selector: 'app-home',
template: `
<h2>Home</h2>
<button (click)="login()">Login</button>
<p>{{ token }}</p>
`
})
export class HomeComponent {
token: string;
constructor(private authService: AuthService, private http: HttpClient) {
this.token = this.authService.getToken();
}
login() {
this.authService.login();
const headers = new HttpHeaders({
'Authorization': `Bearer ${this.authService.getToken()}`
});
this.http.get('/api/data', { headers }).subscribe();
}
}
// Example 7: Sanitization with DomSanitizer
@Component({
selector: 'app-sanitize',
template: `
<h2>Sanitized Content</h2>
<div [innerHTML]="sanitizedHtml"></div>
`
})
export class SanitizeComponent {
sanitizedHtml: SafeHtml;
constructor(private sanitizer: DomSanitizer) {
this.sanitizedHtml = this.sanitizer.bypassSecurityTrustHtml('<b>Safe HTML</b>'); // Example 17
}
}
// Example 8: AI-Powered CAPTCHA (Mocked)
@Component({
selector: 'app-captcha',
template: `
<h2>AI CAPTCHA</h2>
<button (click)="verifyCaptcha()">Verify</button>
`
})
export class CaptchaComponent {
constructor() {}
verifyCaptcha() {
// Mocking reCAPTCHA v3 verification
console.log('reCAPTCHA Token: mock-token');
}
}
// Root NgModule (AppModule)
@NgModule({
declarations: [
HomeComponent,
SanitizeComponent,
CaptchaComponent
],
imports: [
BrowserModule,
CommonModule,
HttpClientModule,
RouterModule.forRoot([
{ path: 'home', component: HomeComponent },
{ path: 'sanitize', component: SanitizeComponent },
{ path: 'captcha', component: CaptchaComponent },
{ path: 'admin', component: HomeComponent, canActivate: [RoleGuard] },
{ path: '', redirectTo: '/home', pathMatch: 'full' }
]),
TranslateModule.forRoot()
],
providers: [
AuthService,
RoleGuard,
OAuthMockService,
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
{ provide: HttpClient, useValue: { get: () => of({}) } } // Mock HttpClient
],
bootstrap: [HomeComponent]
})
export class AppModule {}
// Bootstrap the Application
bootstrapApplication(HomeComponent, {
providers: [
provideRouter([]), // Router is already provided by AppModule
AuthService,
RoleGuard,
OAuthMockService,
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
]
}).catch(err => console.error(err));