reifydb-auth 0.10.3

Authentication and authorization module for ReifyDB
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use std::collections::{BTreeMap, HashMap};

use reifydb_core::{
	interface::{
		auth::{AuthStep, AuthenticationProvider},
		catalog::{authentication_method::AuthenticationMethod, identity::Identity},
	},
	retry::RetryStrategy,
};
use reifydb_transaction::transaction::Transaction;
use reifydb_value::{
	error::Error,
	reifydb_assertions,
	value::{
		Value,
		duration::Duration,
		identity::{IdentityId, IdentityKind},
	},
};
use subtle::ConstantTimeEq;
use tracing::warn;

use super::{AuthResponse, AuthService, GithubAuth, generate_session_token};
use crate::{
	challenge::ChallengeInfo,
	error::AuthError,
	github::{GithubConfig, GithubUser, build_authorize_url},
};

pub(crate) const GITHUB_USER_ID_ATTRIBUTE: &str = "github_user_id";

impl AuthService {
	pub(crate) fn begin_github_login(
		&self,
		caller: IdentityId,
		method: &AuthenticationMethod,
	) -> Result<AuthResponse, Error> {
		let Some(github) = self.github_auth(method) else {
			return Ok(not_configured());
		};

		let state = generate_session_token(&self.rng);
		let payload = HashMap::from([
			("state".to_string(), state.clone()),
			("authorize_url".to_string(), build_authorize_url(&github.config, &state)),
		]);

		let challenge_id = self.challenges.create(
			caller,
			"github".to_string(),
			"github".to_string(),
			payload.clone(),
			None,
			&self.clock,
			&self.rng,
		);

		Ok(AuthResponse::Challenge {
			challenge_id,
			payload,
		})
	}

	pub(crate) fn complete_github_login(
		&self,
		challenge: &ChallengeInfo,
		credentials: &HashMap<String, String>,
		method: &AuthenticationMethod,
	) -> Result<AuthResponse, Error> {
		let Some(github) = self.github_auth(method) else {
			return Ok(not_configured());
		};
		let ttl = method.ttl;

		if !state_matches(challenge, credentials) {
			return Ok(AuthResponse::Failed {
				reason: "invalid oauth state".to_string(),
			});
		}

		let Some(code) = credentials.get("code") else {
			return Ok(AuthResponse::Failed {
				reason: "missing oauth code".to_string(),
			});
		};

		let user = match self.verify_github_user(&github, code) {
			Ok(user) => user,
			Err(e) => {
				warn!("github login failed: {:?}", e);
				return Ok(AuthResponse::Failed {
					reason: "github verification failed".to_string(),
				});
			}
		};

		match self.find_github_identity(&user)? {
			Some(ident) if !ident.enabled => Ok(AuthResponse::Failed {
				reason: "identity is disabled".to_string(),
			}),
			Some(ident) => self.authenticate_github_identity(ident.id, &user, ttl),
			None if method.promote_guest && self.is_guest(challenge.caller)? => {
				self.promote_github_guest(challenge.caller, &user, ttl)
			}
			None => self.auto_provision_github(&user, ttl),
		}
	}

	fn github_auth(&self, method: &AuthenticationMethod) -> Option<GithubAuth> {
		let client_secret = self.auth_registry.github_client_secret()?.to_string();
		let setting = |key: &str| {
			method.properties.get(key).cloned().unwrap_or_else(|| {
				panic!("github method row lacks {key}, which configure_method requires")
			})
		};
		Some(GithubAuth {
			config: GithubConfig {
				client_id: setting("client_id"),
				client_secret,
				redirect_uri: setting("redirect_uri"),
			},
			api: self.github_api.clone(),
		})
	}

	#[inline]
	fn verify_github_user(&self, github: &GithubAuth, code: &str) -> Result<GithubUser, Error> {
		let access_token = github.api.exchange_code(&github.config, code)?;
		github.api.fetch_user(&access_token)
	}

	#[inline]
	fn find_github_identity(&self, user: &GithubUser) -> Result<Option<Identity>, Error> {
		let mut txn = self.engine.begin_query()?;
		let catalog = self.engine.catalog();
		catalog.find_identity_by_attribute_value(
			&mut Transaction::Query(&mut txn),
			GITHUB_USER_ID_ATTRIBUTE,
			&Value::Utf8(user.id.to_string()),
		)
	}

	#[inline]
	fn authenticate_github_identity(
		&self,
		identity: IdentityId,
		user: &GithubUser,
		ttl: Option<Duration>,
	) -> Result<AuthResponse, Error> {
		let stored_auth = {
			let mut txn = self.engine.begin_query()?;
			let catalog = self.engine.catalog();
			catalog.find_authentication_by_identity_and_method(
				&mut Transaction::Query(&mut txn),
				identity,
				"github",
			)?
		};
		let Some(stored_auth) = stored_auth else {
			return Ok(invalid_credentials());
		};

		let provider = self.github_provider()?;
		let credentials = HashMap::from([("github_user_id".to_string(), user.id.to_string())]);
		match provider.authenticate(&stored_auth.properties, &credentials)? {
			AuthStep::Authenticated => {
				self.record_github_login(identity, &user.login)?;
				self.finalize_authentication(identity, ttl)
			}
			_ => Ok(invalid_credentials()),
		}
	}

	fn promote_github_guest(
		&self,
		guest: IdentityId,
		user: &GithubUser,
		ttl: Option<Duration>,
	) -> Result<AuthResponse, Error> {
		let properties = self.github_properties(user)?;
		let user_id = user.id.to_string();
		let response = self.promote_guest(
			guest,
			&format!("github:{}", user.id),
			"github",
			properties,
			Some((GITHUB_USER_ID_ATTRIBUTE, &user_id)),
			ttl,
		)?;
		self.record_github_login(guest, &user.login)?;
		Ok(response)
	}

	fn github_properties(&self, user: &GithubUser) -> Result<BTreeMap<String, String>, Error> {
		self.github_provider()?.create(
			&self.rng,
			&HashMap::from([
				("user_id".to_string(), user.id.to_string()),
				("login".to_string(), user.login.clone()),
			]),
		)
	}

	#[inline]
	fn auto_provision_github(&self, user: &GithubUser, ttl: Option<Duration>) -> Result<AuthResponse, Error> {
		let properties = self.github_properties(user)?;

		let identity = self.create_github_identity(user, properties)?;
		self.record_github_login(identity, &user.login)?;
		self.finalize_authentication(identity, ttl)
	}

	#[inline]
	fn create_github_identity(
		&self,
		user: &GithubUser,
		properties: BTreeMap<String, String>,
	) -> Result<IdentityId, Error> {
		let catalog = self.engine.catalog();
		let identifier = format!("github:{}", user.id);
		let ident = RetryStrategy::default().retry(&self.rng, "auth::create_github_identity", || {
			let mut admin = self.engine.begin_admin()?;
			let ident = catalog.create_identity(
				&mut admin,
				&identifier,
				IdentityKind::User,
				&self.clock,
				&self.rng,
			)?;
			catalog.create_authentication(&mut admin, ident.id, "github", properties.clone())?;
			self.set_lookup_attribute(
				&mut admin,
				ident.id,
				GITHUB_USER_ID_ATTRIBUTE,
				&user.id.to_string(),
			)?;
			admin.commit()?;
			Ok(ident)
		})?;

		reifydb_assertions! {
			assert!(
				ident.id != IdentityId::default(),
				"auto-provisioning created the nil placeholder identity instead of a freshly generated one, so the provisioned principal would later be minted a session token bound to the default id and gain authorization (identifier={identifier:?})"
			);
		}
		Ok(ident.id)
	}

	#[inline]
	fn record_github_login(&self, identity: IdentityId, login: &str) -> Result<(), Error> {
		let catalog = self.engine.catalog();
		RetryStrategy::default().retry(&self.rng, "auth::record_github_login", || {
			let mut admin = self.engine.begin_admin()?;
			let Some(attribute) = catalog
				.find_identity_attribute_by_name(&mut Transaction::Admin(&mut admin), "github_login")?
			else {
				return Ok(());
			};
			catalog.set_identity_attribute_value(
				&mut admin,
				identity,
				&attribute,
				Value::Utf8(login.to_string()),
			)?;
			admin.commit()?;
			Ok(())
		})
	}

	#[inline]
	fn github_provider(&self) -> Result<&dyn AuthenticationProvider, Error> {
		self.auth_registry.get("github").ok_or_else(|| {
			Error::from(AuthError::UnknownMethod {
				method: "github".to_string(),
			})
		})
	}
}

#[inline]
fn state_matches(challenge: &ChallengeInfo, credentials: &HashMap<String, String>) -> bool {
	match (challenge.payload.get("state"), credentials.get("state")) {
		(Some(expected), Some(provided)) => expected.as_bytes().ct_eq(provided.as_bytes()).into(),
		_ => false,
	}
}

#[inline]
fn not_configured() -> AuthResponse {
	AuthResponse::Failed {
		reason: "github authentication is not configured".to_string(),
	}
}

#[inline]
fn invalid_credentials() -> AuthResponse {
	AuthResponse::Failed {
		reason: "invalid credentials".to_string(),
	}
}