Skip to content

Commit

Permalink
fixed #103, #101, #99
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredsteyer committed Sep 13, 2017
1 parent 63999a9 commit d007a52
Show file tree
Hide file tree
Showing 71 changed files with 2,442 additions and 20 deletions.
5 changes: 4 additions & 1 deletion angular-oauth2-oidc/src/base64-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// see: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22
export function b64DecodeUnicode(str) {
return decodeURIComponent(atob(str).split('').map(function(c) {

let base64 = str.replace(/\-/g, '+').replace(/\_/g, '/');

return decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
62 changes: 44 additions & 18 deletions angular-oauth2-oidc/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ export class OAuthService

this.setupRefreshTimer();

if (this.sessionChecksEnabled) {
this.restartSessionChecksIfStillLoggedIn();
}

this.restartRefreshTimerIfStillLoggedIn();

}

/**
Expand All @@ -113,9 +109,14 @@ export class OAuthService
if (this.sessionChecksEnabled) {
this.setupSessionCheck();
}

this.configChanged();
}

private restartSessionChecksIfStillLoggedIn(): void {
private configChanged(): void {
}

public restartSessionChecksIfStillLoggedIn(): void {
if (this.hasValidIdToken()) {
this.initSessionCheck();
}
Expand Down Expand Up @@ -145,8 +146,12 @@ export class OAuthService
.events
.filter(e => e.type === 'token_expires')
.subscribe(e => {
this.silentRefresh();
this.silentRefresh().catch(_ => {
this.debug('automatic silent refresh did not work');
})
});

this.restartRefreshTimerIfStillLoggedIn();
}

public loadDiscoveryDocumentAndTryLogin() {
Expand Down Expand Up @@ -227,7 +232,8 @@ export class OAuthService

private setupAccessTokenTimer(): void {
let expiration = this.getAccessTokenExpiration();
let timeout = this.calcTimeout(expiration);
let storedAt = this.getAccessTokenStoredAt();
let timeout = this.calcTimeout(storedAt, expiration);

this.accessTokenTimeoutSubscription =
Observable
Expand All @@ -239,7 +245,8 @@ export class OAuthService

private setupIdTokenTimer(): void {
let expiration = this.getIdTokenExpiration();
let timeout = this.calcTimeout(expiration);
let storedAt = this.getIdTokenStoredAt();
let timeout = this.calcTimeout(storedAt, expiration);

this.idTokenTimeoutSubscription =
Observable
Expand All @@ -260,10 +267,8 @@ export class OAuthService
}
}

private calcTimeout(expiration: number): number {
let now = Date.now();
let delta = (expiration - now) * this.timeoutFactor;
// let timeout = now + delta;
private calcTimeout(storedAt: number, expiration: number): number {
let delta = (expiration - storedAt) * this.timeoutFactor;
return delta;
}

Expand All @@ -276,6 +281,7 @@ export class OAuthService
*/
public setStorage(storage: OAuthStorage): void {
this._storage = storage;
this.configChanged();
}

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

if (!fullUrl) {
fullUrl = this.issuer + '/.well-known/openid-configuration';
fullUrl = this.issuer || '';
if (!fullUrl.endsWith('/')) {
fullUrl += '/';
}
fullUrl += '.well-known/openid-configuration';
}

if (!this.validateUrlForHttps(fullUrl)) {
Expand Down Expand Up @@ -321,6 +331,10 @@ export class OAuthService
this.discoveryDocumentLoaded = true;
this.discoveryDocumentLoadedSubject.next(doc);

if (this.sessionChecksEnabled) {
this.restartSessionChecksIfStillLoggedIn();
}

this.loadJwks().then(jwks => {
let result: object = {
discoveryDocument: doc,
Expand Down Expand Up @@ -965,7 +979,7 @@ export class OAuthService

private storeAccessTokenResponse(accessToken: string, refreshToken: string, expiresIn: number): void {
this._storage.setItem('access_token', accessToken);

this._storage.setItem('access_token_stored_at', '' + Date.now());
if (expiresIn) {
let expiresInMilliSeconds = expiresIn * 1000;
let now = new Date();
Expand Down Expand Up @@ -1092,9 +1106,10 @@ export class OAuthService
this._storage.setItem('id_token', idToken.idToken);
this._storage.setItem('id_token_claims_obj', idToken.idTokenClaimsJson);
this._storage.setItem('id_token_expires_at', '' + idToken.idTokenExpiresAt);
this._storage.setItem('id_token_stored_at', '' + Date.now());
}

protected storeSessionState(sessionState: string) {
protected storeSessionState(sessionState: string): void {
this._storage.setItem('session_state', sessionState);
}

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


private getAccessTokenStoredAt(): number {
return parseInt(this._storage.getItem('access_token_stored_at'), 10);
}

private getIdTokenStoredAt(): number {
return parseInt(this._storage.getItem('id_token_stored_at'), 10);
}

/**
* Returns the expiration date of the id_token
* as milliseconds since 1970.
Expand Down Expand Up @@ -1340,7 +1364,9 @@ export class OAuthService
this._storage.removeItem('expires_at');
this._storage.removeItem('id_token_claims_obj');
this._storage.removeItem('id_token_expires_at');

this._storage.removeItem('id_token_stored_at');
this._storage.removeItem('access_token_stored_at');

this.silentRefreshSubject = null;

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

if (!this.validateUrlForHttps(this.logoutUrl)) throw new Error('logoutUrl must use Http. Also check property requireHttps.');

// For backward compatibility
if (this.logoutUrl.indexOf('{{') > -1) {
logoutUrl = this.logoutUrl.replace(/\{\{id_token\}\}/, id_token);
Expand Down
2 changes: 1 addition & 1 deletion angular-oauth2-oidc/src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-oauth2-oidc",
"version": "2.1.1",
"version": "2.1.2",
"repository": {
"type": "git",
"url": "https://github.com/manfredsteyer/angular-oauth2-oidc"
Expand Down
62 changes: 62 additions & 0 deletions sample - Kopie/.angular-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"project": {
"name": "sample2"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico",
"silent-refresh.html"
],
"index": "index.html",
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "app",
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.css"
],
"scripts": [],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"lint": [
{
"project": "src/tsconfig.app.json",
"exclude": "**/node_modules/**"
},
{
"project": "src/tsconfig.spec.json",
"exclude": "**/node_modules/**"
},
{
"project": "e2e/tsconfig.e2e.json",
"exclude": "**/node_modules/**"
}
],
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"component": {}
}
}
13 changes: 13 additions & 0 deletions sample - Kopie/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
43 changes: 43 additions & 0 deletions sample - Kopie/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp
/out-tsc

# dependencies
/node_modules

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
testem.log
/typings
yarn-error.log

# e2e
/e2e/*.js
/e2e/*.map

# System Files
.DS_Store
Thumbs.db
28 changes: 28 additions & 0 deletions sample - Kopie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Sample2

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.3.1.

## Development server

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.

## Code scaffolding

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`.

## Build

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.

## Running unit tests

Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Running end-to-end tests

Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
Before running the tests make sure you are serving the app via `ng serve`.

## Further help

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).
14 changes: 14 additions & 0 deletions sample - Kopie/e2e/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AppPage } from './app.po';

describe('sample2 App', () => {
let page: AppPage;

beforeEach(() => {
page = new AppPage();
});

it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to app!');
});
});
11 changes: 11 additions & 0 deletions sample - Kopie/e2e/app.po.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { browser, by, element } from 'protractor';

export class AppPage {
navigateTo() {
return browser.get('/');
}

getParagraphText() {
return element(by.css('app-root h1')).getText();
}
}
14 changes: 14 additions & 0 deletions sample - Kopie/e2e/tsconfig.e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/e2e",
"baseUrl": "./",
"module": "commonjs",
"target": "es5",
"types": [
"jasmine",
"jasminewd2",
"node"
]
}
}
Loading

0 comments on commit d007a52

Please sign in to comment.