-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
127 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 2 additions & 8 deletions
10
...pp/auth/http-auth.interceptor.ts.mustache → .../webapp/app/auth/http-auth.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../generator/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Injectable } from '@angular/core'; | ||
import Keycloak, { KeycloakConfig, KeycloakInstance } from 'keycloak-js'; | ||
import { from, Observable, tap } from 'rxjs'; | ||
import { environment } from '../../environments/environment'; | ||
|
||
const MIN_TOKEN_VALIDITY_SECONDS = 70; | ||
const REFRESH_TOKEN_TIMEOUT_MS = 6000; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class Oauth2AuthService { | ||
private keycloak!: KeycloakInstance; | ||
|
||
get token(): string | undefined { | ||
return this.keycloak.token; | ||
} | ||
|
||
get isAuthenticated(): boolean | undefined { | ||
return this.keycloak.authenticated; | ||
} | ||
|
||
initAuthentication(): Observable<boolean> { | ||
const config: KeycloakConfig = { | ||
url: environment.keycloak.url, | ||
realm: environment.keycloak.realm, | ||
clientId: environment.keycloak.client_id, | ||
}; | ||
this.keycloak = Keycloak(config); | ||
|
||
return from(this.keycloak.init({ onLoad: 'login-required', checkLoginIframe: false })).pipe( | ||
tap(authenticated => { | ||
if (!authenticated) { | ||
window.location.reload(); | ||
} else { | ||
console.debug('Authenticated'); | ||
} | ||
}), | ||
tap(() => { | ||
this.initUpdateTokenRefresh(); | ||
}) | ||
); | ||
} | ||
|
||
logout(): void { | ||
this.keycloak.logout(); | ||
} | ||
|
||
private initUpdateTokenRefresh(): void { | ||
setInterval( | ||
() => | ||
this.keycloak | ||
.updateToken(MIN_TOKEN_VALIDITY_SECONDS) | ||
.then(refreshed => { | ||
if (refreshed) { | ||
console.debug('Token refreshed'); | ||
} else { | ||
const exp = this.keycloak.tokenParsed!.exp!; | ||
const timeSkew = this.keycloak.timeSkew!; | ||
console.debug('Token not refreshed, valid for ' + Math.round(exp + timeSkew - Date.now() / 1000) + ' seconds'); | ||
} | ||
}) | ||
.catch(e => console.error('Failed to refresh token: ' + e)), | ||
REFRESH_TOKEN_TIMEOUT_MS | ||
); | ||
} | ||
} |
62 changes: 0 additions & 62 deletions
62
...r/client/angular/security/oauth2/src/main/webapp/app/auth/oauth2-auth.service.ts.mustache
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
...app/app/login/login.component.ts.mustache → .../main/webapp/app/login/login.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.