Skip to content

Commit b8377aa

Browse files
author
NEXUS\Anthony.Giretti
committed
add iframe silent refresh
1 parent e7941ee commit b8377aa

File tree

8 files changed

+81
-29
lines changed

8 files changed

+81
-29
lines changed

src/app/app.component.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component } from '@angular/core';
2+
import $ from 'jquery';
23

34
@Component({
45
selector: 'app-root',
@@ -7,4 +8,13 @@ import { Component } from '@angular/core';
78
})
89
export class AppComponent {
910
title = 'Demo Open Id Connect with Azure AD Connect';
11+
12+
13+
constructor() {
14+
}
15+
16+
public call()
17+
{
18+
19+
}
1020
}

src/app/auth-callback/auth-callback.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AuthService } from './../services/auth.service';
2-
import { Router } from '@angular/router';
2+
import { Router, ActivatedRoute } from '@angular/router';
33
import { Component, OnInit, NgZone } from '@angular/core';
44

55
@Component({
@@ -9,9 +9,14 @@ import { Component, OnInit, NgZone } from '@angular/core';
99
})
1010
export class AuthCallbackComponent implements OnInit {
1111

12-
constructor(private _router:Router, private _authService: AuthService, private _zone: NgZone) { }
12+
constructor(private _router:Router, private _authService: AuthService, private _zone: NgZone, private _activatedRoute: ActivatedRoute) { }
1313

1414
ngOnInit() {
15+
16+
17+
console.log(document.location.href);
18+
19+
1520
this._authService.completeAuthentication().then(() => {
1621
this._zone.run(
1722
() => this._router.navigate(['/protected'])

src/app/openIdConnectConfig.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export function getClientSettings(): UserManagerSettings {
1313
filterProtocolClaims: true,
1414
loadUserInfo: true,
1515
automaticSilentRenew: true,
16-
silent_redirect_uri: 'http://localhost:4200/assets/silent-refresh.html',
16+
clockSkew: 0,
17+
accessTokenExpiringNotificationTime: 570,
18+
silent_redirect_uri: 'http://localhost:4200/silent-refresh.html',
1719
metadata: {
1820
issuer: "https://sts.windows.net/136544d9-038e-4646-afff-10accb370679/",
1921
authorization_endpoint: "https://login.microsoftonline.com/136544d9-038e-4646-afff-10accb370679/oauth2/authorize",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<p>
22
Welcome {{name}}
33
</p>
4+
5+
6+
7+
<iframe id="test" name="frame1" src="https://login.microsoftonline.com/136544d9-038e-4646-afff-10accb370679/oauth2/authorize?client_id=257b6c36-1168-4aac-be93-6f2cd81cec43&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fauth-callback&response_type=id_token&scope=openid%20profile&state=1d56619048f94af587af8e35529735d1&nonce=89ddb46c69c047caa26fdc44a938eb45&prompt=none"></iframe>

src/app/protected/protected.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export class ProtectedComponent implements OnInit {
1515
console.log(this._authService.isLoggedIn());
1616
console.log(this._authService.getUser());
1717
this.name = this._authService.getName();
18+
19+
this._authService.subscribeevents();
1820
}
1921

2022
}

src/app/services/auth.service.ts

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,37 @@ export class AuthService {
99
private _user: User = null;
1010

1111
constructor() {
12-
this._manager.getUser().then(user => {
13-
this._user = user;
14-
});
12+
13+
// this._manager.getUser().then(user => {
14+
// var token = this.parseJwt(user.id_token);
15+
// user.expires_at = token.exp;
16+
// console.log(user.expires_at);
17+
// this._user = user;
18+
// });
1519

1620
this._manager.events.addUserLoaded(user => {
21+
var token = this.parseJwt(user.id_token);
22+
user.expires_at = token.exp;
23+
console.log(user.expires_at);
1724
this._user = user;
25+
var that = this;
26+
this.subscribeevents();
1827
});
1928

29+
//
30+
31+
32+
}
33+
34+
public isLoggedIn(): boolean {
35+
return this._user != null && !this._user.expired;
36+
}
37+
38+
public getClaims(): any {
39+
return this._user.profile;
40+
}
41+
42+
public subscribeevents() :void {
2043
this._manager.events.addSilentRenewError(() => {
2144
console.log("error SilentRenew");
2245
});
@@ -28,15 +51,17 @@ export class AuthService {
2851
this._manager.events.addAccessTokenExpired(() => {
2952
console.log("access token expired");
3053
});
31-
3254
}
3355

34-
public isLoggedIn(): boolean {
35-
return this._user != null && !this._user.expired;
36-
}
37-
38-
public getClaims(): any {
39-
return this._user.profile;
56+
public refreshCallBack(): void
57+
{
58+
console.log("start refresh callback");
59+
this._manager.signinSilentCallback()
60+
.then(data => {console.log("suucess callback")})
61+
.catch(err => {
62+
console.log("err callback");
63+
});
64+
console.log("end refresh callback");
4065
}
4166

4267
getUser(): any {
@@ -62,4 +87,17 @@ export class AuthService {
6287

6388

6489
}
90+
91+
parseJwt (token) {
92+
var base64Url = token.split('.')[1];
93+
var base64 = base64Url.replace('-', '+').replace('_', '/');
94+
return JSON.parse(window.atob(base64));
95+
};
96+
97+
98+
public Call()
99+
{
100+
//this._http.
101+
}
102+
65103
}
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { UserManager } from 'oidc-client';
33
import { getClientSettings } from '../openIdConnectConfig';
4+
import { AuthService } from '../services/auth.service';
45

56
@Component({
67
selector: 'app-silentrefresh',
@@ -9,24 +10,14 @@ import { getClientSettings } from '../openIdConnectConfig';
910
})
1011
export class SilentRefreshComponent implements OnInit {
1112

12-
private _manager = new UserManager(getClientSettings());
13-
14-
constructor() {
13+
14+
constructor(private _authService:AuthService) {
15+
1516
}
1617

1718
ngOnInit() {
18-
19+
this._authService.refreshCallBack();
1920
}
2021

21-
public refresh(): void
22-
{
23-
console.log("start refresh");
24-
this._manager.signinSilentCallback()
25-
.then(data => {console.log(data)})
26-
.catch((err) => {
27-
console.log(err);
28-
});
29-
console.log("end refresh");
30-
}
3122

32-
}
23+
}

src/assets/silent-refresh.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<title></title>
33
</head>
44
<body>
5-
<script src="oidc-client.min.js"></script>
5+
<script src="./oidc-client.min.js"></script>
66
<script>
77
new UserManager().signinSilentCallback()
88
.catch((err) => {

0 commit comments

Comments
 (0)