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

damienbod / angular-auth-oidc-client / 8381585806

21 Mar 2024 08:50PM UTC coverage: 97.057%. Remained the same
8381585806

Pull #1915

github

web-flow
Merge e3b326d5e into 6c778a4a2
Pull Request #1915: chore(deps-dev): bump webpack-dev-middleware from 5.3.3 to 5.3.4

661 of 698 branches covered (94.7%)

Branch coverage included in aggregate %.

2472 of 2530 relevant lines covered (97.71%)

8.6 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$ =
16✔
23
    new Subject<CallbackContext>();
24

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

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

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

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

51
    return existingIframe;
1✔
52
  }
53

54
  isSilentRenewConfigured(configuration: OpenIdConfiguration): boolean {
55
    const { useRefreshToken, silentRenew } = configuration;
3✔
56

57
    return !useRefreshToken && silentRenew;
3✔
58
  }
59

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

69
    const error = params.get('error');
2✔
70

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

81
      return throwError(() => new Error(error));
1✔
82
    }
83

84
    const code = params.get('code');
1✔
85
    const state = params.get('state');
1✔
86
    const sessionState = params.get('session_state');
1✔
87

88
    const callbackContext = {
1✔
89
      code,
90
      refreshToken: null,
91
      state,
92
      sessionState,
93
      authResult: null,
94
      isRenewProcess: true,
95
      jwtKeys: null,
96
      validationResult: null,
97
      existingIdToken: null,
98
    };
99

100
    return this.flowsService
1✔
101
      .processSilentRenewCodeFlowCallback(callbackContext, config, allConfigs)
102
      .pipe(
103
        catchError(() => {
104
          this.intervalService.stopPeriodicTokenCheck();
×
105
          this.resetAuthDataService.resetAuthorizationData(config, allConfigs);
×
106

107
          return throwError(() => new Error(error));
×
108
        })
109
      );
110
  }
111

112
  silentRenewEventHandler(
113
    e: CustomEvent,
114
    config: OpenIdConfiguration,
115
    allConfigs: OpenIdConfiguration[]
116
  ): void {
117
    this.loggerService.logDebug(config, 'silentRenewEventHandler');
7✔
118
    if (!e.detail) {
7✔
119
      return;
1✔
120
    }
121

122
    let callback$: Observable<CallbackContext>;
123
    const isCodeFlow = this.flowHelper.isCurrentFlowCodeFlow(config);
6✔
124

125
    if (isCodeFlow) {
6✔
126
      const urlParts = e.detail.toString().split('?');
5✔
127

128
      callback$ = this.codeFlowCallbackSilentRenewIframe(
5✔
129
        urlParts,
130
        config,
131
        allConfigs
132
      );
133
    } else {
134
      callback$ =
1✔
135
        this.implicitFlowCallbackService.authenticatedImplicitFlowCallback(
136
          config,
137
          allConfigs,
138
          e.detail
139
        );
140
    }
141

142
    callback$.subscribe({
6✔
143
      next: (callbackContext) => {
144
        this.refreshSessionWithIFrameCompletedInternal$.next(callbackContext);
4✔
145
        this.flowsDataService.resetSilentRenewRunning(config);
4✔
146
      },
147
      error: (err: any) => {
148
        this.loggerService.logError(config, 'Error: ' + err);
2✔
149
        this.refreshSessionWithIFrameCompletedInternal$.next(null);
2✔
150
        this.flowsDataService.resetSilentRenewRunning(config);
2✔
151
      },
152
    });
153
  }
154

155
  private getExistingIframe(): HTMLIFrameElement {
156
    return this.iFrameService.getExistingIFrame(
×
157
      IFRAME_FOR_SILENT_RENEW_IDENTIFIER
158
    );
159
  }
160
}
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