wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
 
import {
    ClientConfiguration,
    buildClientConfiguration,
    CommonClientConfiguration,
    INetworkModule,
    NetworkRequestOptions,
    Logger,
    ICrypto,
    CacheManager,
    ServerTelemetryManager,
    Authority,
    CcsCredential,
    TokenProtocol,
    RequestThumbprint,
    ServerAuthorizationTokenResponse,
    NetworkResponse,
    BaseAuthRequest,
    StubPerformanceClient,
} from "@azure/msal-common/node";
import { version, name } from "../packageMetadata.js";
 
/**
 * Base application class which will construct requests to send to and handle responses from the Microsoft STS using the authorization code flow.
 * @internal
 */
export abstract class BaseClient {
    // Logger object
    public logger: Logger;
 
    // Application config
    protected config: CommonClientConfiguration;
 
    // Crypto Interface
    protected cryptoUtils: ICrypto;
 
    // Storage Interface
    protected cacheManager: CacheManager;
 
    // Network Interface
    protected networkClient: INetworkModule;
 
    // Server Telemetry Manager
    protected serverTelemetryManager: ServerTelemetryManager | null;
 
    // Default authority object
    public authority: Authority;
 
    protected performanceClient: StubPerformanceClient;
 
    protected constructor(configuration: ClientConfiguration) {
        // Set the configuration
        this.config = buildClientConfiguration(configuration);
 
        // Initialize the logger
        this.logger = new Logger(this.config.loggerOptions, name, version);
 
        // Initialize crypto
        this.cryptoUtils = this.config.cryptoInterface;
 
        // Initialize storage interface
        this.cacheManager = this.config.storageInterface;
 
        // Set the network interface
        this.networkClient = this.config.networkInterface;
 
        // Set TelemetryManager
        this.serverTelemetryManager = this.config.serverTelemetryManager;
 
        // set Authority
        this.authority = this.config.authOptions.authority;
 
        this.performanceClient = new StubPerformanceClient();
    }
 
    /**
     * Creates default headers for requests to token endpoint
     */
    protected createTokenRequestHeaders(
        ccsCred?: CcsCredential
    ): Record<string, string> {
        return TokenProtocol.createTokenRequestHeaders(
            this.logger,
            false,
            ccsCred
        );
    }
 
    /**
     * Http post to token endpoint
     * @param tokenEndpoint
     * @param queryString
     * @param headers
     * @param thumbprint
     */
    protected async executePostToTokenEndpoint(
        tokenEndpoint: string,
        queryString: string,
        headers: Record<string, string>,
        thumbprint: RequestThumbprint,
        correlationId: string
    ): Promise<NetworkResponse<ServerAuthorizationTokenResponse>> {
        return TokenProtocol.executePostToTokenEndpoint(
            tokenEndpoint,
            queryString,
            headers,
            thumbprint,
            correlationId,
            this.cacheManager,
            this.networkClient,
            this.logger,
            this.performanceClient,
            this.serverTelemetryManager
        );
    }
 
    /**
     * Wraps sendPostRequestAsync with necessary preflight and postflight logic
     * @param thumbprint - Request thumbprint for throttling
     * @param tokenEndpoint - Endpoint to make the POST to
     * @param options - Body and Headers to include on the POST request
     * @param correlationId - CorrelationId for telemetry
     */
    async sendPostRequest<T extends ServerAuthorizationTokenResponse>(
        thumbprint: RequestThumbprint,
        tokenEndpoint: string,
        options: NetworkRequestOptions,
        correlationId: string
    ): Promise<NetworkResponse<T>> {
        return TokenProtocol.sendPostRequest<T>(
            thumbprint,
            tokenEndpoint,
            options,
            correlationId,
            this.cacheManager,
            this.networkClient,
            this.logger,
            this.performanceClient
        );
    }
 
    /**
     * Creates query string for the /token request
     * @param request
     */
    createTokenQueryParameters(request: BaseAuthRequest): string {
        return TokenProtocol.createTokenQueryParameters(
            request,
            this.config.authOptions.clientId,
            this.config.authOptions.redirectUri,
            this.performanceClient
        );
    }
}