@@ -2,7 +2,7 @@ use crate::auth::Auth;
22use crate :: config:: AppState ;
33use crate :: db;
44use crate :: errors:: { Errors , FieldValidator } ;
5- use crate :: util:: { handle_login, Ticket } ;
5+ use crate :: util:: { handle_login, ugkthid_to_user , Ticket } ;
66use anyhow:: { anyhow, Result } ;
77use openidconnect:: core:: {
88 CoreAuthDisplay , CoreAuthPrompt , CoreAuthenticationFlow , CoreErrorResponseType ,
@@ -133,24 +133,38 @@ pub fn kth_login(cookies: Cookies) -> Redirect {
133133#[ derive( FromForm , Default ) ]
134134pub struct Code {
135135 code : Option < String > ,
136+ #[ allow( dead_code) ]
136137 state : Option < String > ,
137138}
138139
139140#[ get( "/oidc-auth?<params..>" ) ]
140141pub fn kth_oidc_auth (
141- cookies : Cookies ,
142- _conn : db:: DbConn ,
143- _state : State < AppState > ,
142+ mut cookies : Cookies ,
143+ conn : db:: DbConn ,
144+ state : State < AppState > ,
144145 params : Form < Code > ,
145- // client_addr: &ClientAddr,
146+ client_addr : & ClientAddr ,
146147) -> Redirect {
147148 println ! ( "starting oidc auth" ) ;
148149 // cookies.add(Cookie::new("nonce", nonce.secret().clone()));
149150 match cookies. get ( "nonce" ) {
150151 Some ( nonce) => {
151152 println ! ( "got nonce: {}" , nonce. value( ) ) ;
152153 match get_oidc_user ( params, Nonce :: new ( nonce. value ( ) . to_string ( ) ) ) {
153- Ok ( _) => println ! ( "good login!" ) ,
154+ Ok ( ugkthid) => {
155+ println ! ( "good login!" ) ;
156+ match ugkthid_to_user ( & conn, ugkthid) {
157+ Some ( user) => {
158+ println ! ( "User logged in: {:?}" , user) ;
159+ cookies. add ( Cookie :: new (
160+ "userdata" ,
161+ json ! ( user. to_user_auth( & conn, & state. secret, client_addr) )
162+ . to_string ( ) ,
163+ ) ) ;
164+ }
165+ None => println ! ( "Login failed for some reason..." ) ,
166+ }
167+ }
154168 Err ( err) => {
155169 println ! ( "oidc error: {:?}" , err) ;
156170 }
@@ -223,7 +237,7 @@ pub fn use_oidc(mut cookies: Cookies) -> Result<Redirect> {
223237 Ok ( Redirect :: to ( auth_url. to_string ( ) ) )
224238}
225239
226- pub fn get_oidc_user ( params : Form < Code > , nonce : Nonce ) -> Result < ( ) > {
240+ pub fn get_oidc_user ( params : Form < Code > , nonce : Nonce ) -> Result < String > {
227241 // println!("got nonce: {:?}", nonce.secret());
228242 let client = get_client ( ) ?;
229243 let code = params
@@ -260,14 +274,5 @@ pub fn get_oidc_user(params: Form<Code>, nonce: Nonce) -> Result<()> {
260274 // The authenticated user's identity is now available. See the IdTokenClaims struct for a
261275 // complete listing of the available claims.
262276 println ! ( "Got kthid: {:?}" , claims. additional_claims( ) . kthid) ;
263- println ! (
264- "User {} with e-mail address {} has authenticated successfully" ,
265- claims. subject( ) . as_str( ) ,
266- claims
267- . email( )
268- . map( |email| email. as_str( ) )
269- . unwrap_or( "<not provided>" ) ,
270- ) ;
271-
272- Ok ( ( ) )
277+ Ok ( claims. additional_claims ( ) . kthid . clone ( ) )
273278}
0 commit comments