this repo has no description
1use crate::api::error::ApiError;
2use crate::api::EmptyResponse;
3use crate::auth::BearerAuthAdmin;
4use crate::state::AppState;
5use crate::types::{Did, PlainPassword};
6use axum::{
7 Json,
8 extract::State,
9 response::{IntoResponse, Response},
10};
11use serde::Deserialize;
12use tracing::{error, warn};
13
14#[derive(Deserialize)]
15pub struct UpdateAccountEmailInput {
16 pub account: String,
17 pub email: String,
18}
19
20pub async fn update_account_email(
21 State(state): State<AppState>,
22 _auth: BearerAuthAdmin,
23 Json(input): Json<UpdateAccountEmailInput>,
24) -> Response {
25 let account = input.account.trim();
26 let email = input.email.trim();
27 if account.is_empty() || email.is_empty() {
28 return ApiError::InvalidRequest("account and email are required".into()).into_response();
29 }
30 let result = sqlx::query!("UPDATE users SET email = $1 WHERE did = $2", email, account)
31 .execute(&state.db)
32 .await;
33 match result {
34 Ok(r) => {
35 if r.rows_affected() == 0 {
36 return ApiError::AccountNotFound.into_response();
37 }
38 EmptyResponse::ok().into_response()
39 }
40 Err(e) => {
41 error!("DB error updating email: {:?}", e);
42 ApiError::InternalError(None).into_response()
43 }
44 }
45}
46
47#[derive(Deserialize)]
48pub struct UpdateAccountHandleInput {
49 pub did: Did,
50 pub handle: String,
51}
52
53pub async fn update_account_handle(
54 State(state): State<AppState>,
55 _auth: BearerAuthAdmin,
56 Json(input): Json<UpdateAccountHandleInput>,
57) -> Response {
58 let did = &input.did;
59 let input_handle = input.handle.trim();
60 if input_handle.is_empty() {
61 return ApiError::InvalidRequest("handle is required".into()).into_response();
62 }
63 if !input_handle
64 .chars()
65 .all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_')
66 {
67 return ApiError::InvalidHandle(None).into_response();
68 }
69 let hostname = std::env::var("PDS_HOSTNAME").unwrap_or_else(|_| "localhost".to_string());
70 let handle = if !input_handle.contains('.') {
71 format!("{}.{}", input_handle, hostname)
72 } else {
73 input_handle.to_string()
74 };
75 let old_handle = sqlx::query_scalar!("SELECT handle FROM users WHERE did = $1", did.as_str())
76 .fetch_optional(&state.db)
77 .await
78 .ok()
79 .flatten();
80 let existing = sqlx::query!(
81 "SELECT id FROM users WHERE handle = $1 AND did != $2",
82 handle,
83 did.as_str()
84 )
85 .fetch_optional(&state.db)
86 .await;
87 if let Ok(Some(_)) = existing {
88 return ApiError::HandleTaken.into_response();
89 }
90 let result = sqlx::query!("UPDATE users SET handle = $1 WHERE did = $2", handle, did.as_str())
91 .execute(&state.db)
92 .await;
93 match result {
94 Ok(r) => {
95 if r.rows_affected() == 0 {
96 return ApiError::AccountNotFound.into_response();
97 }
98 if let Some(old) = old_handle {
99 let _ = state.cache.delete(&format!("handle:{}", old)).await;
100 }
101 let _ = state.cache.delete(&format!("handle:{}", handle)).await;
102 if let Err(e) =
103 crate::api::repo::record::sequence_identity_event(&state, did.as_str(), Some(&handle)).await
104 {
105 warn!(
106 "Failed to sequence identity event for admin handle update: {}",
107 e
108 );
109 }
110 if let Err(e) = crate::api::identity::did::update_plc_handle(&state, did.as_str(), &handle).await
111 {
112 warn!("Failed to update PLC handle for admin handle update: {}", e);
113 }
114 EmptyResponse::ok().into_response()
115 }
116 Err(e) => {
117 error!("DB error updating handle: {:?}", e);
118 ApiError::InternalError(None).into_response()
119 }
120 }
121}
122
123#[derive(Deserialize)]
124pub struct UpdateAccountPasswordInput {
125 pub did: Did,
126 pub password: PlainPassword,
127}
128
129pub async fn update_account_password(
130 State(state): State<AppState>,
131 _auth: BearerAuthAdmin,
132 Json(input): Json<UpdateAccountPasswordInput>,
133) -> Response {
134 let did = &input.did;
135 let password = input.password.trim();
136 if password.is_empty() {
137 return ApiError::InvalidRequest("password is required".into()).into_response();
138 }
139 let password_hash = match bcrypt::hash(password, bcrypt::DEFAULT_COST) {
140 Ok(h) => h,
141 Err(e) => {
142 error!("Failed to hash password: {:?}", e);
143 return ApiError::InternalError(None).into_response();
144 }
145 };
146 let result = sqlx::query!(
147 "UPDATE users SET password_hash = $1 WHERE did = $2",
148 password_hash,
149 did.as_str()
150 )
151 .execute(&state.db)
152 .await;
153 match result {
154 Ok(r) => {
155 if r.rows_affected() == 0 {
156 return ApiError::AccountNotFound.into_response();
157 }
158 EmptyResponse::ok().into_response()
159 }
160 Err(e) => {
161 error!("DB error updating password: {:?}", e);
162 ApiError::InternalError(None).into_response()
163 }
164 }
165}