Skip to content

Commit 22b7f92

Browse files
committed
remove ID provider model & make configuration deserializable
1 parent d383af1 commit 22b7f92

File tree

6 files changed

+48
-85
lines changed

6 files changed

+48
-85
lines changed

crates/authifier/src/config/sso.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ pub enum Claim {
5353
Email,
5454
}
5555

56-
#[derive(Serialize, Deserialize, Clone)]
56+
#[derive(Clone)]
5757
pub struct IdProvider {
5858
pub id: String,
5959

60-
pub issuer: String,
60+
pub issuer: reqwest::Url,
6161
pub name: Option<String>,
62-
pub icon: Option<String>,
62+
pub icon: Option<reqwest::Url>,
6363

6464
pub scopes: Vec<String>,
6565
pub endpoints: Endpoints,
@@ -105,11 +105,41 @@ impl Serialize for SSO {
105105
}
106106

107107
impl<'de> Deserialize<'de> for SSO {
108-
fn deserialize<D>(_: D) -> Result<Self, D::Error>
108+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
109109
where
110110
D: Deserializer<'de>,
111111
{
112-
todo!()
112+
#[derive(Deserialize)]
113+
pub struct Mock {
114+
pub issuer: reqwest::Url,
115+
pub name: Option<String>,
116+
pub icon: Option<reqwest::Url>,
117+
118+
pub scopes: Vec<String>,
119+
pub endpoints: Endpoints,
120+
pub credentials: Credentials,
121+
pub claims: HashMap<Claim, String>,
122+
123+
pub code_challenge: bool,
124+
}
125+
126+
let map: HashMap<String, Mock> =
127+
HashMap::deserialize(deserializer).map_err(serde::de::Error::custom)?;
128+
129+
Ok(SSO(map
130+
.into_iter()
131+
.map(|(id, mock)| IdProvider {
132+
id,
133+
issuer: mock.issuer,
134+
name: mock.name,
135+
icon: mock.icon,
136+
scopes: mock.scopes,
137+
endpoints: mock.endpoints,
138+
credentials: mock.credentials,
139+
claims: mock.claims,
140+
code_challenge: mock.code_challenge,
141+
})
142+
.collect()))
113143
}
114144
}
115145

crates/authifier/src/impl/id_provider.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use serde::Serialize;
1919
use sha2::{Digest, Sha256};
2020

2121
use crate::{
22-
config::{Credentials, Endpoints},
23-
models::{Callback, IdProvider},
22+
config::{Credentials, Endpoints, IdProvider},
23+
models::Callback,
2424
util::secure_random_str,
2525
Authifier, Error, Result,
2626
};

crates/authifier/src/models/id_provider.rs

Lines changed: 0 additions & 66 deletions
This file was deleted.

crates/authifier/src/models/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod account;
22
mod callback;
3-
mod id_provider;
43
mod invite;
54
mod mfa;
65
mod secret;
@@ -9,7 +8,6 @@ mod ticket;
98

109
pub use account::*;
1110
pub use callback::*;
12-
pub use id_provider::*;
1311
pub use invite::*;
1412
pub use mfa::*;
1513
pub use secret::*;

crates/rocket_authifier/src/routes/sso/authorize.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Redirect to authorization interface
22
//! GET /sso/authorize
3-
use authifier::models::IdProvider;
43
use authifier::{Authifier, Error, Result};
54
use rocket::http::{Cookie, CookieJar};
65
use rocket::response::Redirect;
@@ -24,10 +23,11 @@ pub async fn authorize(
2423
};
2524

2625
// Ensure given ID provider exists
27-
let id_provider = match authifier.config.sso.get(idp_id).cloned() {
28-
Some(config) => IdProvider::try_from(config).map_err(|_| Error::InvalidIdpConfig)?,
29-
None => return Err(Error::InvalidIdpId),
30-
};
26+
let id_provider = authifier
27+
.config
28+
.sso
29+
.get(idp_id)
30+
.ok_or(Error::InvalidIdpId)?;
3131

3232
// Build authorization URI
3333
let (state, uri) = id_provider

crates/rocket_authifier/src/routes/sso/callback.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::collections::HashMap;
44

55
use authifier::config::Claim;
6-
use authifier::models::{Account, IdProvider};
6+
use authifier::models::Account;
77
use authifier::util::{normalise_email, secure_random_str};
88
use authifier::{Authifier, Error, Result};
99
use iso8601_timestamp::Timestamp;
@@ -65,10 +65,11 @@ pub async fn callback(
6565
}
6666

6767
// Ensure given ID provider exists
68-
let id_provider = match authifier.config.sso.get(&*callback.idp_id).cloned() {
69-
Some(config) => IdProvider::try_from(config).map_err(|_| Error::InvalidIdpConfig)?,
70-
None => return Err(Error::InvalidIdpId),
71-
};
68+
let id_provider = authifier
69+
.config
70+
.sso
71+
.get(&*callback.idp_id)
72+
.ok_or(Error::InvalidIdpId)?;
7273

7374
// Ensure authorization code was provided
7475
let Some(code) = data.code.as_deref() else {

0 commit comments

Comments
 (0)