Add authentication from upstream providers (#107)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Let BasicOIDC delegate authentication to upstream providers (Google, GitHub, GitLab, Keycloak...) Reviewed-on: #107
This commit is contained in:
89
src/data/provider.rs
Normal file
89
src/data/provider.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use crate::data::entity_manager::EntityManager;
|
||||
use crate::data::login_redirect::LoginRedirect;
|
||||
use crate::utils::string_utils::apply_env_vars;
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Eq, PartialEq)]
|
||||
pub struct ProviderID(pub String);
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Provider {
|
||||
/// The ID of the provider
|
||||
pub id: ProviderID,
|
||||
|
||||
/// The human-readable name of the client
|
||||
pub name: String,
|
||||
|
||||
/// A logo presented to the users of the provider
|
||||
pub logo: String,
|
||||
|
||||
/// The registration id of BasicOIDC on the provider
|
||||
pub client_id: String,
|
||||
|
||||
/// The registration secret of BasicOIDC on the provider
|
||||
pub client_secret: String,
|
||||
|
||||
/// Specify the URL of the OpenID configuration URL
|
||||
///
|
||||
/// (.well-known/openid-configuration endpoint)
|
||||
pub configuration_url: String,
|
||||
}
|
||||
|
||||
impl Provider {
|
||||
/// Get URL-encoded provider id
|
||||
pub fn id_encoded(&self) -> String {
|
||||
urlencoding::encode(&self.id.0).to_string()
|
||||
}
|
||||
|
||||
/// Get the URL where the logo can be located
|
||||
pub fn logo_url(&self) -> &str {
|
||||
match self.logo.as_str() {
|
||||
"gitea" => "/assets/img/brands/gitea.svg",
|
||||
"gitlab" => "/assets/img/brands/gitlab.svg",
|
||||
"github" => "/assets/img/brands/github.svg",
|
||||
"microsoft" => "/assets/img/brands/microsoft.svg",
|
||||
"google" => "/assets/img/brands/google.svg",
|
||||
s => s,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the URL to use to login with the provider
|
||||
pub fn login_url(&self, redirect_url: &LoginRedirect) -> String {
|
||||
format!(
|
||||
"/login_with_prov?id={}&redirect={}",
|
||||
self.id_encoded(),
|
||||
redirect_url.get_encoded()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Provider {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id.eq(&other.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Provider {}
|
||||
|
||||
pub type ProvidersManager = EntityManager<Provider>;
|
||||
|
||||
impl EntityManager<Provider> {
|
||||
pub fn find_by_id(&self, u: &ProviderID) -> Option<Provider> {
|
||||
for entry in self.iter() {
|
||||
if entry.id.eq(u) {
|
||||
return Some(entry.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn apply_environment_variables(&mut self) {
|
||||
for c in self.iter_mut() {
|
||||
c.id = ProviderID(apply_env_vars(&c.id.0));
|
||||
c.name = apply_env_vars(&c.name);
|
||||
c.logo = apply_env_vars(&c.logo);
|
||||
c.client_id = apply_env_vars(&c.client_id);
|
||||
c.client_secret = apply_env_vars(&c.client_secret);
|
||||
c.configuration_url = apply_env_vars(&c.configuration_url);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user