Skip to main content

haste_server/auth_n/oidc/hardcoded_clients/
admin_app.rs

1use crate::config::ServerConfig;
2use haste_fhir_model::r4::generated::{
3    resources::ClientApplication,
4    terminology::{ClientapplicationGrantType, ClientapplicationResponseTypes},
5    types::FHIRString,
6};
7use haste_jwt::{ProjectId, TenantId};
8use haste_repository::utilities::HOSTNAME_ID_SEPARATOR;
9
10pub fn get_admin_app(config: &ServerConfig) -> Option<ClientApplication> {
11    let redirect_uri = &config.admin_app_redirect_uri;
12
13    Some(ClientApplication {
14        id: Some("admin-app".to_string()),
15        name: Box::new(FHIRString {
16            value: Some("Admin Application".to_string()),
17            ..Default::default()
18        }),
19        responseTypes: ClientapplicationResponseTypes::code(),
20        scope: Some(Box::new(FHIRString {
21            value: Some("offline_access openid email profile fhirUser system/*.*".to_string()),
22            ..Default::default()
23        })),
24        grantType: vec![
25            ClientapplicationGrantType::authorization_code(),
26            ClientapplicationGrantType::refresh_token(),
27        ],
28        redirectUri: Some(vec![FHIRString {
29            value: Some(redirect_uri.clone()),
30            ..Default::default()
31        }]),
32        ..Default::default()
33    })
34}
35
36/// The admin app URL for a project: the redirect uri with `*` replaced by the
37/// `{tenant}--{project}` subdomain.
38pub fn redirect_url(
39    config: &ServerConfig,
40    tenant_id: &TenantId,
41    project_id: &ProjectId,
42) -> Option<String> {
43    let subdomain = format!(
44        "{}{HOSTNAME_ID_SEPARATOR}{}",
45        tenant_id.as_ref(),
46        project_id.as_ref()
47    );
48
49    get_admin_app(config)?
50        .redirectUri?
51        .first()?
52        .value
53        .as_ref()
54        .map(|uri| uri.replace('*', &subdomain))
55}