mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
feat: Implement a web-based account management dashboard
This commit is contained in:
@@ -39,7 +39,7 @@ impl crate::Service for Service {
|
||||
let url_preview_user_agent = config
|
||||
.url_preview_user_agent
|
||||
.clone()
|
||||
.unwrap_or_else(|| conduwuit::version::user_agent_media().to_owned());
|
||||
.unwrap_or_else(|| conduwuit::user_agent_media().to_owned());
|
||||
|
||||
Ok(Arc::new(Self {
|
||||
default: base(config)?
|
||||
@@ -149,7 +149,7 @@ fn base(config: &Config) -> Result<reqwest::ClientBuilder> {
|
||||
.timeout(Duration::from_secs(config.request_total_timeout))
|
||||
.pool_idle_timeout(Duration::from_secs(config.request_idle_timeout))
|
||||
.pool_max_idle_per_host(config.request_idle_per_host.into())
|
||||
.user_agent(conduwuit::version::user_agent())
|
||||
.user_agent(conduwuit::user_agent())
|
||||
.redirect(redirect::Policy::limited(6))
|
||||
.danger_accept_invalid_certs(config.allow_invalid_tls_certificates_yes_i_know_what_the_fuck_i_am_doing_with_this_and_i_know_this_is_insecure)
|
||||
.connection_verbose(cfg!(debug_assertions));
|
||||
|
||||
@@ -181,7 +181,7 @@ impl Service {
|
||||
eprintln!(
|
||||
"Welcome to {} {}!",
|
||||
"Continuwuity".bold().bright_magenta(),
|
||||
conduwuit::version::version().bold()
|
||||
conduwuit::version().bold()
|
||||
);
|
||||
eprintln!();
|
||||
eprintln!(
|
||||
|
||||
@@ -28,7 +28,6 @@ pub mod mailer;
|
||||
pub mod media;
|
||||
pub mod moderation;
|
||||
pub mod oauth;
|
||||
pub mod password_reset;
|
||||
pub mod presence;
|
||||
pub mod pusher;
|
||||
pub mod registration_tokens;
|
||||
|
||||
@@ -5,35 +5,36 @@ use serde::{Deserialize, Deserializer, Serialize};
|
||||
use url::Url;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
#[non_exhaustive]
|
||||
pub struct ClientMetadata {
|
||||
#[serde(default)]
|
||||
application_type: ApplicationType,
|
||||
pub application_type: ApplicationType,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
client_name: Option<String>,
|
||||
pub client_name: Option<String>,
|
||||
|
||||
client_uri: Url,
|
||||
pub client_uri: Url,
|
||||
|
||||
#[serde(default, deserialize_with = "btreeset_skip_err")]
|
||||
grant_types: BTreeSet<GrantType>,
|
||||
pub grant_types: BTreeSet<GrantType>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
logo_uri: Option<Url>,
|
||||
pub logo_uri: Option<Url>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
policy_uri: Option<Url>,
|
||||
pub policy_uri: Option<Url>,
|
||||
|
||||
#[serde(default)]
|
||||
redirect_uris: Vec<Url>,
|
||||
pub redirect_uris: Vec<Url>,
|
||||
|
||||
#[serde(default, deserialize_with = "btreeset_skip_err")]
|
||||
response_types: BTreeSet<ResponseType>,
|
||||
pub response_types: BTreeSet<ResponseType>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
token_endpoint_auth_method: Option<String>,
|
||||
pub token_endpoint_auth_method: Option<String>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
tos_uri: Option<Url>,
|
||||
pub tos_uri: Option<Url>,
|
||||
}
|
||||
|
||||
impl ClientMetadata {
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use base64::Engine;
|
||||
use conduwuit::{Result, utils::hash::sha256};
|
||||
use database::{Deserialized, Json, Map};
|
||||
use ruma::{DeviceId, OwnedUserId, UserId};
|
||||
|
||||
use crate::{Dep, config, oauth::client_metadata::ClientMetadata};
|
||||
|
||||
@@ -11,6 +16,7 @@ pub mod client_metadata;
|
||||
pub struct Service {
|
||||
services: Services,
|
||||
db: Data,
|
||||
tickets: Mutex<HashMap<String, HashMap<OAuthTicket, SystemTime>>>,
|
||||
}
|
||||
|
||||
struct Data {
|
||||
@@ -21,6 +27,22 @@ struct Services {
|
||||
config: Dep<config::Service>,
|
||||
}
|
||||
|
||||
/// A time-limited grant for a client to perform some sensitive action.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum OAuthTicket {
|
||||
CrossSigningReset,
|
||||
}
|
||||
|
||||
impl OAuthTicket {
|
||||
const MAX_AGE: Duration = Duration::from_mins(10);
|
||||
|
||||
pub fn ticket_issue_path(&self) -> &'static str {
|
||||
match self {
|
||||
| Self::CrossSigningReset => "/account/cross_signing_reset",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Service for Service {
|
||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(Self {
|
||||
@@ -30,6 +52,7 @@ impl crate::Service for Service {
|
||||
db: Data {
|
||||
clientid_clientmetadata: args.db["clientid_clientmetadata"].clone(),
|
||||
},
|
||||
tickets: Mutex::default(),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -61,7 +84,7 @@ impl Service {
|
||||
Ok(client_id)
|
||||
}
|
||||
|
||||
async fn get_client_registration(&self, client_id: &str) -> Option<ClientMetadata> {
|
||||
pub async fn get_client_registration(&self, client_id: &str) -> Option<ClientMetadata> {
|
||||
self.db
|
||||
.clientid_clientmetadata
|
||||
.get(client_id)
|
||||
@@ -69,4 +92,33 @@ impl Service {
|
||||
.deserialized()
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub async fn get_client_id_for_device(&self, _device_id: &DeviceId) -> Option<String> {
|
||||
None // TODO
|
||||
}
|
||||
|
||||
/// Issue a ticket for `localpart` to perform some action.
|
||||
pub fn issue_ticket(&self, localpart: String, ticket: OAuthTicket) {
|
||||
self.tickets
|
||||
.lock()
|
||||
.expect("should be able to lock tickets")
|
||||
.entry(localpart)
|
||||
.or_default()
|
||||
.insert(ticket, SystemTime::now());
|
||||
}
|
||||
|
||||
/// Try to consume an unexpired ticket for `localpart`.
|
||||
pub fn try_consume_ticket(&self, localpart: &str, ticket: OAuthTicket) -> bool {
|
||||
let now = SystemTime::now();
|
||||
|
||||
self.tickets
|
||||
.lock()
|
||||
.expect("should be able to lock tickets")
|
||||
.get_mut(localpart)
|
||||
.and_then(|tickets| tickets.remove(&ticket))
|
||||
.is_some_and(|issued| {
|
||||
now.duration_since(issued)
|
||||
.is_ok_and(|duration| duration < OAuthTicket::MAX_AGE)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
use std::{
|
||||
sync::Arc,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use conduwuit::utils::{ReadyExt, stream::TryExpect};
|
||||
use database::{Database, Deserialized, Json, Map};
|
||||
use ruma::{OwnedUserId, UserId};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub(super) struct Data {
|
||||
passwordresettoken_info: Arc<Map>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ResetTokenInfo {
|
||||
pub user: OwnedUserId,
|
||||
pub issued_at: SystemTime,
|
||||
}
|
||||
|
||||
impl ResetTokenInfo {
|
||||
// one hour
|
||||
const MAX_TOKEN_AGE: Duration = Duration::from_hours(1);
|
||||
|
||||
pub fn is_valid(&self) -> bool {
|
||||
let now = SystemTime::now();
|
||||
|
||||
now.duration_since(self.issued_at)
|
||||
.is_ok_and(|duration| duration < Self::MAX_TOKEN_AGE)
|
||||
}
|
||||
}
|
||||
|
||||
impl Data {
|
||||
pub(super) fn new(db: &Arc<Database>) -> Self {
|
||||
Self {
|
||||
passwordresettoken_info: db["passwordresettoken_info"].clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Associate a reset token with its info in the database.
|
||||
pub(super) fn save_token(&self, token: &str, info: &ResetTokenInfo) {
|
||||
self.passwordresettoken_info.raw_put(token, Json(info));
|
||||
}
|
||||
|
||||
/// Lookup the info for a reset token.
|
||||
pub(super) async fn lookup_token_info(&self, token: &str) -> Option<ResetTokenInfo> {
|
||||
self.passwordresettoken_info
|
||||
.get(token)
|
||||
.await
|
||||
.deserialized()
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Find a user's existing reset token, if any.
|
||||
pub(super) async fn find_token_for_user(
|
||||
&self,
|
||||
user: &UserId,
|
||||
) -> Option<(String, ResetTokenInfo)> {
|
||||
self.passwordresettoken_info
|
||||
.stream::<'_, String, ResetTokenInfo>()
|
||||
.expect_ok()
|
||||
.ready_find(|(_, info)| info.user == user)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Remove a reset token.
|
||||
pub(super) fn remove_token(&self, token: &str) { self.passwordresettoken_info.remove(token); }
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
mod data;
|
||||
|
||||
use std::{sync::Arc, time::SystemTime};
|
||||
|
||||
use conduwuit::{Err, Result, utils};
|
||||
use data::{Data, ResetTokenInfo};
|
||||
use ruma::OwnedUserId;
|
||||
|
||||
use crate::{
|
||||
Dep, globals,
|
||||
users::{self, HashedPassword},
|
||||
};
|
||||
|
||||
pub const PASSWORD_RESET_PATH: &str = "/_continuwuity/account/reset_password";
|
||||
pub const RESET_TOKEN_QUERY_PARAM: &str = "token";
|
||||
const RESET_TOKEN_LENGTH: usize = 32;
|
||||
|
||||
pub struct Service {
|
||||
db: Data,
|
||||
services: Services,
|
||||
}
|
||||
|
||||
struct Services {
|
||||
users: Dep<users::Service>,
|
||||
globals: Dep<globals::Service>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ValidResetToken {
|
||||
pub token: String,
|
||||
pub info: ResetTokenInfo,
|
||||
}
|
||||
|
||||
impl crate::Service for Service {
|
||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
Ok(Arc::new(Self {
|
||||
db: Data::new(args.db),
|
||||
services: Services {
|
||||
users: args.depend::<users::Service>("users"),
|
||||
globals: args.depend::<globals::Service>("globals"),
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Generate a random string suitable to be used as a password reset token.
|
||||
#[must_use]
|
||||
pub fn generate_token_string() -> String { utils::random_string(RESET_TOKEN_LENGTH) }
|
||||
|
||||
/// Issue a password reset token for `user`, who must be a local user with
|
||||
/// the `password` origin.
|
||||
pub async fn issue_token(&self, user_id: OwnedUserId) -> Result<ValidResetToken> {
|
||||
if !self.services.globals.user_is_local(&user_id) {
|
||||
return Err!("Cannot issue a password reset token for remote user {user_id}");
|
||||
}
|
||||
|
||||
if user_id == self.services.globals.server_user {
|
||||
return Err!("Cannot issue a password reset token for the server user");
|
||||
}
|
||||
|
||||
if self.services.users.is_deactivated(&user_id).await? {
|
||||
return Err!("Cannot issue a password reset token for deactivated user {user_id}");
|
||||
}
|
||||
|
||||
if let Some((existing_token, _)) = self.db.find_token_for_user(&user_id).await {
|
||||
self.db.remove_token(&existing_token);
|
||||
}
|
||||
|
||||
let token = Self::generate_token_string();
|
||||
let info = ResetTokenInfo {
|
||||
user: user_id,
|
||||
issued_at: SystemTime::now(),
|
||||
};
|
||||
|
||||
self.db.save_token(&token, &info);
|
||||
|
||||
Ok(ValidResetToken { token, info })
|
||||
}
|
||||
|
||||
/// Check if `token` represents a valid, non-expired password reset token.
|
||||
pub async fn check_token(&self, token: &str) -> Option<ValidResetToken> {
|
||||
self.db.lookup_token_info(token).await.and_then(|info| {
|
||||
if info.is_valid() {
|
||||
Some(ValidResetToken { token: token.to_owned(), info })
|
||||
} else {
|
||||
self.db.remove_token(token);
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Consume the supplied valid token, using it to change its user's password
|
||||
/// to `new_password`.
|
||||
pub async fn consume_token(
|
||||
&self,
|
||||
ValidResetToken { token, info }: ValidResetToken,
|
||||
new_password: &str,
|
||||
) -> Result<()> {
|
||||
if info.is_valid() {
|
||||
self.db.remove_token(&token);
|
||||
self.services
|
||||
.users
|
||||
.set_password(&info.user, Some(HashedPassword::new(new_password)?));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ use crate::{
|
||||
account_data, admin, announcements, antispam, appservice, client, config, emergency,
|
||||
federation, firstrun, globals, key_backups, mailer,
|
||||
manager::Manager,
|
||||
media, moderation, oauth, password_reset, presence, pusher, registration_tokens, resolver,
|
||||
rooms, sending, server_keys,
|
||||
media, moderation, oauth, presence, pusher, registration_tokens, resolver, rooms, sending,
|
||||
server_keys,
|
||||
service::{self, Args, Map, Service},
|
||||
sync, threepid, transactions, uiaa, users,
|
||||
};
|
||||
@@ -28,7 +28,6 @@ pub struct Services {
|
||||
pub key_backups: Arc<key_backups::Service>,
|
||||
pub media: Arc<media::Service>,
|
||||
pub oauth: Arc<oauth::Service>,
|
||||
pub password_reset: Arc<password_reset::Service>,
|
||||
pub mailer: Arc<mailer::Service>,
|
||||
pub presence: Arc<presence::Service>,
|
||||
pub pusher: Arc<pusher::Service>,
|
||||
@@ -86,7 +85,6 @@ impl Services {
|
||||
key_backups: build!(key_backups::Service),
|
||||
media: build!(media::Service),
|
||||
oauth: build!(oauth::Service),
|
||||
password_reset: build!(password_reset::Service),
|
||||
mailer: build!(mailer::Service),
|
||||
presence: build!(presence::Service),
|
||||
pusher: build!(pusher::Service),
|
||||
|
||||
@@ -9,8 +9,9 @@ use ruma::{
|
||||
ClientSecret, OwnedClientSecret, OwnedSessionId, SessionId,
|
||||
api::error::{ErrorKind, LimitExceededErrorData},
|
||||
};
|
||||
use tokio::sync::MutexGuard;
|
||||
|
||||
mod session;
|
||||
pub mod session;
|
||||
|
||||
use crate::{
|
||||
Args, Dep, config,
|
||||
@@ -26,6 +27,7 @@ pub struct Service {
|
||||
ratelimiter: DefaultKeyedRateLimiter<Address>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum EmailRequirement {
|
||||
/// Users may change their email, but cannot remove it entirely.
|
||||
Required,
|
||||
@@ -219,13 +221,12 @@ impl Service {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Consume a validated validation session, removing it from the database
|
||||
/// and returning the newly validated email address.
|
||||
pub async fn consume_valid_session(
|
||||
/// Get a validated validation session.
|
||||
pub async fn get_valid_session(
|
||||
&self,
|
||||
session_id: &SessionId,
|
||||
client_secret: &ClientSecret,
|
||||
) -> Result<Address, Cow<'static, str>> {
|
||||
) -> Result<ValidSession<'_>, Cow<'static, str>> {
|
||||
let mut sessions = self.sessions.lock().await;
|
||||
|
||||
let Some(session) = sessions.get_session(session_id) else {
|
||||
@@ -235,9 +236,13 @@ impl Service {
|
||||
if session.client_secret == client_secret
|
||||
&& matches!(session.validation_state, ValidationState::Validated)
|
||||
{
|
||||
let session = sessions.remove_session(session_id);
|
||||
let email = session.email.clone();
|
||||
|
||||
Ok(session.email)
|
||||
Ok(ValidSession {
|
||||
email,
|
||||
session_id: session_id.to_owned(),
|
||||
sessions,
|
||||
})
|
||||
} else {
|
||||
Err("This email address has not been validated. Did you use the link that was sent \
|
||||
to you?"
|
||||
@@ -313,3 +318,20 @@ impl Service {
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ValidSession<'lock> {
|
||||
pub email: Address,
|
||||
session_id: OwnedSessionId,
|
||||
sessions: MutexGuard<'lock, ValidationSessions>,
|
||||
}
|
||||
|
||||
impl ValidSession<'_> {
|
||||
/// Consume this session, removing it from the database and releasing the
|
||||
/// lock it holds.
|
||||
#[must_use]
|
||||
pub fn consume(mut self) -> Address {
|
||||
self.sessions.remove_session(&self.session_id);
|
||||
|
||||
self.email
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ use lettre::Address;
|
||||
use ruma::{ClientSecret, OwnedClientSecret, OwnedSessionId, SessionId};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct ValidationSessions {
|
||||
pub struct ValidationSessions {
|
||||
sessions: HashMap<OwnedSessionId, ValidationSession>,
|
||||
client_secrets: HashMap<OwnedClientSecret, OwnedSessionId>,
|
||||
}
|
||||
|
||||
/// A pending or completed email validation session.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct ValidationSession {
|
||||
pub struct ValidationSession {
|
||||
/// The session's ID
|
||||
pub session_id: OwnedSessionId,
|
||||
/// The client's supplied client secret
|
||||
@@ -28,7 +28,7 @@ pub(crate) struct ValidationSession {
|
||||
|
||||
/// The state of an email validation session.
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum ValidationState {
|
||||
pub enum ValidationState {
|
||||
/// The session is waiting for this validation token to be provided
|
||||
Pending(ValidationToken),
|
||||
/// The session has been validated
|
||||
@@ -36,7 +36,7 @@ pub(crate) enum ValidationState {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct ValidationToken {
|
||||
pub struct ValidationToken {
|
||||
pub token: String,
|
||||
pub issued_at: SystemTime,
|
||||
}
|
||||
@@ -69,7 +69,7 @@ impl ValidationSessions {
|
||||
const RANDOM_SID_LENGTH: usize = 16;
|
||||
|
||||
#[must_use]
|
||||
pub(super) fn generate_session_id() -> OwnedSessionId {
|
||||
pub fn generate_session_id() -> OwnedSessionId {
|
||||
SessionId::parse(utils::random_string(Self::RANDOM_SID_LENGTH)).unwrap()
|
||||
}
|
||||
|
||||
|
||||
+277
-149
@@ -5,9 +5,10 @@ use std::{
|
||||
};
|
||||
|
||||
use conduwuit::{Err, Error, Result, error, utils};
|
||||
use futures::future::OptionFuture;
|
||||
use lettre::Address;
|
||||
use ruma::{
|
||||
UserId,
|
||||
DeviceId, UserId,
|
||||
api::{
|
||||
client::uiaa::{
|
||||
AuthData, AuthFlow, AuthType, EmailIdentity, EmailUserIdentifier,
|
||||
@@ -16,11 +17,19 @@ use ruma::{
|
||||
},
|
||||
error::{ErrorKind, StandardErrorBody},
|
||||
},
|
||||
assign,
|
||||
};
|
||||
use serde_json::{
|
||||
json,
|
||||
value::{RawValue, to_raw_value},
|
||||
};
|
||||
use serde_json::value::RawValue;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{Dep, config, globals, registration_tokens, threepid, users};
|
||||
use crate::{
|
||||
Dep, config, globals,
|
||||
oauth::{self, OAuthTicket},
|
||||
registration_tokens, threepid, users,
|
||||
};
|
||||
|
||||
pub struct Service {
|
||||
services: Services,
|
||||
@@ -33,6 +42,7 @@ struct Services {
|
||||
config: Dep<config::Service>,
|
||||
registration_tokens: Dep<registration_tokens::Service>,
|
||||
threepid: Dep<threepid::Service>,
|
||||
oauth: Dep<oauth::Service>,
|
||||
}
|
||||
|
||||
impl crate::Service for Service {
|
||||
@@ -45,6 +55,7 @@ impl crate::Service for Service {
|
||||
registration_tokens: args
|
||||
.depend::<registration_tokens::Service>("registration_tokens"),
|
||||
threepid: args.depend::<threepid::Service>("threepid"),
|
||||
oauth: args.depend::<oauth::Service>("oauth"),
|
||||
},
|
||||
uiaa_sessions: Mutex::new(HashMap::new()),
|
||||
}))
|
||||
@@ -54,8 +65,54 @@ impl crate::Service for Service {
|
||||
}
|
||||
|
||||
struct UiaaSession {
|
||||
session_metadata: UiaaSessionMetadata,
|
||||
info: UiaaInfo,
|
||||
identity: Identity,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum UiaaSessionMetadata {
|
||||
Legacy {
|
||||
identity: Identity,
|
||||
},
|
||||
OAuth {
|
||||
localpart: String,
|
||||
ticket: OAuthTicket,
|
||||
},
|
||||
}
|
||||
|
||||
impl UiaaSessionMetadata {
|
||||
fn into_identity(self) -> Identity {
|
||||
match self {
|
||||
| UiaaSessionMetadata::Legacy { identity } => identity,
|
||||
| UiaaSessionMetadata::OAuth { localpart, .. } =>
|
||||
assign!(Identity::default(), { localpart: Some(localpart) }),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Information about the user which is initiating this UIAA session.
|
||||
pub struct UiaaInitiator<'a> {
|
||||
user_id: &'a UserId,
|
||||
device_id: Option<&'a DeviceId>,
|
||||
oauth_ticket: Option<OAuthTicket>,
|
||||
}
|
||||
|
||||
impl<'a> UiaaInitiator<'a> {
|
||||
pub fn new(user_id: &'a UserId, device_id: Option<&'a DeviceId>) -> Self {
|
||||
Self { user_id, device_id, oauth_ticket: None }
|
||||
}
|
||||
|
||||
pub fn with_oauth_ticket(
|
||||
user_id: &'a UserId,
|
||||
device_id: Option<&'a DeviceId>,
|
||||
oauth_ticket: OAuthTicket,
|
||||
) -> Self {
|
||||
Self {
|
||||
user_id,
|
||||
device_id,
|
||||
oauth_ticket: Some(oauth_ticket),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Information about the authenticated user's identity.
|
||||
@@ -106,7 +163,7 @@ impl Identity {
|
||||
/// Create an Identity with the localpart of the provided user ID
|
||||
/// and all other fields set to None.
|
||||
#[must_use]
|
||||
pub fn from_user_id(user_id: &UserId) -> Self {
|
||||
fn from_user_id(user_id: &UserId) -> Self {
|
||||
Self {
|
||||
localpart: Some(user_id.localpart().to_owned()),
|
||||
..Default::default()
|
||||
@@ -124,11 +181,11 @@ impl Service {
|
||||
auth: &Option<AuthData>,
|
||||
flows: Vec<AuthFlow>,
|
||||
params: Box<RawValue>,
|
||||
identity: Option<Identity>,
|
||||
initiator: Option<UiaaInitiator<'_>>,
|
||||
) -> Result<Identity> {
|
||||
match auth.as_ref() {
|
||||
| None => {
|
||||
let info = self.create_session(flows, params, identity).await;
|
||||
let info = self.create_session(flows, params, initiator).await?;
|
||||
|
||||
Err(Error::Uiaa(info))
|
||||
},
|
||||
@@ -140,8 +197,8 @@ impl Service {
|
||||
// session if they want to start the UIAA exchange with existing
|
||||
// authentication data. If that happens, we create a new session
|
||||
// here.
|
||||
self.create_session(flows, params, identity)
|
||||
.await
|
||||
self.create_session(flows, params, initiator)
|
||||
.await?
|
||||
.session
|
||||
.unwrap()
|
||||
.into()
|
||||
@@ -161,13 +218,15 @@ impl Service {
|
||||
pub async fn authenticate_password(
|
||||
&self,
|
||||
auth: &Option<AuthData>,
|
||||
identity: Option<Identity>,
|
||||
user_id: &UserId,
|
||||
device_id: Option<&DeviceId>,
|
||||
oauth_ticket: Option<OAuthTicket>,
|
||||
) -> Result<Identity> {
|
||||
self.authenticate(
|
||||
auth,
|
||||
vec![AuthFlow::new(vec![AuthType::Password])],
|
||||
Box::default(),
|
||||
identity,
|
||||
Some(UiaaInitiator { user_id, device_id, oauth_ticket }),
|
||||
)
|
||||
.await
|
||||
}
|
||||
@@ -183,20 +242,64 @@ impl Service {
|
||||
&self,
|
||||
flows: Vec<AuthFlow>,
|
||||
params: Box<RawValue>,
|
||||
identity: Option<Identity>,
|
||||
) -> UiaaInfo {
|
||||
initiator: Option<UiaaInitiator<'_>>,
|
||||
) -> Result<UiaaInfo> {
|
||||
let mut uiaa_sessions = self.uiaa_sessions.lock().await;
|
||||
|
||||
let session_id = utils::random_string(Self::SESSION_ID_LENGTH);
|
||||
let mut info = assign::assign!(UiaaInfo::new(flows), {params: Some(params)});
|
||||
info.session = Some(session_id.clone());
|
||||
|
||||
uiaa_sessions.insert(session_id, UiaaSession {
|
||||
info: info.clone(),
|
||||
identity: identity.unwrap_or_default(),
|
||||
});
|
||||
let mut info = assign!(UiaaInfo::new(flows), { params: Some(params), session: Some(session_id.clone()) });
|
||||
|
||||
info
|
||||
let session_metadata = if let Some(initiator) = initiator {
|
||||
let is_oauth = OptionFuture::from(
|
||||
initiator.device_id.map(async |device_id| {
|
||||
self
|
||||
.services
|
||||
.oauth
|
||||
.get_client_id_for_device(device_id)
|
||||
.await
|
||||
})
|
||||
)
|
||||
.await
|
||||
.is_some();
|
||||
|
||||
if is_oauth {
|
||||
if let Some(oauth_ticket) = initiator.oauth_ticket {
|
||||
let ticket_url = self
|
||||
.services
|
||||
.config
|
||||
.get_client_domain()
|
||||
.join(&format!(
|
||||
"{}{}",
|
||||
conduwuit_core::ROUTE_PREFIX,
|
||||
oauth_ticket.ticket_issue_path()
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
info.flows = vec![AuthFlow::new(vec![AuthType::OAuth])];
|
||||
info.params = Some(to_raw_value(&json!({"url": ticket_url})).unwrap());
|
||||
|
||||
UiaaSessionMetadata::OAuth {
|
||||
localpart: initiator.user_id.localpart().to_owned(),
|
||||
ticket: oauth_ticket,
|
||||
}
|
||||
} else {
|
||||
return Err!(Request(Forbidden(
|
||||
"Clients authorized with OAuth cannot use this route."
|
||||
)));
|
||||
}
|
||||
} else {
|
||||
UiaaSessionMetadata::Legacy {
|
||||
identity: Identity::from_user_id(initiator.user_id),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
UiaaSessionMetadata::Legacy { identity: Identity::default() }
|
||||
};
|
||||
|
||||
uiaa_sessions.insert(session_id, UiaaSession { session_metadata, info: info.clone() });
|
||||
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
/// Proceed with UIAA authentication given a client's authorization data.
|
||||
@@ -225,7 +328,7 @@ impl Service {
|
||||
}
|
||||
|
||||
let completed = {
|
||||
let UiaaSession { info, identity } = session.get_mut();
|
||||
let UiaaSession { session_metadata, info } = session.get_mut();
|
||||
|
||||
let auth_type = auth.auth_type().expect("auth type should be set");
|
||||
|
||||
@@ -258,12 +361,12 @@ impl Service {
|
||||
|
||||
// If the provided stage hasn't already been completed, check it for completion
|
||||
if !completed_stages.contains(auth_type.as_str()) {
|
||||
match self.check_stage(auth, identity.clone()).await {
|
||||
| Ok((completed_stage, updated_identity)) => {
|
||||
match self.check_stage(auth, session_metadata.clone()).await {
|
||||
| Ok((completed_stage, updated_metadata)) => {
|
||||
info.auth_error = None;
|
||||
completed_stages.insert(completed_stage.to_string());
|
||||
info.completed.push(completed_stage);
|
||||
*identity = updated_identity;
|
||||
*session_metadata = updated_metadata;
|
||||
},
|
||||
| Err(error) => {
|
||||
info.auth_error = Some(error);
|
||||
@@ -279,9 +382,9 @@ impl Service {
|
||||
|
||||
if completed {
|
||||
// This session is complete, remove it and return success
|
||||
let (_, UiaaSession { identity, .. }) = session.remove_entry();
|
||||
let (_, UiaaSession { session_metadata, .. }) = session.remove_entry();
|
||||
|
||||
Ok(Ok(identity))
|
||||
Ok(Ok(session_metadata.into_identity()))
|
||||
} else {
|
||||
// The client needs to try again, return the updated session
|
||||
Ok(Err(session.get().info.clone()))
|
||||
@@ -295,152 +398,177 @@ impl Service {
|
||||
async fn check_stage(
|
||||
&self,
|
||||
auth: &AuthData,
|
||||
mut identity: Identity,
|
||||
) -> Result<(AuthType, Identity), StandardErrorBody> {
|
||||
// Note: This function takes ownership of `identity` because mutations to the
|
||||
// identity must not be applied unless checking the stage succeeds. The
|
||||
// updated identity is returned as part of the Ok value, and
|
||||
// `continue_session` handles saving it to `uiaa_sessions`.
|
||||
mut session_metadata: UiaaSessionMetadata,
|
||||
) -> Result<(AuthType, UiaaSessionMetadata), StandardErrorBody> {
|
||||
// Note: This function takes ownership of `session_metadata` because mutations
|
||||
// to the identity (if it's a legacy session) must not be applied unless
|
||||
// checking the stage succeeds. The updated identity is returned as part of
|
||||
// the Ok value, and `continue_session` handles saving it to `uiaa_sessions`.
|
||||
//
|
||||
// This also means it's fine to mutate `identity` at any point in this function,
|
||||
// because those mutations won't be saved unless the function returns Ok.
|
||||
|
||||
match auth {
|
||||
| AuthData::Dummy(_) => Ok(AuthType::Dummy),
|
||||
| AuthData::EmailIdentity(EmailIdentity {
|
||||
thirdparty_id_creds: ThirdpartyIdCredentials { client_secret, sid, .. },
|
||||
..
|
||||
}) => {
|
||||
match self
|
||||
.services
|
||||
.threepid
|
||||
.consume_valid_session(sid, client_secret)
|
||||
.await
|
||||
{
|
||||
| Ok(email) => {
|
||||
if let Some(localpart) =
|
||||
self.services.threepid.get_localpart_for_email(&email).await
|
||||
{
|
||||
identity.try_set_localpart(localpart)?;
|
||||
}
|
||||
let completed_auth_type = match &mut session_metadata {
|
||||
| UiaaSessionMetadata::OAuth { localpart, ticket } => {
|
||||
// m.oauth is the only valid stage for oauth sessions
|
||||
assert!(
|
||||
matches!(auth, AuthData::OAuth(_)),
|
||||
"got non-oauth auth data for oauth session"
|
||||
);
|
||||
|
||||
identity.try_set_email(email)?;
|
||||
|
||||
Ok(AuthType::EmailIdentity)
|
||||
},
|
||||
| Err(message) => Err(StandardErrorBody::new(
|
||||
ErrorKind::ThreepidAuthFailed,
|
||||
message.into_owned(),
|
||||
)),
|
||||
if self.services.oauth.try_consume_ticket(localpart, *ticket) {
|
||||
Ok(AuthType::OAuth)
|
||||
} else {
|
||||
Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"No OAuth ticket available".to_owned(),
|
||||
))
|
||||
}
|
||||
},
|
||||
#[allow(clippy::useless_let_if_seq)]
|
||||
| AuthData::Password(Password { identifier, password, .. }) => {
|
||||
let user_id_or_localpart = match identifier {
|
||||
| UserIdentifier::Matrix(MatrixUserIdentifier { user, .. }) =>
|
||||
user.to_owned(),
|
||||
| UserIdentifier::Email(EmailUserIdentifier { address, .. }) => {
|
||||
let Ok(email) = Address::try_from(address.to_owned()) else {
|
||||
| UiaaSessionMetadata::Legacy { identity } => {
|
||||
match auth {
|
||||
| AuthData::Dummy(_) => Ok(AuthType::Dummy),
|
||||
| AuthData::EmailIdentity(EmailIdentity {
|
||||
thirdparty_id_creds: ThirdpartyIdCredentials { client_secret, sid, .. },
|
||||
..
|
||||
}) => {
|
||||
match self
|
||||
.services
|
||||
.threepid
|
||||
.get_valid_session(sid, client_secret)
|
||||
.await
|
||||
{
|
||||
| Ok(session) => {
|
||||
let email = session.consume();
|
||||
|
||||
if let Some(localpart) =
|
||||
self.services.threepid.get_localpart_for_email(&email).await
|
||||
{
|
||||
identity.try_set_localpart(localpart)?;
|
||||
}
|
||||
|
||||
identity.try_set_email(email)?;
|
||||
|
||||
Ok(AuthType::EmailIdentity)
|
||||
},
|
||||
| Err(message) => Err(StandardErrorBody::new(
|
||||
ErrorKind::ThreepidAuthFailed,
|
||||
message.into_owned(),
|
||||
)),
|
||||
}
|
||||
},
|
||||
#[allow(clippy::useless_let_if_seq)]
|
||||
| AuthData::Password(Password { identifier, password, .. }) => {
|
||||
let user_id_or_localpart = match identifier {
|
||||
| UserIdentifier::Matrix(MatrixUserIdentifier { user, .. }) =>
|
||||
user.to_owned(),
|
||||
| UserIdentifier::Email(EmailUserIdentifier { address, .. }) => {
|
||||
let Ok(email) = Address::try_from(address.to_owned()) else {
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::InvalidParam,
|
||||
"Email is malformed".to_owned(),
|
||||
));
|
||||
};
|
||||
|
||||
if let Some(localpart) =
|
||||
self.services.threepid.get_localpart_for_email(&email).await
|
||||
{
|
||||
identity.try_set_email(email)?;
|
||||
|
||||
localpart
|
||||
} else {
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"Invalid identifier or password".to_owned(),
|
||||
));
|
||||
}
|
||||
},
|
||||
| _ =>
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::Unrecognized,
|
||||
"Identifier type not recognized".to_owned(),
|
||||
)),
|
||||
};
|
||||
|
||||
let Ok(user_id) = UserId::parse_with_server_name(
|
||||
user_id_or_localpart,
|
||||
self.services.globals.server_name(),
|
||||
) else {
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::InvalidParam,
|
||||
"Email is malformed".to_owned(),
|
||||
"User ID is malformed".to_owned(),
|
||||
));
|
||||
};
|
||||
|
||||
if let Some(localpart) =
|
||||
self.services.threepid.get_localpart_for_email(&email).await
|
||||
if self
|
||||
.services
|
||||
.users
|
||||
.check_password(&user_id, password)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
identity.try_set_email(email)?;
|
||||
identity.try_set_localpart(user_id.localpart().to_owned())?;
|
||||
|
||||
localpart
|
||||
Ok(AuthType::Password)
|
||||
} else {
|
||||
return Err(StandardErrorBody::new(
|
||||
Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"Invalid identifier or password".to_owned(),
|
||||
));
|
||||
))
|
||||
}
|
||||
},
|
||||
| _ =>
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::Unrecognized,
|
||||
"Identifier type not recognized".to_owned(),
|
||||
)),
|
||||
};
|
||||
| AuthData::ReCaptcha(ReCaptcha { response, .. }) => {
|
||||
let Some(ref private_site_key) =
|
||||
self.services.config.recaptcha_private_site_key
|
||||
else {
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"ReCaptcha is not configured".to_owned(),
|
||||
));
|
||||
};
|
||||
|
||||
let Ok(user_id) = UserId::parse_with_server_name(
|
||||
user_id_or_localpart,
|
||||
self.services.globals.server_name(),
|
||||
) else {
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::InvalidParam,
|
||||
"User ID is malformed".to_owned(),
|
||||
));
|
||||
};
|
||||
|
||||
if self
|
||||
.services
|
||||
.users
|
||||
.check_password(&user_id, password)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
identity.try_set_localpart(user_id.localpart().to_owned())?;
|
||||
|
||||
Ok(AuthType::Password)
|
||||
} else {
|
||||
Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"Invalid identifier or password".to_owned(),
|
||||
))
|
||||
}
|
||||
},
|
||||
| AuthData::ReCaptcha(ReCaptcha { response, .. }) => {
|
||||
let Some(ref private_site_key) = self.services.config.recaptcha_private_site_key
|
||||
else {
|
||||
return Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"ReCaptcha is not configured".to_owned(),
|
||||
));
|
||||
};
|
||||
|
||||
match recaptcha_verify::verify_v3(private_site_key, response, None).await {
|
||||
| Ok(()) => Ok(AuthType::ReCaptcha),
|
||||
| Err(e) => {
|
||||
error!("ReCaptcha verification failed: {e:?}");
|
||||
Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"ReCaptcha verification failed".to_owned(),
|
||||
))
|
||||
match recaptcha_verify::verify_v3(private_site_key, response, None).await
|
||||
{
|
||||
| Ok(()) => Ok(AuthType::ReCaptcha),
|
||||
| Err(e) => {
|
||||
error!("ReCaptcha verification failed: {e:?}");
|
||||
Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"ReCaptcha verification failed".to_owned(),
|
||||
))
|
||||
},
|
||||
}
|
||||
},
|
||||
| AuthData::RegistrationToken(RegistrationToken { token, .. }) => {
|
||||
let token = token.trim().to_owned();
|
||||
|
||||
if let Some(valid_token) = self
|
||||
.services
|
||||
.registration_tokens
|
||||
.validate_token(token)
|
||||
.await
|
||||
{
|
||||
self.services
|
||||
.registration_tokens
|
||||
.mark_token_as_used(valid_token);
|
||||
|
||||
Ok(AuthType::RegistrationToken)
|
||||
} else {
|
||||
Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"Invalid registration token".to_owned(),
|
||||
))
|
||||
}
|
||||
},
|
||||
| AuthData::Terms(_) => Ok(AuthType::Terms),
|
||||
| _ => Err(StandardErrorBody::new(
|
||||
ErrorKind::Unrecognized,
|
||||
"Unsupported stage type".into(),
|
||||
)),
|
||||
}
|
||||
},
|
||||
| AuthData::RegistrationToken(RegistrationToken { token, .. }) => {
|
||||
let token = token.trim().to_owned();
|
||||
}?;
|
||||
|
||||
if let Some(valid_token) = self
|
||||
.services
|
||||
.registration_tokens
|
||||
.validate_token(token)
|
||||
.await
|
||||
{
|
||||
self.services
|
||||
.registration_tokens
|
||||
.mark_token_as_used(valid_token);
|
||||
|
||||
Ok(AuthType::RegistrationToken)
|
||||
} else {
|
||||
Err(StandardErrorBody::new(
|
||||
ErrorKind::Forbidden,
|
||||
"Invalid registration token".to_owned(),
|
||||
))
|
||||
}
|
||||
},
|
||||
| AuthData::Terms(_) => Ok(AuthType::Terms),
|
||||
| _ => Err(StandardErrorBody::new(
|
||||
ErrorKind::Unrecognized,
|
||||
"Unsupported stage type".into(),
|
||||
)),
|
||||
}
|
||||
.map(|auth_type| (auth_type, identity))
|
||||
Ok((completed_auth_type, session_metadata))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user