-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathNativeAuthError.ts
More file actions
110 lines (98 loc) · 3.07 KB
/
Copy pathNativeAuthError.ts
File metadata and controls
110 lines (98 loc) · 3.07 KB
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
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import {
AuthError,
InteractionRequiredAuthError,
InteractionRequiredAuthErrorCodes,
createInteractionRequiredAuthError,
} from "@azure/msal-common/browser";
import {
createBrowserAuthError,
BrowserAuthErrorCodes,
getDefaultErrorMessage,
} from "./BrowserAuthError.js";
import * as NativeAuthErrorCodes from "./NativeAuthErrorCodes.js";
import * as NativeStatusCodes from "../broker/nativeBroker/NativeStatusCodes.js";
export { NativeAuthErrorCodes };
export type OSError = {
error?: number;
protocol_error?: string;
properties?: object;
status?: string;
retryable?: boolean;
};
const INVALID_METHOD_ERROR = -2147186943;
export class NativeAuthError extends AuthError {
ext: OSError | undefined;
constructor(errorCode: string, description?: string, ext?: OSError) {
super(errorCode, description || getDefaultErrorMessage(errorCode));
Object.setPrototypeOf(this, NativeAuthError.prototype);
this.name = "NativeAuthError";
this.ext = ext;
}
}
/**
* These errors should result in a fallback to the 'standard' browser based auth flow.
*/
export function isFatalNativeAuthError(error: NativeAuthError): boolean {
if (
error.ext &&
error.ext.status &&
error.ext.status === NativeStatusCodes.DISABLED
) {
return true;
}
if (
error.ext &&
error.ext.error &&
error.ext.error === INVALID_METHOD_ERROR
) {
return true;
}
switch (error.errorCode) {
case NativeAuthErrorCodes.contentError:
case NativeAuthErrorCodes.pageException:
return true;
default:
return false;
}
}
/**
* Create the appropriate error object based on the WAM status code.
* @param code
* @param description
* @param ext
* @returns
*/
export function createNativeAuthError(
code: string,
description?: string,
ext?: OSError
): AuthError {
if (ext && ext.status) {
switch (ext.status) {
case NativeStatusCodes.ACCOUNT_UNAVAILABLE:
return createInteractionRequiredAuthError(
InteractionRequiredAuthErrorCodes.nativeAccountUnavailable,
getDefaultErrorMessage(code)
);
case NativeStatusCodes.USER_INTERACTION_REQUIRED:
return new InteractionRequiredAuthError(code, description);
case NativeStatusCodes.USER_CANCEL:
return createBrowserAuthError(
BrowserAuthErrorCodes.userCancelled
);
case NativeStatusCodes.NO_NETWORK:
return createBrowserAuthError(
BrowserAuthErrorCodes.noNetworkConnectivity
);
case NativeStatusCodes.UI_NOT_ALLOWED:
return createInteractionRequiredAuthError(
InteractionRequiredAuthErrorCodes.uiNotAllowed
);
}
}
return new NativeAuthError(code, description, ext);
}