Skip to content

Commit

Permalink
fix: multiplying calls to token endpoint in code flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceteareth committed Oct 21, 2020
1 parent 8d152c2 commit 59f65d2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion projects/lib/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
protected accessTokenTimeoutSubscription: Subscription;
protected idTokenTimeoutSubscription: Subscription;
protected tokenReceivedSubscription: Subscription;
protected automaticRefreshSubscription: Subscription;
protected sessionCheckEventListener: EventListener;
protected jwksUri: string;
protected sessionCheckTimer: any;
Expand Down Expand Up @@ -222,7 +223,8 @@ export class OAuthService extends AuthConfig implements OnDestroy {
noPrompt = true
): void {
let shouldRunSilentRefresh = true;
this.events
this.clearAutomaticRefreshTimer();
this.automaticRefreshSubscription = this.events
.pipe(
tap(e => {
if (e.type === 'token_received') {
Expand Down Expand Up @@ -448,6 +450,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
public stopAutomaticRefresh() {
this.clearAccessTokenTimer();
this.clearIdTokenTimer();
this.clearAutomaticRefreshTimer();
}

protected clearAccessTokenTimer(): void {
Expand All @@ -462,6 +465,12 @@ export class OAuthService extends AuthConfig implements OnDestroy {
}
}

protected clearAutomaticRefreshTimer(): void {
if (this.automaticRefreshSubscription) {
this.automaticRefreshSubscription.unsubscribe();
}
}

protected calcTimeout(storedAt: number, expiration: number): number {
const now = Date.now();
const delta =
Expand Down
8 changes: 8 additions & 0 deletions projects/sample/src/app/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,13 @@ <h2>Further Actions</h2>
<button class="btn btn-default" (click)="loadUserProfile()">
Load User Profile
</button>

<button class="btn btn-default" (click)="startAutomaticRefresh()">
Start automatic refresh
</button>

<button class="btn btn-default" (click)="stopAutomaticRefresh()">
Stop automatic refresh
</button>
</div>
</div>
8 changes: 8 additions & 0 deletions projects/sample/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ export class HomeComponent implements OnInit {
this.oauthService.loadUserProfile().then(up => (this.userProfile = up));
}

startAutomaticRefresh(): void {
this.oauthService.setupAutomaticSilentRefresh();
}

stopAutomaticRefresh(): void {
this.oauthService.stopAutomaticRefresh();
}

get givenName() {
var claims = this.oauthService.getIdentityClaims();
if (!claims) return null;
Expand Down

0 comments on commit 59f65d2

Please sign in to comment.