Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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:
- `<app_url>/v1/account/oidc/authenticate/callback`
Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion config.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Provider name that clients show on their login button, e.g. `Authelia`.
#[serde(default)]
pub display_name: Option<String>,
}

/// Accepts a list, or a comma-separated string so that lists can be set
Expand Down Expand Up @@ -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(),
Expand All @@ -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());
}
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}
28 changes: 28 additions & 0 deletions src/handlers/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -42,6 +43,25 @@ async fn health_state(privacy_policy_url: web::Data<Option<String>>) -> 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())] {
Expand Down Expand Up @@ -69,6 +89,13 @@ mod tests {
}
}

pub(crate) fn oidc_display_name(oidc: Option<&OidcConfig>) -> Option<String> {
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<impl Responder> {
Expand All @@ -79,6 +106,7 @@ async fn server_metainfo() -> HandlerResult<impl Responder> {
version: env!("CARGO_PKG_VERSION").into(),
capabilities: MetaResponseCapabilities {
oidc: CONFIG.oidc.is_some(),
oidc_display_name: oidc_display_name(CONFIG.oidc.as_ref()),
},
}))
}
17 changes: 9 additions & 8 deletions src/route_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading