@@ -5,16 +5,17 @@ use std::{
55 ops:: Deref ,
66} ;
77
8+ use reqwest:: Url ;
89use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
910
1011#[ derive( Debug , Serialize , Deserialize , Clone ) ]
1112#[ serde( rename_all = "lowercase" , tag = "type" ) ]
1213pub 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 ) ]
5758pub 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 ) ]
9697pub struct SSO ( HashSet < IdProvider > ) ;
9798
9899impl 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+ }
0 commit comments