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
/*
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
 
import {
    SignInResendCodeParams,
    SignInSubmitCodeParams,
} from "../../interaction_client/parameter/SignInParams.js";
import { SignInResendCodeResult } from "../result/SignInResendCodeResult.js";
import { SignInSubmitCodeResult } from "../result/SignInSubmitCodeResult.js";
import { SignInCodeRequiredStateParameters } from "./SignInStateParameters.js";
import { SignInState } from "./SignInState.js";
import { SIGN_IN_CODE_REQUIRED_STATE_TYPE } from "../../../core/auth_flow/AuthFlowStateTypes.js";
 
/*
 * Sign-in code required state.
 */
export class SignInCodeRequiredState extends SignInState<SignInCodeRequiredStateParameters> {
    /**
     * The type of the state.
     */
    stateType = SIGN_IN_CODE_REQUIRED_STATE_TYPE;
 
    /**
     * Once user configures email one-time passcode as a authentication method in Microsoft Entra, a one-time passcode will be sent to the user’s email.
     * Submit this one-time passcode to continue sign-in flow.
     * @param {string} code - The code to submit.
     * @returns {Promise<SignInSubmitCodeResult>} The result of the operation.
     */
    async submitCode(code: string): Promise<SignInSubmitCodeResult> {
        try {
            this.ensureCodeIsValid(code, this.stateParameters.codeLength);
 
            const submitCodeParams: SignInSubmitCodeParams = {
                clientId: this.stateParameters.config.auth.clientId,
                correlationId: this.stateParameters.correlationId,
                challengeType:
                    this.stateParameters.config.customAuth.challengeTypes ?? [],
                scopes: this.stateParameters.scopes ?? [],
                continuationToken: this.stateParameters.continuationToken ?? "",
                code: code,
                username: this.stateParameters.username,
                claims: this.stateParameters.claims,
            };
 
            this.stateParameters.logger.verbose(
                "Submitting code for sign-in.",
                this.stateParameters.correlationId
            );
 
            const submitCodeResult =
                await this.stateParameters.signInClient.submitCode(
                    submitCodeParams
                );
 
            this.stateParameters.logger.verbose(
                "Code submitted for sign-in.",
                this.stateParameters.correlationId
            );
 
            const nextState = this.handleSignInResult(
                submitCodeResult,
                this.stateParameters.scopes
            );
 
            if (nextState.error) {
                return SignInSubmitCodeResult.createWithError(nextState.error);
            }
 
            return new SignInSubmitCodeResult(
                nextState.state,
                nextState.accountInfo
            );
        } catch (error) {
            this.stateParameters.logger.errorPii(
                `Failed to submit code for sign-in. Error: '${error}'.`,
                this.stateParameters.correlationId
            );
 
            return SignInSubmitCodeResult.createWithError(error);
        }
    }
 
    /**
     * Resends the another one-time passcode for sign-in flow if the previous one hasn't been verified.
     * @returns {Promise<SignInResendCodeResult>} The result of the operation.
     */
    async resendCode(): Promise<SignInResendCodeResult> {
        try {
            const submitCodeParams: SignInResendCodeParams = {
                clientId: this.stateParameters.config.auth.clientId,
                correlationId: this.stateParameters.correlationId,
                challengeType:
                    this.stateParameters.config.customAuth.challengeTypes ?? [],
                continuationToken: this.stateParameters.continuationToken ?? "",
                username: this.stateParameters.username,
            };
 
            this.stateParameters.logger.verbose(
                "Resending code for sign-in.",
                this.stateParameters.correlationId
            );
 
            const result = await this.stateParameters.signInClient.resendCode(
                submitCodeParams
            );
 
            this.stateParameters.logger.verbose(
                "Code resent for sign-in.",
                this.stateParameters.correlationId
            );
 
            return new SignInResendCodeResult(
                new SignInCodeRequiredState({
                    correlationId: result.correlationId,
                    continuationToken: result.continuationToken,
                    logger: this.stateParameters.logger,
                    config: this.stateParameters.config,
                    signInClient: this.stateParameters.signInClient,
                    cacheClient: this.stateParameters.cacheClient,
                    jitClient: this.stateParameters.jitClient,
                    mfaClient: this.stateParameters.mfaClient,
                    username: this.stateParameters.username,
                    codeLength: result.codeLength,
                    scopes: this.stateParameters.scopes,
                })
            );
        } catch (error) {
            return SignInResendCodeResult.createWithError(error);
        }
    }
 
    /**
     * Gets the sent code length.
     * @returns {number} The length of the code.
     */
    getCodeLength(): number {
        return this.stateParameters.codeLength;
    }
 
    /**
     * Gets the scopes to request.
     * @returns {string[] | undefined} The scopes to request.
     */
    getScopes(): string[] | undefined {
        return this.stateParameters.scopes;
    }
}