Skip to content

Commit

Permalink
Tolerate missing username
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Feb 9, 2025
1 parent 3191f34 commit 91a0deb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
14 changes: 5 additions & 9 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use url::Url;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
preferred_username: String,
username: Option<String>,
roles: Vec<String>,
}

Expand Down Expand Up @@ -408,11 +408,10 @@ async fn fallible_callback(
)
.context("Failed to validate access token")?;

let preferred_username: String = claims
let username: Option<String> = claims
.preferred_username()
.map(|username| username.as_str())
.context("OIDC server did not provide preferred_username")?
.to_string();
.map(|username| username.to_string())
.or_else(|| claims.email().map(|email| email.to_string()));

let maybe_roles_at: Option<&serde_json::Value> =
value_at_path(&access_token.claims, &state.roles_path);
Expand All @@ -439,10 +438,7 @@ async fn fallible_callback(
maybe_roles_at.and_then(to_string_array).unwrap_or_default()
};

let user: User = User {
preferred_username,
roles,
};
let user: User = User { username, roles };

session
.insert(USER_KEY, user.clone())
Expand Down
4 changes: 2 additions & 2 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ <h1>{{ title }}</h1>
</ul>
{% else %}
<p>No pages to display</p>
{% endif %} {% if let Some(username) = user.username %}
<p>You are signed in as {{ username }}.</p>
{% endif %}

<p>You are signed in as {{ user.preferred_username }}.</p>
<a href="/logout">Logout</a>
{% else %}
<a href="/login">Login to view documents...</a>
Expand Down

0 comments on commit 91a0deb

Please sign in to comment.