picodata_plugin/
authentication.rs

1//! Module for authentication related entities.
2
3use crate::internal::ffi::pico_ffi_authenticate;
4
5use tarantool::error::BoxError;
6
7/// # Description
8///
9/// Tries to authenticate a user with specified password.
10/// Authentication method is determined via accessing `_pico_user`
11/// system table using `admin` session.
12///
13/// # FFI
14///
15/// Uses [`pico_ffi_authenticate`].
16///
17/// # Errors
18///
19/// - User was not found in the list of available users.
20/// - Authentication method was not initialized for the user.
21/// - Username length is greater than `u32`.
22/// - Password is not correct for the specified user.
23///
24/// # Panics
25///
26/// - Global Raft node is not initialized.
27/// - Authentication data is not set for the specified user.
28/// - Session of `admin` user is closed.
29/// - User `admin` is not found.
30/// - User `admin` does not have enough permissions.
31/// - Internal error on accessing underlying Tarantool space of `_pico_user` system table.
32pub fn authenticate(username: &str, password: impl AsRef<[u8]>) -> Result<(), BoxError> {
33    match unsafe { pico_ffi_authenticate(username.into(), password.as_ref().into()) } {
34        0 => Ok(()),
35        _ => {
36            let error = BoxError::last();
37            Err(error)
38        }
39    }
40}