Skip to content

Commit 1dfd89d

Browse files
committed
test configuration and small fixes
1 parent 22b7f92 commit 1dfd89d

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

crates/authifier/src/config/sso.rs

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ use std::{
55
ops::Deref,
66
};
77

8+
use reqwest::Url;
89
use serde::{Deserialize, Deserializer, Serialize, Serializer};
910

1011
#[derive(Debug, Serialize, Deserialize, Clone)]
1112
#[serde(rename_all = "lowercase", tag = "type")]
1213
pub enum Endpoints {
1314
Discoverable,
1415
Manual {
15-
authorization: String,
16-
token: String,
17-
userinfo: String,
16+
authorization: Url,
17+
token: Url,
18+
userinfo: Url,
1819
},
1920
}
2021

@@ -53,13 +54,13 @@ pub enum Claim {
5354
Email,
5455
}
5556

56-
#[derive(Clone)]
57+
#[derive(Clone, Debug)]
5758
pub struct IdProvider {
5859
pub id: String,
5960

60-
pub issuer: reqwest::Url,
61+
pub issuer: Url,
6162
pub name: Option<String>,
62-
pub icon: Option<reqwest::Url>,
63+
pub icon: Option<Url>,
6364

6465
pub scopes: Vec<String>,
6566
pub endpoints: Endpoints,
@@ -92,7 +93,7 @@ impl Hash for IdProvider {
9293
}
9394
}
9495

95-
#[derive(Default, Clone)]
96+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
9697
pub struct SSO(HashSet<IdProvider>);
9798

9899
impl Serialize for SSO {
@@ -111,9 +112,9 @@ impl<'de> Deserialize<'de> for SSO {
111112
{
112113
#[derive(Deserialize)]
113114
pub struct Mock {
114-
pub issuer: reqwest::Url,
115+
pub issuer: Url,
115116
pub name: Option<String>,
116-
pub icon: Option<reqwest::Url>,
117+
pub icon: Option<Url>,
117118

118119
pub scopes: Vec<String>,
119120
pub endpoints: Endpoints,
@@ -150,3 +151,67 @@ impl Deref for SSO {
150151
&self.0
151152
}
152153
}
154+
155+
#[cfg(test)]
156+
mod tests {
157+
use super::*;
158+
159+
#[test]
160+
fn deserialize_sso_config() {
161+
let value = serde_json::json!(
162+
{
163+
"Gitlab": {
164+
"issuer": "https://gitlab.com",
165+
"scopes": ["openid"],
166+
167+
"endpoints": {
168+
"type": "discoverable"
169+
},
170+
"credentials": {
171+
"type": "post",
172+
"client_id": "foobar",
173+
"client_secret": "baz"
174+
},
175+
"claims": {
176+
"id": "sub",
177+
"email": "preferred_email"
178+
},
179+
180+
"code_challenge": false,
181+
}
182+
}
183+
);
184+
185+
let result: SSO = serde_json::from_value(value).expect("config deserializes successfully");
186+
187+
assert_eq!(
188+
result,
189+
SSO([IdProvider {
190+
id: "Gitlab".to_owned(),
191+
192+
issuer: "https://gitlab.com"
193+
.parse()
194+
.expect("issuer should be valid"),
195+
name: None,
196+
icon: None,
197+
198+
scopes: vec!["openid".to_owned()],
199+
endpoints: Endpoints::Discoverable,
200+
credentials: Credentials::Post {
201+
client_id: "foobar".to_owned(),
202+
client_secret: "baz".to_owned(),
203+
},
204+
claims: [
205+
(Claim::Id, "sub".to_owned()),
206+
(Claim::Email, "preferred_email".to_owned())
207+
]
208+
.into_iter()
209+
.collect(),
210+
211+
code_challenge: false,
212+
}]
213+
.into_iter()
214+
.collect())
215+
);
216+
}
217+
}

crates/authifier/src/impl/id_provider.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl IdProvider {
6161

6262
metadata.authorization_endpoint().to_owned()
6363
}
64-
Endpoints::Manual { authorization, .. } => authorization.parse().unwrap(),
64+
Endpoints::Manual { authorization, .. } => authorization.clone(),
6565
};
6666

6767
// Append the client ID, redirect URI and state to the authorization URI
@@ -129,7 +129,7 @@ impl IdProvider {
129129

130130
metadata.token_endpoint().to_owned()
131131
}
132-
Endpoints::Manual { token, .. } => token.parse().unwrap(),
132+
Endpoints::Manual { token, .. } => token.clone(),
133133
};
134134

135135
// Build request for access token with authorization code
@@ -175,7 +175,7 @@ impl IdProvider {
175175

176176
metadata.userinfo_endpoint.as_ref().cloned()
177177
}
178-
Endpoints::Manual { userinfo, .. } => Some(userinfo.parse().unwrap()),
178+
Endpoints::Manual { userinfo, .. } => Some(userinfo.clone()),
179179
}) else {
180180
return Ok(None);
181181
};

0 commit comments

Comments
 (0)