-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_session.rs
More file actions
91 lines (80 loc) · 2.41 KB
/
Copy pathget_session.rs
File metadata and controls
91 lines (80 loc) · 2.41 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
//! com.atproto.server.getSession endpoint.
//!
//! Returns information about the current authenticated session.
use std::net::SocketAddr;
use std::sync::Arc;
use axum::{
Json,
extract::{ConnectInfo, State},
http::HeaderMap,
response::{IntoResponse, Response},
};
use serde::Serialize;
use crate::pds::db::StatisticKey;
use crate::pds::server::PdsState;
use crate::pds::auth::{auth_failure_response, check_user_auth};
use super::{get_caller_info};
/// Successful response for getSession.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetSessionResponse {
/// The user's DID.
did: String,
/// The user's handle.
handle: String,
/// The user's email (optional).
#[serde(skip_serializing_if = "Option::is_none")]
email: Option<String>,
/// Whether the email is confirmed.
email_confirmed: bool,
}
/// GET /xrpc/com.atproto.server.getSession - Get current session endpoint.
///
/// Returns information about the authenticated user's current session.
///
/// # Headers
///
/// * `Authorization: Bearer <access_jwt>` - Required
///
/// # Returns
///
/// * `200 OK` with session info on success
/// * `400 Bad Request` if token is expired
/// * `401 Unauthorized` if not authenticated
pub async fn get_session(
State(state): State<Arc<PdsState>>,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
headers: HeaderMap,
) -> 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.server.getSession".to_string(),
ip_address,
user_agent,
};
let _ = state.db.increment_statistic_for_endpoint(&stat_key);
// Check authentication (supports Legacy and OAuth)
let auth_result = check_user_auth(
&state,
&headers,
None,
"GET",
"/xrpc/com.atproto.server.getSession",
);
if !auth_result.is_authenticated {
return auth_failure_response(&auth_result);
}
// Get user info from config
let did = state.db.get_config_property("UserDid").unwrap_or_default();
let handle = state.db.get_config_property("UserHandle").unwrap_or_default();
let email = state.db.get_config_property("UserEmail").ok();
Json(GetSessionResponse {
did,
handle,
email,
email_confirmed: true,
})
.into_response()
}