Skip to main content

openstack_keystone_core/identity_mapping/
mod.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14
15//! # Identity mapping provider
16//!
17//! Identity mapping provider provides a mapping of the entity ID between
18//! Keystone and the remote system (i.e. LDAP, IdP, OpenFGA, SCIM, etc).
19
20use async_trait::async_trait;
21
22pub mod backend;
23pub mod error;
24#[cfg(any(test, feature = "mock"))]
25pub mod mock;
26pub mod service;
27pub mod types;
28
29use crate::config::Config;
30use crate::identity_mapping::service::IdentityMappingService;
31use crate::keystone::ServiceState;
32use crate::plugin_manager::PluginManagerApi;
33use types::*;
34
35pub use error::IdentityMappingProviderError;
36#[cfg(any(test, feature = "mock"))]
37pub use mock::MockIdentityMappingProvider;
38pub use types::IdentityMappingApi;
39
40pub enum IdentityMappingProvider {
41    Service(IdentityMappingService),
42    #[cfg(any(test, feature = "mock"))]
43    Mock(MockIdentityMappingProvider),
44}
45
46impl IdentityMappingProvider {
47    pub fn new<P: PluginManagerApi>(
48        config: &Config,
49        plugin_manager: &P,
50    ) -> Result<Self, IdentityMappingProviderError> {
51        Ok(Self::Service(IdentityMappingService::new(
52            config,
53            plugin_manager,
54        )?))
55    }
56}
57
58#[async_trait]
59impl IdentityMappingApi for IdentityMappingProvider {
60    /// Get the `IdMapping` by the local data.
61    async fn get_by_local_id<'a>(
62        &self,
63        state: &ServiceState,
64        local_id: &'a str,
65        domain_id: &'a str,
66        entity_type: IdMappingEntityType,
67    ) -> Result<Option<IdMapping>, IdentityMappingProviderError> {
68        match self {
69            Self::Service(provider) => {
70                provider
71                    .get_by_local_id(state, local_id, domain_id, entity_type)
72                    .await
73            }
74            #[cfg(any(test, feature = "mock"))]
75            Self::Mock(provider) => {
76                provider
77                    .get_by_local_id(state, local_id, domain_id, entity_type)
78                    .await
79            }
80        }
81    }
82
83    /// Get the IdMapping by the public_id.
84    #[tracing::instrument(level = "info", skip(self, state))]
85    async fn get_by_public_id<'a>(
86        &self,
87        state: &ServiceState,
88        public_id: &'a str,
89    ) -> Result<Option<IdMapping>, IdentityMappingProviderError> {
90        match self {
91            Self::Service(provider) => provider.get_by_public_id(state, public_id).await,
92            #[cfg(any(test, feature = "mock"))]
93            Self::Mock(provider) => provider.get_by_public_id(state, public_id).await,
94        }
95    }
96}