diff --git a/README.md b/README.md index 99dfc7e..d15777c 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ The server never migrates an existing database on its own. To upgrade: Add an `[oidc]` section to `config.toml`, or set the matching `OIDC__*` environment variables, to let users sign in through an OpenID Connect provider. -All options except `allowed_redirect_urls` are required. As an environment +All options except `allowed_redirect_urls` and `display_name` are required. As an environment variable, `allowed_redirect_urls` takes a comma-separated list, e.g. `OIDC__ALLOWED_REDIRECT_URLS=myapp://oidc,https://app.example.com/oidc`. With the Compose quick start, add the variables to the service's `environment` @@ -172,7 +172,8 @@ list. Compose doesn't pass entries in `.env` to the container on its own. | `client_id` | Client ID of the OAuth app | | | `client_secret` | Client secret of the OAuth app | | | `app_url` | Public URL of this server | `https://sync.example.com` | -| `allowed_redirect_urls` | Client URLs that login and deletion may redirect back to | `["myapp://oidc"]` | +| `allowed_redirect_urls` | Client URLs that login and deletion may redirect back to | `["opentubex://oidc"]` | +| `display_name` | Provider name that clients show on their login button | `Authelia` | - Allow these redirect URLs in the OAuth app: - `/v1/account/oidc/authenticate/callback` @@ -182,6 +183,9 @@ list. Compose doesn't pass entries in `.env` to the container on its own. login and deletion while the list is empty. - With `allow_registration = false`, users cannot sign in with OIDC for the first time. +- The OpenTubeX desktop and mobile apps return to `opentubex://oidc`, and its + iOS nightly builds to `opentubex-nightly://oidc`. They show the login button + when `GET /meta` reports `"oidc": true`, labeled with `display_name` if set. ### External services diff --git a/config.dev.toml b/config.dev.toml index c5e63f3..9248256 100644 --- a/config.dev.toml +++ b/config.dev.toml @@ -20,4 +20,7 @@ client_id = "opentubex-dev" client_secret = "opentubex-dev" app_url = "http://localhost:8080" # Client URLs that OIDC login and deletion may redirect back to. -# allowed_redirect_urls = ["myapp://oidc"] +# OpenTubeX desktop and mobile apps return to opentubex://oidc. +# allowed_redirect_urls = ["opentubex://oidc"] +# Provider name that clients show on their login button. +# display_name = "Authelia" diff --git a/src/config.rs b/src/config.rs index 5cf5abd..944b34c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -34,6 +34,9 @@ pub struct OidcConfig { /// token to this URL, so only list clients you trust. #[serde(default, deserialize_with = "string_list")] pub allowed_redirect_urls: Vec, + /// Provider name that clients show on their login button, e.g. `Authelia`. + #[serde(default)] + pub display_name: Option, } /// Accepts a list, or a comma-separated string so that lists can be set @@ -335,6 +338,7 @@ mod tests { client_secret: String::new(), app_url: String::new(), allowed_redirect_urls: Vec::new(), + display_name: None, }); assert_eq!( config.validate(), @@ -358,6 +362,7 @@ mod tests { client_secret: STRONG.to_owned(), app_url: "https://sync.example.com".to_owned(), allowed_redirect_urls: Vec::new(), + display_name: None, }); assert!(config.validate().is_ok()); } @@ -392,6 +397,7 @@ mod tests { "OIDC__ALLOWED_REDIRECT_URLS", "myapp://oidc, https://app.example.com/oidc", ), + ("OIDC__DISPLAY_NAME", "Authelia"), ]); assert_eq!(config.secret, STRONG); @@ -407,6 +413,7 @@ mod tests { oidc.allowed_redirect_urls, ["myapp://oidc", "https://app.example.com/oidc"] ); + assert_eq!(oidc.display_name.as_deref(), Some("Authelia")); } /// Variables override single options of a `config.toml` section, and the diff --git a/src/dto.rs b/src/dto.rs index 673bab4..28b9852 100644 --- a/src/dto.rs +++ b/src/dto.rs @@ -346,4 +346,7 @@ pub struct MetaResponseApi { #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct MetaResponseCapabilities { pub oidc: bool, + /// The configured `oidc.display_name`, for clients to label their login button. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub oidc_display_name: Option, } diff --git a/src/handlers/health.rs b/src/handlers/health.rs index 3a678b5..07c1fbb 100644 --- a/src/handlers/health.rs +++ b/src/handlers/health.rs @@ -3,6 +3,7 @@ use utoipa_actix_web::scope; use crate::{ CONFIG, + config::OidcConfig, dto::{HealthResponse, MetaResponse, MetaResponseApi, MetaResponseCapabilities}, handlers::{HandlerResult, ScopedHandler, encrypted_sync::sync_capabilities}, }; @@ -42,6 +43,25 @@ async fn health_state(privacy_policy_url: web::Data>) -> impl Res mod tests { use super::*; + #[test] + fn oidc_display_name_is_trimmed_and_omitted_when_blank() { + let config = |display_name: Option<&str>| OidcConfig { + provider_url: "https://auth.example.com".to_owned(), + client_id: "sync-server".to_owned(), + client_secret: "secret".to_owned(), + app_url: "https://sync.example.com".to_owned(), + allowed_redirect_urls: Vec::new(), + display_name: display_name.map(str::to_owned), + }; + assert_eq!(oidc_display_name(None), None); + assert_eq!(oidc_display_name(Some(&config(None))), None); + assert_eq!(oidc_display_name(Some(&config(Some(" ")))), None); + assert_eq!( + oidc_display_name(Some(&config(Some(" Authelia ")))), + Some("Authelia".to_owned()) + ); + } + #[actix_web::test] async fn health_routes_advertise_only_the_configured_privacy_policy() { for policy in [None, Some("https://operator.example/privacy".to_owned())] { @@ -69,6 +89,13 @@ mod tests { } } +pub(crate) fn oidc_display_name(oidc: Option<&OidcConfig>) -> Option { + oidc.and_then(|oidc| oidc.display_name.as_deref()) + .map(str::trim) + .filter(|name| !name.is_empty()) + .map(str::to_owned) +} + #[utoipa::path(responses((status = OK, body = MetaResponse)))] #[get("/meta")] async fn server_metainfo() -> HandlerResult { @@ -79,6 +106,7 @@ async fn server_metainfo() -> HandlerResult { version: env!("CARGO_PKG_VERSION").into(), capabilities: MetaResponseCapabilities { oidc: CONFIG.oidc.is_some(), + oidc_display_name: oidc_display_name(CONFIG.oidc.as_ref()), }, })) } diff --git a/src/route_tests.rs b/src/route_tests.rs index 8ae535b..23b9107 100644 --- a/src/route_tests.rs +++ b/src/route_tests.rs @@ -748,14 +748,15 @@ async fn metadata_is_public_and_reports_version_and_oidc_configuration() { let response = test::call_service(&app, request).await; assert_eq!(response.status(), StatusCode::OK); let body: Value = test::read_body_json(response).await; - assert_eq!( - body, - json!({ - "api": { "libretube-sync": env!("CARGO_PKG_VERSION") }, - "version": env!("CARGO_PKG_VERSION"), - "oidc": CONFIG.oidc.is_some(), - }) - ); + let mut expected = json!({ + "api": { "libretube-sync": env!("CARGO_PKG_VERSION") }, + "version": env!("CARGO_PKG_VERSION"), + "oidc": CONFIG.oidc.is_some(), + }); + if let Some(name) = crate::handlers::health::oidc_display_name(CONFIG.oidc.as_ref()) { + expected["oidc_display_name"] = json!(name); + } + assert_eq!(body, expected); } #[actix_web::test]