open ai api

This commit is contained in:
2025-09-16 09:38:01 +08:00
parent c71c3ab417
commit 5f9a2bc78c
7168 changed files with 352600 additions and 209 deletions

View File

@@ -0,0 +1,65 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { AuthError } from "@azure/msal-common/node";
import * as ManagedIdentityErrorCodes from "./ManagedIdentityErrorCodes.js";
import { ManagedIdentityEnvironmentVariableNames } from "../utils/Constants.js";
export { ManagedIdentityErrorCodes };
/**
* ManagedIdentityErrorMessage class containing string constants used by error codes and messages.
*/
export const ManagedIdentityErrorMessages = {
[ManagedIdentityErrorCodes.invalidFileExtension]:
"The file path in the WWW-Authenticate header does not contain a .key file.",
[ManagedIdentityErrorCodes.invalidFilePath]:
"The file path in the WWW-Authenticate header is not in a valid Windows or Linux Format.",
[ManagedIdentityErrorCodes.invalidManagedIdentityIdType]:
"More than one ManagedIdentityIdType was provided.",
[ManagedIdentityErrorCodes.invalidSecret]:
"The secret in the file on the file path in the WWW-Authenticate header is greater than 4096 bytes.",
[ManagedIdentityErrorCodes.platformNotSupported]:
"The platform is not supported by Azure Arc. Azure Arc only supports Windows and Linux.",
[ManagedIdentityErrorCodes.missingId]:
"A ManagedIdentityId id was not provided.",
[ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
.AZURE_POD_IDENTITY_AUTHORITY_HOST]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST}' environment variable is malformed.`,
[ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
.IDENTITY_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT}' environment variable is malformed.`,
[ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
.IMDS_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT}' environment variable is malformed.`,
[ManagedIdentityErrorCodes.MsiEnvironmentVariableUrlMalformedErrorCodes
.MSI_ENDPOINT]: `The Managed Identity's '${ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT}' environment variable is malformed.`,
[ManagedIdentityErrorCodes.networkUnavailable]:
"Authentication unavailable. The request to the managed identity endpoint timed out.",
[ManagedIdentityErrorCodes.unableToCreateAzureArc]:
"Azure Arc Managed Identities can only be system assigned.",
[ManagedIdentityErrorCodes.unableToCreateCloudShell]:
"Cloud Shell Managed Identities can only be system assigned.",
[ManagedIdentityErrorCodes.unableToCreateSource]:
"Unable to create a Managed Identity source based on environment variables.",
[ManagedIdentityErrorCodes.unableToReadSecretFile]:
"Unable to read the secret file.",
[ManagedIdentityErrorCodes.userAssignedNotAvailableAtRuntime]:
"Service Fabric user assigned managed identity ClientId or ResourceId is not configurable at runtime.",
[ManagedIdentityErrorCodes.wwwAuthenticateHeaderMissing]:
"A 401 response was received form the Azure Arc Managed Identity, but the www-authenticate header is missing.",
[ManagedIdentityErrorCodes.wwwAuthenticateHeaderUnsupportedFormat]:
"A 401 response was received form the Azure Arc Managed Identity, but the www-authenticate header is in an unsupported format.",
};
export class ManagedIdentityError extends AuthError {
constructor(errorCode: string) {
super(errorCode, ManagedIdentityErrorMessages[errorCode]);
this.name = "ManagedIdentityError";
Object.setPrototypeOf(this, ManagedIdentityError.prototype);
}
}
export function createManagedIdentityError(
errorCode: string
): ManagedIdentityError {
return new ManagedIdentityError(errorCode);
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { ManagedIdentityEnvironmentVariableNames } from "../utils/Constants.js";
export const invalidFileExtension = "invalid_file_extension";
export const invalidFilePath = "invalid_file_path";
export const invalidManagedIdentityIdType = "invalid_managed_identity_id_type";
export const invalidSecret = "invalid_secret";
export const missingId = "missing_client_id";
export const networkUnavailable = "network_unavailable";
export const platformNotSupported = "platform_not_supported";
export const unableToCreateAzureArc = "unable_to_create_azure_arc";
export const unableToCreateCloudShell = "unable_to_create_cloud_shell";
export const unableToCreateSource = "unable_to_create_source";
export const unableToReadSecretFile = "unable_to_read_secret_file";
export const urlParseError = "url_parse_error";
export const userAssignedNotAvailableAtRuntime =
"user_assigned_not_available_at_runtime";
export const wwwAuthenticateHeaderMissing = "www_authenticate_header_missing";
export const wwwAuthenticateHeaderUnsupportedFormat =
"www_authenticate_header_unsupported_format";
export const MsiEnvironmentVariableUrlMalformedErrorCodes = {
[ManagedIdentityEnvironmentVariableNames.AZURE_POD_IDENTITY_AUTHORITY_HOST]:
"azure_pod_identity_authority_host_url_malformed",
[ManagedIdentityEnvironmentVariableNames.IDENTITY_ENDPOINT]:
"identity_endpoint_url_malformed",
[ManagedIdentityEnvironmentVariableNames.IMDS_ENDPOINT]:
"imds_endpoint_url_malformed",
[ManagedIdentityEnvironmentVariableNames.MSI_ENDPOINT]:
"msi_endpoint_url_malformed",
} as const;
export type MsiEnvironmentVariableErrorCodes =
(typeof MsiEnvironmentVariableUrlMalformedErrorCodes)[keyof typeof MsiEnvironmentVariableUrlMalformedErrorCodes];

View File

@@ -0,0 +1,145 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { AuthError } from "@azure/msal-common/node";
/**
* NodeAuthErrorMessage class containing string constants used by error codes and messages.
*/
export const NodeAuthErrorMessage = {
invalidLoopbackAddressType: {
code: "invalid_loopback_server_address_type",
desc: "Loopback server address is not type string. This is unexpected.",
},
unableToLoadRedirectUri: {
code: "unable_to_load_redirectUrl",
desc: "Loopback server callback was invoked without a url. This is unexpected.",
},
noAuthCodeInResponse: {
code: "no_auth_code_in_response",
desc: "No auth code found in the server response. Please check your network trace to determine what happened.",
},
noLoopbackServerExists: {
code: "no_loopback_server_exists",
desc: "No loopback server exists yet.",
},
loopbackServerAlreadyExists: {
code: "loopback_server_already_exists",
desc: "Loopback server already exists. Cannot create another.",
},
loopbackServerTimeout: {
code: "loopback_server_timeout",
desc: "Timed out waiting for auth code listener to be registered.",
},
stateNotFoundError: {
code: "state_not_found",
desc: "State not found. Please verify that the request originated from msal.",
},
thumbprintMissing: {
code: "thumbprint_missing_from_client_certificate",
desc: "Client certificate does not contain a SHA-1 or SHA-256 thumbprint.",
},
redirectUriNotSupported: {
code: "redirect_uri_not_supported",
desc: "RedirectUri is not supported in this scenario. Please remove redirectUri from the request.",
},
};
export class NodeAuthError extends AuthError {
constructor(errorCode: string, errorMessage?: string) {
super(errorCode, errorMessage);
this.name = "NodeAuthError";
}
/**
* Creates an error thrown if loopback server address is of type string.
*/
static createInvalidLoopbackAddressTypeError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.invalidLoopbackAddressType.code,
`${NodeAuthErrorMessage.invalidLoopbackAddressType.desc}`
);
}
/**
* Creates an error thrown if the loopback server is unable to get a url.
*/
static createUnableToLoadRedirectUrlError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.unableToLoadRedirectUri.code,
`${NodeAuthErrorMessage.unableToLoadRedirectUri.desc}`
);
}
/**
* Creates an error thrown if the server response does not contain an auth code.
*/
static createNoAuthCodeInResponseError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.noAuthCodeInResponse.code,
`${NodeAuthErrorMessage.noAuthCodeInResponse.desc}`
);
}
/**
* Creates an error thrown if the loopback server has not been spun up yet.
*/
static createNoLoopbackServerExistsError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.noLoopbackServerExists.code,
`${NodeAuthErrorMessage.noLoopbackServerExists.desc}`
);
}
/**
* Creates an error thrown if a loopback server already exists when attempting to create another one.
*/
static createLoopbackServerAlreadyExistsError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.loopbackServerAlreadyExists.code,
`${NodeAuthErrorMessage.loopbackServerAlreadyExists.desc}`
);
}
/**
* Creates an error thrown if the loopback server times out registering the auth code listener.
*/
static createLoopbackServerTimeoutError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.loopbackServerTimeout.code,
`${NodeAuthErrorMessage.loopbackServerTimeout.desc}`
);
}
/**
* Creates an error thrown when the state is not present.
*/
static createStateNotFoundError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.stateNotFoundError.code,
NodeAuthErrorMessage.stateNotFoundError.desc
);
}
/**
* Creates an error thrown when client certificate was provided, but neither the SHA-1 or SHA-256 thumbprints were provided
*/
static createThumbprintMissingError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.thumbprintMissing.code,
NodeAuthErrorMessage.thumbprintMissing.desc
);
}
/**
* Creates an error thrown when redirectUri is provided in an unsupported scenario
*/
static createRedirectUriNotSupportedError(): NodeAuthError {
return new NodeAuthError(
NodeAuthErrorMessage.redirectUriNotSupported.code,
NodeAuthErrorMessage.redirectUriNotSupported.desc
);
}
}