update identity detection methods

This commit is contained in:
yggverse 2025-01-23 13:28:18 +02:00
parent 9a7984f345
commit d7f6e2f16b
8 changed files with 47 additions and 41 deletions

View file

@ -126,8 +126,8 @@ impl Identity {
/// Get `Identity` match `request`
/// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates)
/// * this function work with memory cache (not database)
pub fn match_scope(&self, request: &str) -> Option<Item> {
if let Some(auth) = self.auth.memory.match_scope(request) {
pub fn get(&self, request: &str) -> Option<Item> {
if let Some(auth) = self.auth.get(request) {
match self.memory.get(auth.profile_identity_id) {
Ok(pem) => {
return Some(Item {

View file

@ -41,14 +41,15 @@ impl Auth {
/// * deactivate active auth by remove previous records from `Self` database
/// * reindex `Self` memory index on success
/// * return last insert `profile_identity_auth_id` on success
pub fn apply(&self, profile_identity_id: i64, auth_url: &str) -> Result<i64, Error> {
let scope = filter_scope(auth_url);
pub fn apply(&self, profile_identity_id: i64, request: &str) -> Result<i64, Error> {
// Cleanup records match `scope` (unauthorize)
self.remove_scope(&scope)?;
self.remove(request)?;
// Create new record (auth)
let profile_identity_auth_id = match self.database.add(profile_identity_id, &scope) {
let profile_identity_auth_id = match self
.database
.add(profile_identity_id, &filter_scope(request))
{
Ok(id) => id,
Err(e) => return Err(Error::Database(e)),
};
@ -61,8 +62,8 @@ impl Auth {
}
/// Remove all records match request (unauthorize)
pub fn remove_scope(&self, scope: &str) -> Result<(), Error> {
match self.database.records_scope(Some(scope)) {
pub fn remove(&self, request: &str) -> Result<(), Error> {
match self.database.records_scope(Some(&filter_scope(request))) {
Ok(records) => {
for record in records {
if let Err(e) = self.database.delete(record.id) {
@ -113,6 +114,20 @@ impl Auth {
Ok(())
}
// Getters
/// Check request string matches condition
pub fn is_matches(&self, request: &str, profile_identity_id: i64) -> bool {
self.memory
.match_scope(&filter_scope(request))
.is_some_and(|auth| auth.profile_identity_id == profile_identity_id)
}
/// Get memory item string match request
pub fn get(&self, request: &str) -> Option<memory::Auth> {
self.memory.match_scope(&filter_scope(request))
}
}
// Tools

View file

@ -58,20 +58,17 @@ impl Memory {
}
}
/// Get identity match `request`
/// Get identity exactly match `scope`
/// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates)
/// * contain unspecified length priority implementation @TODO
pub fn match_scope(&self, request: &str) -> Option<Auth> {
/// * see also parent `is_match_request`
pub fn match_scope(&self, scope: &str) -> Option<Auth> {
let mut result = Vec::new();
// Get all records starts with `scope`
let query = super::filter_scope(request);
for (scope, &profile_identity_id) in self.index.borrow().iter() {
if query.starts_with(scope) {
for (value, &profile_identity_id) in self.index.borrow().iter() {
if scope.starts_with(value) {
result.push(Auth {
profile_identity_id,
scope: scope.clone(),
scope: value.clone(),
})
}
}