• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

damienbod / angular-auth-oidc-client / 5117794006

pending completion
5117794006

push

github

web-flow
Merge pull request #1639 from timdeschryver/example/standalone

docs: add standalone example

656 of 692 branches covered (94.8%)

Branch coverage included in aggregate %.

2420 of 2477 relevant lines covered (97.7%)

8.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.98
/projects/angular-auth-oidc-client/src/lib/iframe/silent-renew.service.ts
1
import { HttpParams } from '@angular/common/http';
2
import { Injectable } from '@angular/core';
3
import { Observable, Subject, throwError } from 'rxjs';
4
import { catchError } from 'rxjs/operators';
5
import { AuthStateService } from '../auth-state/auth-state.service';
6
import { ImplicitFlowCallbackService } from '../callback/implicit-flow-callback.service';
7
import { IntervalService } from '../callback/interval.service';
8
import { CallbackContext } from '../flows/callback-context';
9
import { FlowsDataService } from '../flows/flows-data.service';
10
import { FlowsService } from '../flows/flows.service';
11
import { ResetAuthDataService } from '../flows/reset-auth-data.service';
12
import { LoggerService } from '../logging/logger.service';
13
import { FlowHelper } from '../utils/flowHelper/flow-helper.service';
14
import { ValidationResult } from '../validation/validation-result';
15
import { OpenIdConfiguration } from '../config/openid-configuration';
16
import { IFrameService } from './existing-iframe.service';
17

18
const IFRAME_FOR_SILENT_RENEW_IDENTIFIER = 'myiFrameForSilentRenew';
1✔
19

20
@Injectable({ providedIn: 'root' })
21
export class SilentRenewService {
1✔
22
  private readonly refreshSessionWithIFrameCompletedInternal$ = new Subject<CallbackContext>();
16✔
23

24
  get refreshSessionWithIFrameCompleted$(): Observable<CallbackContext> {
25
    return this.refreshSessionWithIFrameCompletedInternal$.asObservable();
52✔
26
  }
27

28
  constructor(
29
    private readonly iFrameService: IFrameService,
16✔
30
    private readonly flowsService: FlowsService,
16✔
31
    private readonly resetAuthDataService: ResetAuthDataService,
16✔
32
    private readonly flowsDataService: FlowsDataService,
16✔
33
    private readonly authStateService: AuthStateService,
16✔
34
    private readonly loggerService: LoggerService,
16✔
35
    private readonly flowHelper: FlowHelper,
16✔
36
    private readonly implicitFlowCallbackService: ImplicitFlowCallbackService,
16✔
37
    private readonly intervalService: IntervalService
16✔
38
  ) {}
39

40
  getOrCreateIframe(config: OpenIdConfiguration): HTMLIFrameElement {
41
    const existingIframe = this.getExistingIframe();
2✔
42

43
    if (!existingIframe) {
2✔
44
      return this.iFrameService.addIFrameToWindowBody(IFRAME_FOR_SILENT_RENEW_IDENTIFIER, config);
1✔
45
    }
46

47
    return existingIframe;
1✔
48
  }
49

50
  isSilentRenewConfigured(configuration: OpenIdConfiguration): boolean {
51
    const { useRefreshToken, silentRenew } = configuration;
3✔
52

53
    return !useRefreshToken && silentRenew;
3✔
54
  }
55

56
  codeFlowCallbackSilentRenewIframe(
57
    urlParts: string[],
58
    config: OpenIdConfiguration,
59
    allConfigs: OpenIdConfiguration[]
60
  ): Observable<CallbackContext> {
61
    const params = new HttpParams({
2✔
62
      fromString: urlParts[1],
63
    });
64

65
    const error = params.get('error');
2✔
66

67
    if (error) {
2✔
68
      this.authStateService.updateAndPublishAuthState({
1✔
69
        isAuthenticated: false,
70
        validationResult: ValidationResult.LoginRequired,
71
        isRenewProcess: true,
72
      });
73
      this.resetAuthDataService.resetAuthorizationData(config, allConfigs);
1✔
74
      this.flowsDataService.setNonce('', config);
1✔
75
      this.intervalService.stopPeriodicTokenCheck();
1✔
76

77
      return throwError(() => new Error(error));
1✔
78
    }
79

80
    const code = params.get('code');
1✔
81
    const state = params.get('state');
1✔
82
    const sessionState = params.get('session_state');
1✔
83

84
    const callbackContext = {
1✔
85
      code,
86
      refreshToken: null,
87
      state,
88
      sessionState,
89
      authResult: null,
90
      isRenewProcess: true,
91
      jwtKeys: null,
92
      validationResult: null,
93
      existingIdToken: null,
94
    };
95

96
    return this.flowsService.processSilentRenewCodeFlowCallback(callbackContext, config, allConfigs).pipe(
1✔
97
      catchError(() => {
98
        this.intervalService.stopPeriodicTokenCheck();
×
99
        this.resetAuthDataService.resetAuthorizationData(config, allConfigs);
×
100

101
        return throwError(() => new Error(error));
×
102
      })
103
    );
104
  }
105

106
  silentRenewEventHandler(e: CustomEvent, config: OpenIdConfiguration, allConfigs: OpenIdConfiguration[]): void {
107
    this.loggerService.logDebug(config, 'silentRenewEventHandler');
7✔
108
    if (!e.detail) {
7✔
109
      return;
1✔
110
    }
111

112
    let callback$: Observable<CallbackContext>;
113
    const isCodeFlow = this.flowHelper.isCurrentFlowCodeFlow(config);
6✔
114

115
    if (isCodeFlow) {
6✔
116
      const urlParts = e.detail.toString().split('?');
5✔
117

118
      callback$ = this.codeFlowCallbackSilentRenewIframe(urlParts, config, allConfigs);
5✔
119
    } else {
120
      callback$ = this.implicitFlowCallbackService.authenticatedImplicitFlowCallback(config, allConfigs, e.detail);
1✔
121
    }
122

123
    callback$.subscribe({
6✔
124
      next: (callbackContext) => {
125
        this.refreshSessionWithIFrameCompletedInternal$.next(callbackContext);
4✔
126
        this.flowsDataService.resetSilentRenewRunning(config);
4✔
127
      },
128
      error: (err: any) => {
129
        this.loggerService.logError(config, 'Error: ' + err);
2✔
130
        this.refreshSessionWithIFrameCompletedInternal$.next(null);
2✔
131
        this.flowsDataService.resetSilentRenewRunning(config);
2✔
132
      },
133
    });
134
  }
135

136
  private getExistingIframe(): HTMLIFrameElement {
137
    return this.iFrameService.getExistingIFrame(IFRAME_FOR_SILENT_RENEW_IDENTIFIER);
×
138
  }
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc