-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve_handle.rs
More file actions
121 lines (109 loc) · 3.52 KB
/
Copy pathresolve_handle.rs
File metadata and controls
121 lines (109 loc) · 3.52 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
111
112
113
114
115
116
117
118
119
120
121
//! com.atproto.identity.resolveHandle endpoint.
//!
//! Resolves a handle (e.g., "alice.bsky.social") to a DID.
use std::net::SocketAddr;
use std::sync::Arc;
use axum::{
Json,
extract::{ConnectInfo, Query, State},
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
};
use serde::{Deserialize, Serialize};
use crate::pds::db::StatisticKey;
use crate::pds::server::PdsState;
use super::{get_caller_info};
use crate::ws::{ActorQueryOptions, BlueskyClient, DEFAULT_APP_VIEW_HOST_NAME};
/// Query parameters for resolveHandle.
#[derive(Deserialize)]
pub struct ResolveHandleParams {
/// The handle to resolve.
handle: Option<String>,
}
/// Successful response for resolveHandle.
#[derive(Serialize)]
pub struct ResolveHandleResponse {
/// The resolved DID.
did: String,
}
/// Error response for resolveHandle.
#[derive(Serialize)]
pub struct ResolveHandleError {
error: String,
message: String,
}
/// GET /xrpc/com.atproto.identity.resolveHandle - Handle resolution endpoint.
///
/// Resolves an AT Protocol handle to its DID.
///
/// # Parameters
///
/// * `handle` - The handle to resolve (e.g., "alice.bsky.social")
///
/// # Returns
///
/// * `200 OK` with `{ "did": "did:plc:..." }` on success
/// * `400 Bad Request` if handle parameter is missing
/// * `404 Not Found` if handle cannot be resolved
pub async fn resolve_handle(
State(state): State<Arc<PdsState>>,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
headers: HeaderMap,
Query(params): Query<ResolveHandleParams>,
) -> Response {
// Get caller info for statistics
let (ip_address, user_agent) = get_caller_info(&headers, Some(addr));
// Increment statistics
let stat_key = StatisticKey {
name: "xrpc/com.atproto.identity.resolveHandle".to_string(),
ip_address,
user_agent,
};
let _ = state.db.increment_statistic_for_endpoint(&stat_key);
// Validate handle parameter
let handle = match params.handle {
Some(h) if !h.is_empty() => h,
_ => {
return (
StatusCode::BAD_REQUEST,
Json(ResolveHandleError {
error: "InvalidRequest".to_string(),
message: "Error: Params must have the property \"handle\"".to_string(),
}),
)
.into_response();
}
};
// Resolve the handle to a DID
let app_view_host_name = state.db.get_config_property("AppViewHostName")
.unwrap_or_else(|_| DEFAULT_APP_VIEW_HOST_NAME.to_string());
let client = BlueskyClient::new(&app_view_host_name);
let options = ActorQueryOptions::default().with_did_doc(false);
let actor_info = match client.resolve_actor_info(&handle, Some(options)).await {
Ok(info) => info,
Err(_) => {
return (
StatusCode::NOT_FOUND,
Json(ResolveHandleError {
error: "NotFound".to_string(),
message: "Error: Handle not found".to_string(),
}),
)
.into_response();
}
};
// Check if DID was resolved
match actor_info.did {
Some(did) if did.starts_with("did:") => {
(StatusCode::OK, Json(ResolveHandleResponse { did })).into_response()
}
_ => (
StatusCode::NOT_FOUND,
Json(ResolveHandleError {
error: "NotFound".to_string(),
message: "Error: Handle not found".to_string(),
}),
)
.into_response(),
}
}