<commit_before>﻿import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { of, Subject } from 'rxjs';
import { map, switchMap, tap } from 'rxjs/operators';
import { ConfigurationProvider } from '../config/config.provider';
import { OpenIdConfiguration } from '../models/auth.configuration';
import { LoggerService } from '../services/oidc.logger.service';

@Injectable()
export class OidcConfigService {
    private STS_SERVER_SUFFIX = `/.well-known/openid-configuration`;
    private configurationLoadedInternal = new Subject();

    // TODO DO WE NEED THIS?
    public get onConfigurationLoaded() {
        return this.configurationLoadedInternal.asObservable();
    }

    constructor(
        private readonly loggerService: LoggerService,
        private readonly httpClient: HttpClient,
        private readonly configurationProvider: ConfigurationProvider
    ) {}

    withConfig(passedConfig: OpenIdConfiguration, mappingFunction?: any) {
        if (!passedConfig.customConfigServer && !passedConfig.stsServer) {
            this.loggerService.logError('please provide at least an stsServer or a custom config');
            return;
        }

        if (passedConfig.customConfigServer && !mappingFunction) {
            this.loggerService.logError(
                'If you have given a custom config server then please provide a mapping method as second param, too '
            );
            return;
        }

        const loadConfig$ = this.getCustomConfig(passedConfig, mappingFunction).pipe(
            switchMap((libConfig) =>
                this.getWellKnownDocument(passedConfig.stsServer || libConfig.stsServer).pipe(
                    map((wellKnownEndpoints) => {
                        return {
                            libConfig,
                            wellKnownEndpoints,
                        };
                    })
                )
            ),
            tap(console.log),
            map(({ libConfig, wellKnownEndpoints }) => {
                return {
                    customConfig: libConfig,
                    wellKnownEndpoints: {
                        issuer: wellKnownEndpoints.issuer,
                        jwksUri: wellKnownEndpoints.jwks_uri,
                        authorizationEndpoint: wellKnownEndpoints.authorization_endpoint,
                        tokenEndpoint: wellKnownEndpoints.token_endpoint,
                        userinfoEndpoint: wellKnownEndpoints.userinfo_endpoint,
                        endSessionEndpoint: wellKnownEndpoints.end_session_endpoint,
                        checkSessionIframe: wellKnownEndpoints.check_session_iframe,
                        revocationEndpoint: wellKnownEndpoints.revocation_endpoint,
                        introspectionEndpoint: wellKnownEndpoints.introspection_endpoint,
                    },
                };
            }),
            tap(({ customConfig, wellKnownEndpoints }) => this.configurationProvider.setConfig(customConfig, wellKnownEndpoints))
        );<commit_msg>removing console.log<commit_after>﻿import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { of, Subject } from 'rxjs';
import { map, switchMap, tap } from 'rxjs/operators';
import { ConfigurationProvider } from '../config/config.provider';
import { OpenIdConfiguration } from '../models/auth.configuration';
import { LoggerService } from '../services/oidc.logger.service';

@Injectable()
export class OidcConfigService {
    private STS_SERVER_SUFFIX = `/.well-known/openid-configuration`;
    private configurationLoadedInternal = new Subject();

    // TODO DO WE NEED THIS?
    public get onConfigurationLoaded() {
        return this.configurationLoadedInternal.asObservable();
    }

    constructor(
        private readonly loggerService: LoggerService,
        private readonly httpClient: HttpClient,
        private readonly configurationProvider: ConfigurationProvider
    ) {}

    withConfig(passedConfig: OpenIdConfiguration, mappingFunction?: any) {
        if (!passedConfig.customConfigServer && !passedConfig.stsServer) {
            this.loggerService.logError('please provide at least an stsServer or a custom config');
            return;
        }

        if (passedConfig.customConfigServer && !mappingFunction) {
            this.loggerService.logError(
                'If you have given a custom config server then please provide a mapping method as second param, too '
            );
            return;
        }

        const loadConfig$ = this.getCustomConfig(passedConfig, mappingFunction).pipe(
            switchMap((libConfig) =>
                this.getWellKnownDocument(passedConfig.stsServer || libConfig.stsServer).pipe(
                    map((wellKnownEndpoints) => {
                        return {
                            libConfig,
                            wellKnownEndpoints,
                        };
                    })
                )
            ),
            map(({ libConfig, wellKnownEndpoints }) => {
                return {
                    customConfig: libConfig,
                    wellKnownEndpoints: {
                        issuer: wellKnownEndpoints.issuer,
                        jwksUri: wellKnownEndpoints.jwks_uri,
                        authorizationEndpoint: wellKnownEndpoints.authorization_endpoint,
                        tokenEndpoint: wellKnownEndpoints.token_endpoint,
                        userinfoEndpoint: wellKnownEndpoints.userinfo_endpoint,
                        endSessionEndpoint: wellKnownEndpoints.end_session_endpoint,
                        checkSessionIframe: wellKnownEndpoints.check_session_iframe,
                        revocationEndpoint: wellKnownEndpoints.revocation_endpoint,
                        introspectionEndpoint: wellKnownEndpoints.introspection_endpoint,
                    },
                };
            }),
            tap(({ customConfig, wellKnownEndpoints }) => this.configurationProvider.setConfig(customConfig, wellKnownEndpoints))
        );
{'fraction_non_alphanumeric': 0.08151549942594719, 'fraction_numerical': 0.0, 'mean_word_length': 2.056641604010025, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1088489', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1362, 'n_words': 296}