Skip to main content

openauth_plugins/api_key/
errors.rs

1use http::StatusCode;
2use openauth_core::api::{ApiErrorResponse, ApiResponse};
3use openauth_core::error::OpenAuthError;
4use openauth_core::plugin::PluginErrorCode;
5
6pub const INVALID_METADATA_TYPE: &str = "INVALID_METADATA_TYPE";
7pub const REFILL_AMOUNT_AND_INTERVAL_REQUIRED: &str = "REFILL_AMOUNT_AND_INTERVAL_REQUIRED";
8pub const REFILL_INTERVAL_AND_AMOUNT_REQUIRED: &str = "REFILL_INTERVAL_AND_AMOUNT_REQUIRED";
9pub const UNAUTHORIZED_SESSION: &str = "UNAUTHORIZED_SESSION";
10pub const KEY_NOT_FOUND: &str = "KEY_NOT_FOUND";
11pub const KEY_DISABLED: &str = "KEY_DISABLED";
12pub const KEY_EXPIRED: &str = "KEY_EXPIRED";
13pub const USAGE_EXCEEDED: &str = "USAGE_EXCEEDED";
14pub const EXPIRES_IN_IS_TOO_SMALL: &str = "EXPIRES_IN_IS_TOO_SMALL";
15pub const EXPIRES_IN_IS_TOO_LARGE: &str = "EXPIRES_IN_IS_TOO_LARGE";
16pub const INVALID_PREFIX_LENGTH: &str = "INVALID_PREFIX_LENGTH";
17pub const INVALID_NAME_LENGTH: &str = "INVALID_NAME_LENGTH";
18pub const METADATA_DISABLED: &str = "METADATA_DISABLED";
19pub const RATE_LIMIT_EXCEEDED: &str = "RATE_LIMIT_EXCEEDED";
20pub const NO_VALUES_TO_UPDATE: &str = "NO_VALUES_TO_UPDATE";
21pub const KEY_DISABLED_EXPIRATION: &str = "KEY_DISABLED_EXPIRATION";
22pub const INVALID_API_KEY: &str = "INVALID_API_KEY";
23pub const INVALID_REFERENCE_ID_FROM_API_KEY: &str = "INVALID_REFERENCE_ID_FROM_API_KEY";
24pub const SERVER_ONLY_PROPERTY: &str = "SERVER_ONLY_PROPERTY";
25pub const FAILED_TO_UPDATE_API_KEY: &str = "FAILED_TO_UPDATE_API_KEY";
26pub const NAME_REQUIRED: &str = "NAME_REQUIRED";
27pub const ORGANIZATION_ID_REQUIRED: &str = "ORGANIZATION_ID_REQUIRED";
28pub const USER_NOT_MEMBER_OF_ORGANIZATION: &str = "USER_NOT_MEMBER_OF_ORGANIZATION";
29pub const INSUFFICIENT_API_KEY_PERMISSIONS: &str = "INSUFFICIENT_API_KEY_PERMISSIONS";
30pub const NO_DEFAULT_API_KEY_CONFIGURATION_FOUND: &str = "NO_DEFAULT_API_KEY_CONFIGURATION_FOUND";
31pub const ORGANIZATION_PLUGIN_REQUIRED: &str = "ORGANIZATION_PLUGIN_REQUIRED";
32
33pub const ERROR_CODES: &[(&str, &str)] = &[
34    (INVALID_METADATA_TYPE, "metadata must be an object or undefined"),
35    (
36        REFILL_AMOUNT_AND_INTERVAL_REQUIRED,
37        "refillAmount is required when refillInterval is provided",
38    ),
39    (
40        REFILL_INTERVAL_AND_AMOUNT_REQUIRED,
41        "refillInterval is required when refillAmount is provided",
42    ),
43    (UNAUTHORIZED_SESSION, "Unauthorized or invalid session"),
44    (KEY_NOT_FOUND, "API Key not found"),
45    (KEY_DISABLED, "API Key is disabled"),
46    (KEY_EXPIRED, "API Key has expired"),
47    (USAGE_EXCEEDED, "API Key has reached its usage limit"),
48    (
49        EXPIRES_IN_IS_TOO_SMALL,
50        "The expiresIn is smaller than the predefined minimum value.",
51    ),
52    (
53        EXPIRES_IN_IS_TOO_LARGE,
54        "The expiresIn is larger than the predefined maximum value.",
55    ),
56    (
57        INVALID_PREFIX_LENGTH,
58        "The prefix length is either too large or too small.",
59    ),
60    (
61        INVALID_NAME_LENGTH,
62        "The name length is either too large or too small.",
63    ),
64    (METADATA_DISABLED, "Metadata is disabled."),
65    (RATE_LIMIT_EXCEEDED, "Rate limit exceeded."),
66    (NO_VALUES_TO_UPDATE, "No values to update."),
67    (
68        KEY_DISABLED_EXPIRATION,
69        "Custom key expiration values are disabled.",
70    ),
71    (INVALID_API_KEY, "Invalid API key."),
72    (
73        INVALID_REFERENCE_ID_FROM_API_KEY,
74        "The reference id from the API key is invalid.",
75    ),
76    (
77        SERVER_ONLY_PROPERTY,
78        "The property you're trying to set can only be set from the server auth instance only.",
79    ),
80    (FAILED_TO_UPDATE_API_KEY, "Failed to update API key"),
81    (NAME_REQUIRED, "API Key name is required."),
82    (
83        ORGANIZATION_ID_REQUIRED,
84        "Organization ID is required for organization-owned API keys.",
85    ),
86    (
87        USER_NOT_MEMBER_OF_ORGANIZATION,
88        "You are not a member of the organization that owns this API key.",
89    ),
90    (
91        INSUFFICIENT_API_KEY_PERMISSIONS,
92        "You do not have permission to perform this action on organization API keys.",
93    ),
94    (
95        NO_DEFAULT_API_KEY_CONFIGURATION_FOUND,
96        "No default api-key configuration found.",
97    ),
98    (
99        ORGANIZATION_PLUGIN_REQUIRED,
100        "Organization plugin is required for organization-owned API keys. Please install and configure the organization plugin.",
101    ),
102];
103
104pub fn message(code: &str) -> &'static str {
105    ERROR_CODES
106        .iter()
107        .find_map(|(candidate, message)| (*candidate == code).then_some(*message))
108        .unwrap_or("Unknown API key error")
109}
110
111pub fn plugin_error_codes() -> Vec<PluginErrorCode> {
112    ERROR_CODES
113        .iter()
114        .map(|(code, message)| PluginErrorCode::new(*code, *message))
115        .collect()
116}
117
118pub fn error_response(status: StatusCode, code: &str) -> Result<ApiResponse, OpenAuthError> {
119    error_response_with_message(status, code, message(code))
120}
121
122pub fn error_response_with_message(
123    status: StatusCode,
124    code: &str,
125    message: impl Into<String>,
126) -> Result<ApiResponse, OpenAuthError> {
127    let body = serde_json::to_vec(&ApiErrorResponse {
128        code: code.to_owned(),
129        message: message.into(),
130        original_message: None,
131    })
132    .map_err(|error| OpenAuthError::Api(error.to_string()))?;
133    http::Response::builder()
134        .status(status)
135        .header(http::header::CONTENT_TYPE, "application/json")
136        .body(body)
137        .map_err(|error| OpenAuthError::Api(error.to_string()))
138}