witspry-auth-ng-client 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/angular.json +40 -0
- package/jest.config.js +32 -0
- package/karma.conf.js +33 -0
- package/ng-package.json +7 -0
- package/package.json +74 -48
- package/src/lib/components/index.ts +1 -0
- package/src/lib/components/oauth2-callback/oauth2-callback.component.spec.ts +215 -0
- package/src/lib/components/oauth2-callback/oauth2-callback.component.ts +291 -0
- package/src/lib/guards/index.ts +1 -0
- package/src/lib/guards/oauth2-auth.guard.ts +210 -0
- package/src/lib/interceptors/index.ts +1 -0
- package/src/lib/interceptors/oauth2.interceptor.ts +250 -0
- package/src/lib/models/index.ts +3 -0
- package/src/lib/models/oauth2-config.model.ts +71 -0
- package/src/lib/models/oauth2-types.model.ts +142 -0
- package/src/lib/models/witsauth-types.model.ts +8 -0
- package/src/lib/ng-oauth2-pkce.service.ts +66 -0
- package/src/lib/services/index.ts +2 -0
- package/src/lib/services/oauth2-storage.service.ts +212 -0
- package/src/lib/services/oauth2.service.ts +1019 -0
- package/src/lib/tokens/index.ts +1 -0
- package/src/lib/tokens/oauth2-config.token.ts +7 -0
- package/src/public-api.ts +26 -0
- package/src/test-setup.ts +51 -0
- package/tsconfig.json +32 -0
- package/tsconfig.lib.json +17 -0
- package/tsconfig.spec.json +17 -0
- package/fesm2022/witspry-auth-ng-client.mjs +0 -1716
- package/fesm2022/witspry-auth-ng-client.mjs.map +0 -1
- package/types/witspry-auth-ng-client.d.ts +0 -510
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { Component, OnInit, Inject, PLATFORM_ID, ChangeDetectorRef } from '@angular/core';
|
|
2
|
+
import { CommonModule, isPlatformBrowser } from '@angular/common';
|
|
3
|
+
import { Router } from '@angular/router';
|
|
4
|
+
import { OAuth2Service } from '../../services/oauth2.service';
|
|
5
|
+
import { OAuth2Config } from '../../models/oauth2-config.model';
|
|
6
|
+
import { OAUTH2_CONFIG_TOKEN } from '../../tokens/oauth2-config.token';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* OAuth2 Callback Component
|
|
10
|
+
* Handles the OAuth2 authorization callback and processes the authorization code
|
|
11
|
+
*/
|
|
12
|
+
@Component({
|
|
13
|
+
selector: 'lib-oauth2-callback',
|
|
14
|
+
standalone: true,
|
|
15
|
+
imports: [CommonModule],
|
|
16
|
+
template: `
|
|
17
|
+
<div class="oauth2-callback-container">
|
|
18
|
+
<div class="oauth2-callback-content">
|
|
19
|
+
<div *ngIf="isLoading" class="loading-section">
|
|
20
|
+
<div class="spinner"></div>
|
|
21
|
+
<h2>Processing Authentication...</h2>
|
|
22
|
+
<p>Please wait while we complete your login.</p>
|
|
23
|
+
</div>
|
|
24
|
+
|
|
25
|
+
<div *ngIf="success && !isLoading" class="success-section">
|
|
26
|
+
<div class="success-icon">✓</div>
|
|
27
|
+
|
|
28
|
+
<div *ngIf="isWitsAuthMessageApplicable" class="witsauth-message">
|
|
29
|
+
<p style="padding-bottom:10px;"><strong>{{ witsAuthMessage }}</strong></p>
|
|
30
|
+
<button (click)="goHome()" class="home-button">
|
|
31
|
+
Go Home
|
|
32
|
+
</button>
|
|
33
|
+
</div>
|
|
34
|
+
<div *ngIf="!isWitsAuthMessageApplicable" class="witsauth-message">
|
|
35
|
+
<h2>Authentication Successful!</h2>
|
|
36
|
+
<p>You will be redirected shortly...</p>
|
|
37
|
+
</div>
|
|
38
|
+
|
|
39
|
+
</div>
|
|
40
|
+
|
|
41
|
+
<div *ngIf="!!error && !isLoading && !success" class="error-section">
|
|
42
|
+
<div class="error-icon">✗</div>
|
|
43
|
+
<h2>Authentication Failed</h2>
|
|
44
|
+
<p class="error-message">{{ error }}</p>
|
|
45
|
+
<div class="error-actions">
|
|
46
|
+
<button (click)="retryLogin()" class="retry-button">
|
|
47
|
+
Try Again
|
|
48
|
+
</button>
|
|
49
|
+
<button (click)="goHome()" class="home-button">
|
|
50
|
+
Go Home
|
|
51
|
+
</button>
|
|
52
|
+
</div>
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
`,
|
|
57
|
+
styles: [`
|
|
58
|
+
.oauth2-callback-container {
|
|
59
|
+
display: flex;
|
|
60
|
+
justify-content: center;
|
|
61
|
+
align-items: center;
|
|
62
|
+
min-height: 100vh;
|
|
63
|
+
background-color: #f5f5f5;
|
|
64
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.oauth2-callback-content {
|
|
68
|
+
background: white;
|
|
69
|
+
border-radius: 8px;
|
|
70
|
+
padding: 2rem;
|
|
71
|
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
|
72
|
+
text-align: center;
|
|
73
|
+
max-width: 400px;
|
|
74
|
+
width: 100%;
|
|
75
|
+
margin: 1rem;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.loading-section h2,
|
|
79
|
+
.success-section h2,
|
|
80
|
+
.error-section h2 {
|
|
81
|
+
margin: 1rem 0 0.5rem 0;
|
|
82
|
+
color: #333;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.loading-section p,
|
|
86
|
+
.success-section p {
|
|
87
|
+
color: #666;
|
|
88
|
+
margin-bottom: 0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.spinner {
|
|
92
|
+
border: 3px solid #f3f3f3;
|
|
93
|
+
border-top: 3px solid #007bff;
|
|
94
|
+
border-radius: 50%;
|
|
95
|
+
width: 40px;
|
|
96
|
+
height: 40px;
|
|
97
|
+
animation: spin 1s linear infinite;
|
|
98
|
+
margin: 0 auto 1rem auto;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
@keyframes spin {
|
|
102
|
+
0% { transform: rotate(0deg); }
|
|
103
|
+
100% { transform: rotate(360deg); }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.success-icon {
|
|
107
|
+
width: 60px;
|
|
108
|
+
height: 60px;
|
|
109
|
+
border-radius: 50%;
|
|
110
|
+
background-color: #28a745;
|
|
111
|
+
color: white;
|
|
112
|
+
display: flex;
|
|
113
|
+
align-items: center;
|
|
114
|
+
justify-content: center;
|
|
115
|
+
font-size: 2rem;
|
|
116
|
+
font-weight: bold;
|
|
117
|
+
margin: 0 auto 1rem auto;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.error-icon {
|
|
121
|
+
width: 60px;
|
|
122
|
+
height: 60px;
|
|
123
|
+
border-radius: 50%;
|
|
124
|
+
background-color: #dc3545;
|
|
125
|
+
color: white;
|
|
126
|
+
display: flex;
|
|
127
|
+
align-items: center;
|
|
128
|
+
justify-content: center;
|
|
129
|
+
font-size: 2rem;
|
|
130
|
+
font-weight: bold;
|
|
131
|
+
margin: 0 auto 1rem auto;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.error-message {
|
|
135
|
+
color: #dc3545;
|
|
136
|
+
margin: 1rem 0;
|
|
137
|
+
padding: 0.5rem;
|
|
138
|
+
background-color: #f8d7da;
|
|
139
|
+
border: 1px solid #f5c6cb;
|
|
140
|
+
border-radius: 4px;
|
|
141
|
+
font-size: 0.9rem;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.error-actions {
|
|
145
|
+
display: flex;
|
|
146
|
+
gap: 1rem;
|
|
147
|
+
justify-content: center;
|
|
148
|
+
margin-top: 1.5rem;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.retry-button,
|
|
152
|
+
.home-button {
|
|
153
|
+
padding: 0.75rem 1.5rem;
|
|
154
|
+
border: none;
|
|
155
|
+
border-radius: 4px;
|
|
156
|
+
cursor: pointer;
|
|
157
|
+
font-size: 0.9rem;
|
|
158
|
+
font-weight: 500;
|
|
159
|
+
transition: background-color 0.2s;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.retry-button {
|
|
163
|
+
background-color: #007bff;
|
|
164
|
+
color: white;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.retry-button:hover {
|
|
168
|
+
background-color: #0056b3;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
.home-button {
|
|
172
|
+
background-color: #6c757d;
|
|
173
|
+
color: white;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
.home-button:hover {
|
|
177
|
+
background-color: #545b62;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@media (max-width: 480px) {
|
|
181
|
+
.oauth2-callback-content {
|
|
182
|
+
margin: 0.5rem;
|
|
183
|
+
padding: 1.5rem;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.error-actions {
|
|
187
|
+
flex-direction: column;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.retry-button,
|
|
191
|
+
.home-button {
|
|
192
|
+
width: 100%;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
`]
|
|
196
|
+
})
|
|
197
|
+
export class OAuth2CallbackComponent implements OnInit {
|
|
198
|
+
isLoading = true;
|
|
199
|
+
error: string | null = null;
|
|
200
|
+
success = false;
|
|
201
|
+
isWitsAuthMessageApplicable = false;
|
|
202
|
+
witsAuthMessage: string | null = null;
|
|
203
|
+
|
|
204
|
+
constructor(
|
|
205
|
+
private oauth2Service: OAuth2Service,
|
|
206
|
+
private router: Router,
|
|
207
|
+
@Inject(PLATFORM_ID) private platformId: Object,
|
|
208
|
+
@Inject(OAUTH2_CONFIG_TOKEN) private config: OAuth2Config,
|
|
209
|
+
private cdr: ChangeDetectorRef
|
|
210
|
+
) {}
|
|
211
|
+
|
|
212
|
+
async ngOnInit(): Promise<void> {
|
|
213
|
+
// Only handle callback in browser environment
|
|
214
|
+
if (!isPlatformBrowser(this.platformId)) {
|
|
215
|
+
this.isLoading = false;
|
|
216
|
+
//this.error = 'Browser environment required for OAuth2 callback';
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Add a small delay to ensure the component is fully rendered on client side
|
|
221
|
+
setTimeout(async () => {
|
|
222
|
+
try {
|
|
223
|
+
console.log('Starting OAuth2 callback processing...');
|
|
224
|
+
console.log('Current URL:', window.location.href);
|
|
225
|
+
console.log('LocalStorage available:', typeof(Storage) !== "undefined");
|
|
226
|
+
|
|
227
|
+
// Handle WithsAuth specific response
|
|
228
|
+
if (this.config.oAuthProvider === 'witsauth') {
|
|
229
|
+
const witsAuthResponse = this.oauth2Service.getWitsAuthResponse();
|
|
230
|
+
if (witsAuthResponse.isApplicable) {
|
|
231
|
+
this.isLoading = false;
|
|
232
|
+
this.success = true;
|
|
233
|
+
this.error = null; // Clear any previous error
|
|
234
|
+
this.isWitsAuthMessageApplicable = true;
|
|
235
|
+
this.witsAuthMessage = witsAuthResponse.message;
|
|
236
|
+
console.log('WitsAuth message:', this.witsAuthMessage);
|
|
237
|
+
return;
|
|
238
|
+
} else {
|
|
239
|
+
console.warn('WitsAuth response not applicable');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Handle the OAuth2 callback
|
|
244
|
+
await this.oauth2Service.handleCallback();
|
|
245
|
+
|
|
246
|
+
// Success
|
|
247
|
+
this.isLoading = false;
|
|
248
|
+
this.success = true;
|
|
249
|
+
this.error = null; // Clear any previous error
|
|
250
|
+
|
|
251
|
+
// Redirect after a short delay
|
|
252
|
+
setTimeout(() => {
|
|
253
|
+
// Use configurable redirect route from OAuth2Config, defaulting to '/'
|
|
254
|
+
const redirectRoute = this.config.redirectRoute || '/';
|
|
255
|
+
this.router.navigate([redirectRoute]);
|
|
256
|
+
}, 2000);
|
|
257
|
+
|
|
258
|
+
} catch (error) {
|
|
259
|
+
console.error('OAuth2 callback error:', error);
|
|
260
|
+
this.success = false;
|
|
261
|
+
this.isLoading = false;
|
|
262
|
+
this.error = 'Authentication failed. Contact support or try again later.';
|
|
263
|
+
|
|
264
|
+
this.cdr.detectChanges();
|
|
265
|
+
}
|
|
266
|
+
}, 100);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
retryLogin(): void {
|
|
270
|
+
if (!this.oauth2Service.isAuthenticationAvailable()) {
|
|
271
|
+
this.error = 'Authentication not available in current environment';
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
this.isLoading = true;
|
|
276
|
+
this.error = null;
|
|
277
|
+
this.success = false;
|
|
278
|
+
|
|
279
|
+
this.oauth2Service.startAuthorization().catch(error => {
|
|
280
|
+
console.error('Retry login error:', error);
|
|
281
|
+
this.isLoading = false;
|
|
282
|
+
this.error = 'Failed to start authentication';
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
goHome(): void {
|
|
287
|
+
// Use configurable redirect route from OAuth2Config, defaulting to '/'
|
|
288
|
+
const redirectRoute = this.config.redirectRoute || '/';
|
|
289
|
+
this.router.navigate([redirectRoute]);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './oauth2-auth.guard';
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { inject } from '@angular/core';
|
|
2
|
+
import { Router, CanActivateFn, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
|
3
|
+
import { Observable, of } from 'rxjs';
|
|
4
|
+
import { map, take } from 'rxjs/operators';
|
|
5
|
+
import { OAuth2Service } from '../services/oauth2.service';
|
|
6
|
+
import { OAUTH2_CONFIG_TOKEN } from '../tokens';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* OAuth2 Authentication Guard Function
|
|
10
|
+
* Protects routes by checking if user is authenticated
|
|
11
|
+
*/
|
|
12
|
+
export const oauth2AuthGuard: CanActivateFn = (
|
|
13
|
+
route: ActivatedRouteSnapshot,
|
|
14
|
+
state: RouterStateSnapshot
|
|
15
|
+
): Observable<boolean> | Promise<boolean> | boolean => {
|
|
16
|
+
const oauth2Service = inject(OAuth2Service);
|
|
17
|
+
const router = inject(Router);
|
|
18
|
+
const config = inject(OAUTH2_CONFIG_TOKEN);
|
|
19
|
+
|
|
20
|
+
// Check if authentication is available in current environment
|
|
21
|
+
if (!oauth2Service.isAuthenticationAvailable()) {
|
|
22
|
+
console.warn('[OAuth2AuthGuard] Authentication not available in current environment');
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Check current authentication state
|
|
27
|
+
return oauth2Service.authState$.pipe(
|
|
28
|
+
take(1),
|
|
29
|
+
map(authState => {
|
|
30
|
+
if (authState.isAuthenticated) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Check if we have stored tokens that might be valid
|
|
35
|
+
if (oauth2Service.isAuthenticated()) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// User is not authenticated, check for refresh token and attempt to refresh
|
|
40
|
+
if (config.autoRefresh) {
|
|
41
|
+
oauth2Service.refreshAccessToken().catch(error => {
|
|
42
|
+
console.error('[OAuth2AuthGuard] Failed to refresh access token:', error);
|
|
43
|
+
oauth2Service.startAuthorization().catch(error => {
|
|
44
|
+
console.error('[OAuth2AuthGuard] Failed to start OAuth2 flow:', error);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
// You can customize this behavior:
|
|
49
|
+
// Option 1: Start OAuth2 flow immediately
|
|
50
|
+
oauth2Service.startAuthorization().catch(error => {
|
|
51
|
+
console.error('[OAuth2AuthGuard] Failed to start OAuth2 flow:', error);
|
|
52
|
+
});
|
|
53
|
+
// Option 2: Redirect to a login page
|
|
54
|
+
// router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* OAuth2 Authentication Guard Class (for backward compatibility)
|
|
65
|
+
* @deprecated Use oauth2AuthGuard function instead
|
|
66
|
+
*/
|
|
67
|
+
export class OAuth2AuthGuard {
|
|
68
|
+
constructor(
|
|
69
|
+
private oauth2Service: OAuth2Service,
|
|
70
|
+
private router: Router
|
|
71
|
+
) {}
|
|
72
|
+
|
|
73
|
+
canActivate(
|
|
74
|
+
route: ActivatedRouteSnapshot,
|
|
75
|
+
state: RouterStateSnapshot
|
|
76
|
+
): Observable<boolean> | Promise<boolean> | boolean {
|
|
77
|
+
// Check if authentication is available in current environment
|
|
78
|
+
if (!this.oauth2Service.isAuthenticationAvailable()) {
|
|
79
|
+
console.warn('[OAuth2AuthGuard] Authentication not available in current environment');
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Check current authentication state
|
|
84
|
+
return this.oauth2Service.authState$.pipe(
|
|
85
|
+
take(1),
|
|
86
|
+
map(authState => {
|
|
87
|
+
if (authState.isAuthenticated) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Check if we have stored tokens that might be valid
|
|
92
|
+
if (this.oauth2Service.isAuthenticated()) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// User is not authenticated, redirect to login or start OAuth flow
|
|
97
|
+
console.log('[OAuth2AuthGuard] User not authenticated, starting OAuth2 flow');
|
|
98
|
+
|
|
99
|
+
this.oauth2Service.startAuthorization().catch(error => {
|
|
100
|
+
console.error('[OAuth2AuthGuard] Failed to start OAuth2 flow:', error);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return false;
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* OAuth2 Unauthenticated Guard Function
|
|
111
|
+
* Protects routes that should only be accessible to unauthenticated users
|
|
112
|
+
* (e.g., login page, registration page)
|
|
113
|
+
*/
|
|
114
|
+
export const oauth2UnauthGuard: CanActivateFn = (
|
|
115
|
+
route: ActivatedRouteSnapshot,
|
|
116
|
+
state: RouterStateSnapshot
|
|
117
|
+
): Observable<boolean> | Promise<boolean> | boolean => {
|
|
118
|
+
const oauth2Service = inject(OAuth2Service);
|
|
119
|
+
const router = inject(Router);
|
|
120
|
+
|
|
121
|
+
// Check if authentication is available in current environment
|
|
122
|
+
if (!oauth2Service.isAuthenticationAvailable()) {
|
|
123
|
+
return true; // Allow access if auth is not available
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Check current authentication state
|
|
127
|
+
return oauth2Service.authState$.pipe(
|
|
128
|
+
take(1),
|
|
129
|
+
map(authState => {
|
|
130
|
+
if (!authState.isAuthenticated && !oauth2Service.isAuthenticated()) {
|
|
131
|
+
return true; // User is not authenticated, allow access
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// User is authenticated, redirect to protected area
|
|
135
|
+
console.log('[OAuth2UnauthGuard] User already authenticated, redirecting');
|
|
136
|
+
router.navigate(['/']); // Redirect to home or dashboard
|
|
137
|
+
return false;
|
|
138
|
+
})
|
|
139
|
+
);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* OAuth2 Role Guard Function
|
|
144
|
+
* Protects routes based on user roles or permissions
|
|
145
|
+
*/
|
|
146
|
+
export interface OAuth2RoleGuardConfig {
|
|
147
|
+
roles?: string[];
|
|
148
|
+
permissions?: string[];
|
|
149
|
+
requireAll?: boolean; // If true, user must have ALL roles/permissions. If false, user needs ANY.
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function oauth2RoleGuard(config: OAuth2RoleGuardConfig): CanActivateFn {
|
|
153
|
+
return (route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean => {
|
|
154
|
+
const oauth2Service = inject(OAuth2Service);
|
|
155
|
+
const router = inject(Router);
|
|
156
|
+
|
|
157
|
+
// First check if user is authenticated
|
|
158
|
+
if (!oauth2Service.isAuthenticated()) {
|
|
159
|
+
console.log('[OAuth2RoleGuard] User not authenticated');
|
|
160
|
+
oauth2Service.startAuthorization().catch(error => {
|
|
161
|
+
console.error('[OAuth2RoleGuard] Failed to start OAuth2 flow:', error);
|
|
162
|
+
});
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// If no roles or permissions specified, just check authentication
|
|
167
|
+
if (!config.roles?.length && !config.permissions?.length) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Get user info to check roles/permissions
|
|
172
|
+
return oauth2Service.getUserInfo().then(userInfo => {
|
|
173
|
+
const userRoles = (userInfo as any).roles || [];
|
|
174
|
+
const userPermissions = (userInfo as any).permissions || [];
|
|
175
|
+
|
|
176
|
+
let hasRequiredRoles = true;
|
|
177
|
+
let hasRequiredPermissions = true;
|
|
178
|
+
|
|
179
|
+
// Check roles
|
|
180
|
+
if (config.roles?.length) {
|
|
181
|
+
if (config.requireAll) {
|
|
182
|
+
hasRequiredRoles = config.roles.every(role => userRoles.includes(role));
|
|
183
|
+
} else {
|
|
184
|
+
hasRequiredRoles = config.roles.some(role => userRoles.includes(role));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Check permissions
|
|
189
|
+
if (config.permissions?.length) {
|
|
190
|
+
if (config.requireAll) {
|
|
191
|
+
hasRequiredPermissions = config.permissions.every(permission => userPermissions.includes(permission));
|
|
192
|
+
} else {
|
|
193
|
+
hasRequiredPermissions = config.permissions.some(permission => userPermissions.includes(permission));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const hasAccess = hasRequiredRoles && hasRequiredPermissions;
|
|
198
|
+
|
|
199
|
+
if (!hasAccess) {
|
|
200
|
+
console.warn('[OAuth2RoleGuard] User does not have required roles/permissions');
|
|
201
|
+
router.navigate(['/unauthorized']); // Redirect to unauthorized page
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return hasAccess;
|
|
205
|
+
}).catch(error => {
|
|
206
|
+
console.error('[OAuth2RoleGuard] Failed to get user info:', error);
|
|
207
|
+
return false;
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './oauth2.interceptor';
|