Skip to main content

openstack_keystone_core/federation/
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//! # Federation provider
15//!
16//! Federation provider implements the functionality necessary for the user
17//! federation.
18use async_trait::async_trait;
19
20pub mod api;
21pub mod backend;
22pub mod error;
23#[cfg(any(test, feature = "mock"))]
24pub mod mock;
25pub mod service;
26pub mod types;
27
28use crate::config::Config;
29use crate::keystone::ServiceState;
30use crate::plugin_manager::PluginManagerApi;
31use service::FederationService;
32use types::*;
33
34pub use crate::federation::error::FederationProviderError;
35#[cfg(any(test, feature = "mock"))]
36pub use mock::MockFederationProvider;
37pub use types::FederationApi;
38
39pub enum FederationProvider {
40    Service(FederationService),
41    #[cfg(any(test, feature = "mock"))]
42    Mock(MockFederationProvider),
43}
44
45impl FederationProvider {
46    pub fn new<P: PluginManagerApi>(
47        config: &Config,
48        plugin_manager: &P,
49    ) -> Result<Self, FederationProviderError> {
50        Ok(Self::Service(FederationService::new(
51            config,
52            plugin_manager,
53        )?))
54    }
55}
56
57#[async_trait]
58impl FederationApi for FederationProvider {
59    /// Cleanup expired resources.
60    #[tracing::instrument(level = "info", skip(self, state))]
61    async fn cleanup(&self, state: &ServiceState) -> Result<(), FederationProviderError> {
62        match self {
63            Self::Service(provider) => provider.cleanup(state).await,
64            #[cfg(any(test, feature = "mock"))]
65            Self::Mock(provider) => provider.cleanup(state).await,
66        }
67    }
68
69    /// Create new auth state.
70    #[tracing::instrument(level = "debug", skip(self, state))]
71    async fn create_auth_state(
72        &self,
73        state: &ServiceState,
74        auth_state: AuthState,
75    ) -> Result<AuthState, FederationProviderError> {
76        match self {
77            Self::Service(provider) => provider.create_auth_state(state, auth_state).await,
78            #[cfg(any(test, feature = "mock"))]
79            Self::Mock(provider) => provider.create_auth_state(state, auth_state).await,
80        }
81    }
82
83    /// Create Identity provider.
84    #[tracing::instrument(level = "debug", skip(self, state))]
85    async fn create_identity_provider(
86        &self,
87        state: &ServiceState,
88        idp: IdentityProviderCreate,
89    ) -> Result<IdentityProvider, FederationProviderError> {
90        match self {
91            Self::Service(provider) => provider.create_identity_provider(state, idp).await,
92            #[cfg(any(test, feature = "mock"))]
93            Self::Mock(provider) => provider.create_identity_provider(state, idp).await,
94        }
95    }
96
97    /// Create mapping.
98    #[tracing::instrument(level = "debug", skip(self, state))]
99    async fn create_mapping(
100        &self,
101        state: &ServiceState,
102        mapping: Mapping,
103    ) -> Result<Mapping, FederationProviderError> {
104        match self {
105            Self::Service(provider) => provider.create_mapping(state, mapping).await,
106            #[cfg(any(test, feature = "mock"))]
107            Self::Mock(provider) => provider.create_mapping(state, mapping).await,
108        }
109    }
110
111    /// Delete auth state.
112    #[tracing::instrument(level = "debug", skip(self, state))]
113    async fn delete_auth_state<'a>(
114        &self,
115        state: &ServiceState,
116        id: &'a str,
117    ) -> Result<(), FederationProviderError> {
118        match self {
119            Self::Service(provider) => provider.delete_identity_provider(state, id).await,
120            #[cfg(any(test, feature = "mock"))]
121            Self::Mock(provider) => provider.delete_auth_state(state, id).await,
122        }
123    }
124
125    /// Delete identity provider.
126    #[tracing::instrument(level = "debug", skip(self, state))]
127    async fn delete_identity_provider<'a>(
128        &self,
129        state: &ServiceState,
130        id: &'a str,
131    ) -> Result<(), FederationProviderError> {
132        match self {
133            Self::Service(provider) => provider.delete_identity_provider(state, id).await,
134            #[cfg(any(test, feature = "mock"))]
135            Self::Mock(provider) => provider.delete_identity_provider(state, id).await,
136        }
137    }
138
139    /// Delete identity provider.
140    #[tracing::instrument(level = "debug", skip(self, state))]
141    async fn delete_mapping<'a>(
142        &self,
143        state: &ServiceState,
144        id: &'a str,
145    ) -> Result<(), FederationProviderError> {
146        match self {
147            Self::Service(provider) => provider.delete_mapping(state, id).await,
148            #[cfg(any(test, feature = "mock"))]
149            Self::Mock(provider) => provider.delete_mapping(state, id).await,
150        }
151    }
152
153    /// Get auth state by ID.
154    #[tracing::instrument(level = "debug", skip(self, state))]
155    async fn get_auth_state<'a>(
156        &self,
157        state: &ServiceState,
158        id: &'a str,
159    ) -> Result<Option<AuthState>, FederationProviderError> {
160        match self {
161            Self::Service(provider) => provider.get_auth_state(state, id).await,
162            #[cfg(any(test, feature = "mock"))]
163            Self::Mock(provider) => provider.get_auth_state(state, id).await,
164        }
165    }
166
167    /// Get single IDP by ID.
168    #[tracing::instrument(level = "info", skip(self, state))]
169    async fn get_identity_provider<'a>(
170        &self,
171        state: &ServiceState,
172        id: &'a str,
173    ) -> Result<Option<IdentityProvider>, FederationProviderError> {
174        match self {
175            Self::Service(provider) => provider.get_identity_provider(state, id).await,
176            #[cfg(any(test, feature = "mock"))]
177            Self::Mock(provider) => provider.get_identity_provider(state, id).await,
178        }
179    }
180
181    /// Get single mapping by ID.
182    #[tracing::instrument(level = "info", skip(self, state))]
183    async fn get_mapping<'a>(
184        &self,
185        state: &ServiceState,
186        id: &'a str,
187    ) -> Result<Option<Mapping>, FederationProviderError> {
188        match self {
189            Self::Service(provider) => provider.get_mapping(state, id).await,
190            #[cfg(any(test, feature = "mock"))]
191            Self::Mock(provider) => provider.get_mapping(state, id).await,
192        }
193    }
194
195    /// List IDP.
196    #[tracing::instrument(level = "info", skip(self, state))]
197    async fn list_identity_providers(
198        &self,
199        state: &ServiceState,
200        params: &IdentityProviderListParameters,
201    ) -> Result<Vec<IdentityProvider>, FederationProviderError> {
202        match self {
203            Self::Service(provider) => provider.list_identity_providers(state, params).await,
204            #[cfg(any(test, feature = "mock"))]
205            Self::Mock(provider) => provider.list_identity_providers(state, params).await,
206        }
207    }
208
209    /// List mappings.
210    #[tracing::instrument(level = "info", skip(self, state))]
211    async fn list_mappings(
212        &self,
213        state: &ServiceState,
214        params: &MappingListParameters,
215    ) -> Result<Vec<Mapping>, FederationProviderError> {
216        match self {
217            Self::Service(provider) => provider.list_mappings(state, params).await,
218            #[cfg(any(test, feature = "mock"))]
219            Self::Mock(provider) => provider.list_mappings(state, params).await,
220        }
221    }
222
223    /// Update Identity provider.
224    #[tracing::instrument(level = "debug", skip(self, state))]
225    async fn update_identity_provider<'a>(
226        &self,
227        state: &ServiceState,
228        id: &'a str,
229        idp: IdentityProviderUpdate,
230    ) -> Result<IdentityProvider, FederationProviderError> {
231        match self {
232            Self::Service(provider) => provider.update_identity_provider(state, id, idp).await,
233            #[cfg(any(test, feature = "mock"))]
234            Self::Mock(provider) => provider.update_identity_provider(state, id, idp).await,
235        }
236    }
237
238    /// Update mapping
239    #[tracing::instrument(level = "debug", skip(self, state))]
240    async fn update_mapping<'a>(
241        &self,
242        state: &ServiceState,
243        id: &'a str,
244        mapping: MappingUpdate,
245    ) -> Result<Mapping, FederationProviderError> {
246        match self {
247            Self::Service(provider) => provider.update_mapping(state, id, mapping).await,
248            #[cfg(any(test, feature = "mock"))]
249            Self::Mock(provider) => provider.update_mapping(state, id, mapping).await,
250        }
251    }
252}