1use http::StatusCode;
2use rustauth_core::api::{ApiErrorResponse, ApiResponse};
3use rustauth_core::error::RustAuthError;
4use rustauth_core::plugin::PluginErrorCode;
5
6pub const ADMIN_ERROR_CODES: &[&str] = &[
7 "FAILED_TO_CREATE_USER",
8 "USER_ALREADY_EXISTS",
9 "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL",
10 "YOU_CANNOT_BAN_YOURSELF",
11 "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE",
12 "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS",
13 "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS",
14 "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS",
15 "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS",
16 "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS",
17 "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS",
18 "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS",
19 "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD",
20 "BANNED_USER",
21 "YOU_ARE_NOT_ALLOWED_TO_GET_USER",
22 "NO_DATA_TO_UPDATE",
23 "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS",
24 "YOU_CANNOT_REMOVE_YOURSELF",
25 "YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE",
26 "YOU_CANNOT_IMPERSONATE_ADMINS",
27 "INVALID_ROLE_TYPE",
28];
29
30macro_rules! code {
31 ($fn_name:ident, $code:literal, $message:literal) => {
32 pub fn $fn_name() -> PluginErrorCode {
33 PluginErrorCode::new($code, $message)
34 }
35 };
36}
37
38code!(
39 failed_to_create_user,
40 "FAILED_TO_CREATE_USER",
41 "Failed to create user"
42);
43code!(
44 user_already_exists,
45 "USER_ALREADY_EXISTS",
46 "User already exists."
47);
48code!(
49 cannot_ban_yourself,
50 "YOU_CANNOT_BAN_YOURSELF",
51 "You cannot ban yourself"
52);
53code!(
54 not_allowed_to_change_role,
55 "YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE",
56 "You are not allowed to change users role"
57);
58code!(
59 not_allowed_to_create_users,
60 "YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS",
61 "You are not allowed to create users"
62);
63code!(
64 not_allowed_to_list_users,
65 "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS",
66 "You are not allowed to list users"
67);
68code!(
69 not_allowed_to_list_sessions,
70 "YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS",
71 "You are not allowed to list users sessions"
72);
73code!(
74 not_allowed_to_ban_users,
75 "YOU_ARE_NOT_ALLOWED_TO_BAN_USERS",
76 "You are not allowed to ban users"
77);
78code!(
79 not_allowed_to_impersonate_users,
80 "YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS",
81 "You are not allowed to impersonate users"
82);
83code!(
84 not_allowed_to_revoke_sessions,
85 "YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS",
86 "You are not allowed to revoke users sessions"
87);
88code!(
89 not_allowed_to_delete_users,
90 "YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS",
91 "You are not allowed to delete users"
92);
93code!(
94 not_allowed_to_set_password,
95 "YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD",
96 "You are not allowed to set users password"
97);
98code!(
99 not_allowed_to_get_user,
100 "YOU_ARE_NOT_ALLOWED_TO_GET_USER",
101 "You are not allowed to get user"
102);
103code!(no_data_to_update, "NO_DATA_TO_UPDATE", "No data to update");
104code!(
105 not_allowed_to_update_users,
106 "YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS",
107 "You are not allowed to update users"
108);
109code!(
110 cannot_remove_yourself,
111 "YOU_CANNOT_REMOVE_YOURSELF",
112 "You cannot remove yourself"
113);
114code!(
115 not_allowed_to_set_unknown_role,
116 "YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE",
117 "You are not allowed to set a non-existent role value"
118);
119code!(
120 cannot_impersonate_admins,
121 "YOU_CANNOT_IMPERSONATE_ADMINS",
122 "You cannot impersonate admins"
123);
124code!(invalid_role_type, "INVALID_ROLE_TYPE", "Invalid role type");
125
126pub fn user_already_exists_use_another_email() -> PluginErrorCode {
127 PluginErrorCode::new(
128 "USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL",
129 "User already exists. Use another email.",
130 )
131}
132
133pub fn banned_user(message: &str) -> PluginErrorCode {
134 PluginErrorCode::new("BANNED_USER", message)
135}
136
137pub fn error_response(
138 status: StatusCode,
139 code: impl Into<String>,
140 message: impl Into<String>,
141) -> Result<ApiResponse, RustAuthError> {
142 let body = serde_json::to_vec(&ApiErrorResponse {
143 code: code.into(),
144 message: message.into(),
145 original_message: None,
146 })
147 .map_err(|error| RustAuthError::Api(error.to_string()))?;
148 http::Response::builder()
149 .status(status)
150 .header(http::header::CONTENT_TYPE, "application/json")
151 .body(body)
152 .map_err(|error| RustAuthError::Api(error.to_string()))
153}
154
155pub fn forbidden(error: PluginErrorCode) -> Result<ApiResponse, RustAuthError> {
156 error_response(StatusCode::FORBIDDEN, error.code, error.message)
157}
158
159pub fn bad_request(error: PluginErrorCode) -> Result<ApiResponse, RustAuthError> {
160 error_response(StatusCode::BAD_REQUEST, error.code, error.message)
161}
162
163pub fn unauthorized() -> Result<ApiResponse, RustAuthError> {
164 error_response(StatusCode::UNAUTHORIZED, "UNAUTHORIZED", "Unauthorized")
165}
166
167pub fn not_found_user() -> Result<ApiResponse, RustAuthError> {
168 error_response(StatusCode::NOT_FOUND, "USER_NOT_FOUND", "User not found")
169}