Skip to content

Commit d007a52

Browse files
committed
fixed #103, #101, #99
1 parent 63999a9 commit d007a52

File tree

71 files changed

+2442
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2442
-20
lines changed

Diff for: angular-oauth2-oidc/src/base64-helper.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// see: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22
22
export function b64DecodeUnicode(str) {
3-
return decodeURIComponent(atob(str).split('').map(function(c) {
3+
4+
let base64 = str.replace(/\-/g, '+').replace(/\_/g, '/');
5+
6+
return decodeURIComponent(atob(base64).split('').map(function(c) {
47
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
58
}).join(''));
69
}

Diff for: angular-oauth2-oidc/src/oauth-service.ts

+44-18
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ export class OAuthService
9292

9393
this.setupRefreshTimer();
9494

95-
if (this.sessionChecksEnabled) {
96-
this.restartSessionChecksIfStillLoggedIn();
97-
}
98-
99-
this.restartRefreshTimerIfStillLoggedIn();
95+
10096
}
10197

10298
/**
@@ -113,9 +109,14 @@ export class OAuthService
113109
if (this.sessionChecksEnabled) {
114110
this.setupSessionCheck();
115111
}
112+
113+
this.configChanged();
116114
}
117115

118-
private restartSessionChecksIfStillLoggedIn(): void {
116+
private configChanged(): void {
117+
}
118+
119+
public restartSessionChecksIfStillLoggedIn(): void {
119120
if (this.hasValidIdToken()) {
120121
this.initSessionCheck();
121122
}
@@ -145,8 +146,12 @@ export class OAuthService
145146
.events
146147
.filter(e => e.type === 'token_expires')
147148
.subscribe(e => {
148-
this.silentRefresh();
149+
this.silentRefresh().catch(_ => {
150+
this.debug('automatic silent refresh did not work');
151+
})
149152
});
153+
154+
this.restartRefreshTimerIfStillLoggedIn();
150155
}
151156

152157
public loadDiscoveryDocumentAndTryLogin() {
@@ -227,7 +232,8 @@ export class OAuthService
227232

228233
private setupAccessTokenTimer(): void {
229234
let expiration = this.getAccessTokenExpiration();
230-
let timeout = this.calcTimeout(expiration);
235+
let storedAt = this.getAccessTokenStoredAt();
236+
let timeout = this.calcTimeout(storedAt, expiration);
231237

232238
this.accessTokenTimeoutSubscription =
233239
Observable
@@ -239,7 +245,8 @@ export class OAuthService
239245

240246
private setupIdTokenTimer(): void {
241247
let expiration = this.getIdTokenExpiration();
242-
let timeout = this.calcTimeout(expiration);
248+
let storedAt = this.getIdTokenStoredAt();
249+
let timeout = this.calcTimeout(storedAt, expiration);
243250

244251
this.idTokenTimeoutSubscription =
245252
Observable
@@ -260,10 +267,8 @@ export class OAuthService
260267
}
261268
}
262269

263-
private calcTimeout(expiration: number): number {
264-
let now = Date.now();
265-
let delta = (expiration - now) * this.timeoutFactor;
266-
// let timeout = now + delta;
270+
private calcTimeout(storedAt: number, expiration: number): number {
271+
let delta = (expiration - storedAt) * this.timeoutFactor;
267272
return delta;
268273
}
269274

@@ -276,6 +281,7 @@ export class OAuthService
276281
*/
277282
public setStorage(storage: OAuthStorage): void {
278283
this._storage = storage;
284+
this.configChanged();
279285
}
280286

281287
/**
@@ -292,7 +298,11 @@ export class OAuthService
292298
return new Promise((resolve, reject) => {
293299

294300
if (!fullUrl) {
295-
fullUrl = this.issuer + '/.well-known/openid-configuration';
301+
fullUrl = this.issuer || '';
302+
if (!fullUrl.endsWith('/')) {
303+
fullUrl += '/';
304+
}
305+
fullUrl += '.well-known/openid-configuration';
296306
}
297307

298308
if (!this.validateUrlForHttps(fullUrl)) {
@@ -321,6 +331,10 @@ export class OAuthService
321331
this.discoveryDocumentLoaded = true;
322332
this.discoveryDocumentLoadedSubject.next(doc);
323333

334+
if (this.sessionChecksEnabled) {
335+
this.restartSessionChecksIfStillLoggedIn();
336+
}
337+
324338
this.loadJwks().then(jwks => {
325339
let result: object = {
326340
discoveryDocument: doc,
@@ -965,7 +979,7 @@ export class OAuthService
965979

966980
private storeAccessTokenResponse(accessToken: string, refreshToken: string, expiresIn: number): void {
967981
this._storage.setItem('access_token', accessToken);
968-
982+
this._storage.setItem('access_token_stored_at', '' + Date.now());
969983
if (expiresIn) {
970984
let expiresInMilliSeconds = expiresIn * 1000;
971985
let now = new Date();
@@ -1092,9 +1106,10 @@ export class OAuthService
10921106
this._storage.setItem('id_token', idToken.idToken);
10931107
this._storage.setItem('id_token_claims_obj', idToken.idTokenClaimsJson);
10941108
this._storage.setItem('id_token_expires_at', '' + idToken.idTokenExpiresAt);
1109+
this._storage.setItem('id_token_stored_at', '' + Date.now());
10951110
}
10961111

1097-
protected storeSessionState(sessionState: string) {
1112+
protected storeSessionState(sessionState: string): void {
10981113
this._storage.setItem('session_state', sessionState);
10991114
}
11001115

@@ -1273,6 +1288,15 @@ export class OAuthService
12731288
return parseInt(this._storage.getItem('expires_at'), 10);
12741289
}
12751290

1291+
1292+
private getAccessTokenStoredAt(): number {
1293+
return parseInt(this._storage.getItem('access_token_stored_at'), 10);
1294+
}
1295+
1296+
private getIdTokenStoredAt(): number {
1297+
return parseInt(this._storage.getItem('id_token_stored_at'), 10);
1298+
}
1299+
12761300
/**
12771301
* Returns the expiration date of the id_token
12781302
* as milliseconds since 1970.
@@ -1340,7 +1364,9 @@ export class OAuthService
13401364
this._storage.removeItem('expires_at');
13411365
this._storage.removeItem('id_token_claims_obj');
13421366
this._storage.removeItem('id_token_expires_at');
1343-
1367+
this._storage.removeItem('id_token_stored_at');
1368+
this._storage.removeItem('access_token_stored_at');
1369+
13441370
this.silentRefreshSubject = null;
13451371

13461372
if (!this.logoutUrl) return;
@@ -1350,7 +1376,7 @@ export class OAuthService
13501376
let logoutUrl: string;
13511377

13521378
if (!this.validateUrlForHttps(this.logoutUrl)) throw new Error('logoutUrl must use Http. Also check property requireHttps.');
1353-
1379+
13541380
// For backward compatibility
13551381
if (this.logoutUrl.indexOf('{{') > -1) {
13561382
logoutUrl = this.logoutUrl.replace(/\{\{id_token\}\}/, id_token);

Diff for: angular-oauth2-oidc/src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-oauth2-oidc",
3-
"version": "2.1.1",
3+
"version": "2.1.2",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/manfredsteyer/angular-oauth2-oidc"

Diff for: sample - Kopie/.angular-cli.json

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
3+
"project": {
4+
"name": "sample2"
5+
},
6+
"apps": [
7+
{
8+
"root": "src",
9+
"outDir": "dist",
10+
"assets": [
11+
"assets",
12+
"favicon.ico",
13+
"silent-refresh.html"
14+
],
15+
"index": "index.html",
16+
"main": "main.ts",
17+
"polyfills": "polyfills.ts",
18+
"test": "test.ts",
19+
"tsconfig": "tsconfig.app.json",
20+
"testTsconfig": "tsconfig.spec.json",
21+
"prefix": "app",
22+
"styles": [
23+
"styles.css",
24+
"../node_modules/bootstrap/dist/css/bootstrap.css"
25+
],
26+
"scripts": [],
27+
"environmentSource": "environments/environment.ts",
28+
"environments": {
29+
"dev": "environments/environment.ts",
30+
"prod": "environments/environment.prod.ts"
31+
}
32+
}
33+
],
34+
"e2e": {
35+
"protractor": {
36+
"config": "./protractor.conf.js"
37+
}
38+
},
39+
"lint": [
40+
{
41+
"project": "src/tsconfig.app.json",
42+
"exclude": "**/node_modules/**"
43+
},
44+
{
45+
"project": "src/tsconfig.spec.json",
46+
"exclude": "**/node_modules/**"
47+
},
48+
{
49+
"project": "e2e/tsconfig.e2e.json",
50+
"exclude": "**/node_modules/**"
51+
}
52+
],
53+
"test": {
54+
"karma": {
55+
"config": "./karma.conf.js"
56+
}
57+
},
58+
"defaults": {
59+
"styleExt": "css",
60+
"component": {}
61+
}
62+
}

Diff for: sample - Kopie/.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

Diff for: sample - Kopie/.gitignore

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
8+
# dependencies
9+
/node_modules
10+
11+
# IDEs and editors
12+
/.idea
13+
.project
14+
.classpath
15+
.c9/
16+
*.launch
17+
.settings/
18+
*.sublime-workspace
19+
20+
# IDE - VSCode
21+
.vscode/*
22+
!.vscode/settings.json
23+
!.vscode/tasks.json
24+
!.vscode/launch.json
25+
!.vscode/extensions.json
26+
27+
# misc
28+
/.sass-cache
29+
/connect.lock
30+
/coverage
31+
/libpeerconnection.log
32+
npm-debug.log
33+
testem.log
34+
/typings
35+
yarn-error.log
36+
37+
# e2e
38+
/e2e/*.js
39+
/e2e/*.map
40+
41+
# System Files
42+
.DS_Store
43+
Thumbs.db

Diff for: sample - Kopie/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Sample2
2+
3+
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.3.1.
4+
5+
## Development server
6+
7+
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
8+
9+
## Code scaffolding
10+
11+
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
12+
13+
## Build
14+
15+
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `-prod` flag for a production build.
16+
17+
## Running unit tests
18+
19+
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
20+
21+
## Running end-to-end tests
22+
23+
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
24+
Before running the tests make sure you are serving the app via `ng serve`.
25+
26+
## Further help
27+
28+
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).

Diff for: sample - Kopie/e2e/app.e2e-spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { AppPage } from './app.po';
2+
3+
describe('sample2 App', () => {
4+
let page: AppPage;
5+
6+
beforeEach(() => {
7+
page = new AppPage();
8+
});
9+
10+
it('should display welcome message', () => {
11+
page.navigateTo();
12+
expect(page.getParagraphText()).toEqual('Welcome to app!');
13+
});
14+
});

Diff for: sample - Kopie/e2e/app.po.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { browser, by, element } from 'protractor';
2+
3+
export class AppPage {
4+
navigateTo() {
5+
return browser.get('/');
6+
}
7+
8+
getParagraphText() {
9+
return element(by.css('app-root h1')).getText();
10+
}
11+
}

Diff for: sample - Kopie/e2e/tsconfig.e2e.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../out-tsc/e2e",
5+
"baseUrl": "./",
6+
"module": "commonjs",
7+
"target": "es5",
8+
"types": [
9+
"jasmine",
10+
"jasminewd2",
11+
"node"
12+
]
13+
}
14+
}

0 commit comments

Comments
 (0)