Skip to main content

soma_gateway/gateway/manager/
core.rs

1use crate::config::{GatewayConfig, UpstreamConfig};
2
3use super::{GatewayManager, GatewayManagerError};
4
5impl GatewayManager {
6    pub fn reload(&self, next: GatewayConfig) -> Result<(), GatewayManagerError> {
7        self.reload_config(next)
8    }
9
10    pub fn upstream_config(&self, name: &str) -> Option<UpstreamConfig> {
11        self.config
12            .read()
13            .expect("gateway config poisoned")
14            .upstream
15            .iter()
16            .find(|upstream| upstream.name == name)
17            .cloned()
18    }
19
20    #[cfg(feature = "oauth")]
21    pub async fn upstream_oauth_access_token(
22        &self,
23        upstream: &UpstreamConfig,
24        subject: &str,
25    ) -> Result<Option<String>, GatewayManagerError> {
26        if upstream.oauth.is_none() {
27            return Ok(None);
28        }
29        let runtime = self
30            .oauth_runtime
31            .read()
32            .expect("gateway oauth runtime poisoned")
33            .clone()
34            .ok_or_else(|| {
35                GatewayManagerError::OAuth(format!(
36                    "upstream `{}` is not connected with OAuth",
37                    upstream.name
38                ))
39            })?;
40        runtime
41            .manager(&upstream.name)
42            .ok_or_else(|| {
43                GatewayManagerError::OAuth(format!(
44                    "upstream `{}` has no OAuth manager",
45                    upstream.name
46                ))
47            })?
48            .access_token(subject)
49            .await
50            .map(Some)
51            .map_err(|error| GatewayManagerError::OAuth(error.to_string()))
52    }
53}
54
55#[cfg(test)]
56#[path = "core_tests.rs"]
57mod tests;