@@ -5,12 +5,17 @@ use crate::errors::{Errors, FieldValidator};
55use crate :: util:: { handle_login, Ticket } ;
66use anyhow:: { anyhow, Result } ;
77use openidconnect:: core:: {
8- CoreAuthenticationFlow , CoreClient , CoreProviderMetadata , CoreUserInfoClaims ,
8+ CoreAuthDisplay , CoreAuthPrompt , CoreAuthenticationFlow , CoreErrorResponseType ,
9+ CoreGenderClaim , CoreJsonWebKey , CoreJsonWebKeyType , CoreJsonWebKeyUse ,
10+ CoreJweContentEncryptionAlgorithm , CoreJwsSigningAlgorithm , CoreProviderMetadata ,
11+ CoreRevocableToken , CoreRevocationErrorResponse , CoreTokenIntrospectionResponse , CoreTokenType ,
912} ;
13+ use serde:: Serialize ;
1014
1115use openidconnect:: {
12- AccessTokenHash , AuthorizationCode , ClientId , ClientSecret , CsrfToken , IssuerUrl , Nonce ,
13- PkceCodeChallenge , RedirectUrl , Scope ,
16+ AccessTokenHash , AdditionalClaims , AuthorizationCode , Client , ClientId , ClientSecret ,
17+ CsrfToken , EmptyExtraTokenFields , IdTokenFields , IssuerUrl , Nonce , RedirectUrl , Scope ,
18+ StandardErrorResponse , StandardTokenResponse ,
1419} ;
1520use rocket:: http:: { Cookie , Cookies } ;
1621use rocket:: request:: Form ;
@@ -22,14 +27,49 @@ use serde::Deserialize;
2227use std:: env;
2328
2429use openidconnect:: reqwest:: http_client;
25-
2630use openidconnect:: { OAuth2TokenResponse , TokenResponse } ;
2731
2832#[ derive( Deserialize ) ]
2933pub struct LoginUser {
3034 user : LoginUserData ,
3135}
3236
37+ #[ derive( Clone , Debug , Default , Deserialize , PartialEq , Serialize ) ]
38+ pub struct KthAdditionalClaims {
39+ kthid : std:: string:: String ,
40+ }
41+ impl AdditionalClaims for KthAdditionalClaims { }
42+
43+ pub type KthIdTokenFields = IdTokenFields <
44+ KthAdditionalClaims ,
45+ EmptyExtraTokenFields ,
46+ CoreGenderClaim ,
47+ CoreJweContentEncryptionAlgorithm ,
48+ CoreJwsSigningAlgorithm ,
49+ CoreJsonWebKeyType ,
50+ > ;
51+
52+ pub type KthTokenResponse = StandardTokenResponse < KthIdTokenFields , CoreTokenType > ;
53+
54+ // EmptyAdditionalClaims,
55+ type KthClient = Client <
56+ KthAdditionalClaims ,
57+ CoreAuthDisplay ,
58+ CoreGenderClaim ,
59+ CoreJweContentEncryptionAlgorithm ,
60+ CoreJwsSigningAlgorithm ,
61+ CoreJsonWebKeyType ,
62+ CoreJsonWebKeyUse ,
63+ CoreJsonWebKey ,
64+ CoreAuthPrompt ,
65+ StandardErrorResponse < CoreErrorResponseType > ,
66+ KthTokenResponse ,
67+ CoreTokenType ,
68+ CoreTokenIntrospectionResponse ,
69+ CoreRevocableToken ,
70+ CoreRevocationErrorResponse ,
71+ > ;
72+
3373#[ derive( Deserialize ) ]
3474struct LoginUserData {
3575 username : Option < String > ,
@@ -72,7 +112,7 @@ pub fn post_users_login(
72112}
73113
74114#[ get( "/login" ) ]
75- pub fn kth_login ( mut cookies : Cookies ) -> Redirect {
115+ pub fn kth_login ( cookies : Cookies ) -> Redirect {
76116 if let Ok ( oidc) = env:: var ( "USE_OIDC" ) {
77117 println ! ( "use oidc: {}" , oidc) ;
78118 match oidc. as_str ( ) {
@@ -93,16 +133,16 @@ pub fn kth_login(mut cookies: Cookies) -> Redirect {
93133#[ derive( FromForm , Default ) ]
94134pub struct Code {
95135 code : Option < String > ,
96- state : Option < String > ,
136+ // state: Option<String>,
97137}
98138
99139#[ get( "/oidc-auth?<params..>" ) ]
100140pub fn kth_oidc_auth (
101- mut cookies : Cookies ,
102- conn : db:: DbConn ,
103- state : State < AppState > ,
141+ cookies : Cookies ,
142+ _conn : db:: DbConn ,
143+ _state : State < AppState > ,
104144 params : Form < Code > ,
105- client_addr : & ClientAddr ,
145+ // client_addr: &ClientAddr,
106146) -> Redirect {
107147 println ! ( "starting oidc auth" ) ;
108148 // cookies.add(Cookie::new("nonce", nonce.secret().clone()));
@@ -142,7 +182,7 @@ pub fn kth_auth(
142182 Redirect :: to ( "/" )
143183}
144184
145- pub fn get_client ( ) -> Result < CoreClient > {
185+ pub fn get_client ( ) -> Result < KthClient > {
146186 let provider_metadata = CoreProviderMetadata :: discover (
147187 & IssuerUrl :: new ( "https://login.ug.kth.se/adfs" . to_string ( ) ) ?,
148188 http_client,
@@ -152,7 +192,7 @@ pub fn get_client() -> Result<CoreClient> {
152192 // and token URL.
153193 let application_id = env:: var ( "APPLICATION_ID" ) . expect ( "OIDC need an application ID" ) ;
154194 let client_secret = env:: var ( "CLIENT_SECRET" ) . expect ( "OIDC need a client secret" ) ;
155- let client = CoreClient :: from_provider_metadata (
195+ let client = KthClient :: from_provider_metadata (
156196 provider_metadata,
157197 ClientId :: new ( application_id) ,
158198 Some ( ClientSecret :: new ( client_secret) ) ,
@@ -228,19 +268,5 @@ pub fn get_oidc_user(params: Form<Code>, nonce: Nonce) -> Result<()> {
228268 . unwrap_or( "<not provided>" ) ,
229269 ) ;
230270
231- // If available, we can use the UserInfo endpoint to request additional information.
232-
233- // The user_info request uses the AccessToken returned in the token response. To parse custom
234- // claims, use UserInfoClaims directly (with the desired type parameters) rather than using the
235- // CoreUserInfoClaims type alias.
236- let userinfo: CoreUserInfoClaims = client
237- . user_info ( token_response. access_token ( ) . to_owned ( ) , None )
238- . map_err ( |err| anyhow ! ( "No user info endpoint: {:?}" , err) ) ?
239- . request ( http_client)
240- . map_err ( |err| anyhow ! ( "Failed requesting user info: {:?}" , err) ) ?;
241-
242- println ! ( "user info: {:?}" , userinfo) ;
243- // See the OAuth2TokenResponse trait for a listing of other available fields such as
244- // access_token() and refresh_token().
245271 Ok ( ( ) )
246272}
0 commit comments