@@ -2,8 +2,11 @@ use atrium_api::types::string::Did;
22use axum:: {
33 Router ,
44 extract:: { FromRef , Query , State } ,
5- http:: header:: { HeaderMap , REFERER } ,
6- response:: { Html , IntoResponse , Json , Redirect } ,
5+ http:: {
6+ StatusCode ,
7+ header:: { HeaderMap , REFERER } ,
8+ } ,
9+ response:: { Html , IntoResponse , Json , Redirect , Response } ,
710 routing:: get,
811} ;
912use axum_extra:: extract:: cookie:: { Cookie , Key , SameSite , SignedCookieJar } ;
@@ -19,7 +22,7 @@ use tokio::net::TcpListener;
1922use tokio_util:: sync:: CancellationToken ;
2023use url:: Url ;
2124
22- use crate :: { ExpiringTaskMap , OAuth , OauthCallbackParams , ResolveHandleError } ;
25+ use crate :: { ExpiringTaskMap , OAuth , OAuthCallbackParams , OAuthCompleteError , ResolveHandleError } ;
2326
2427const FAVICON : & [ u8 ] = include_bytes ! ( "../static/favicon.ico" ) ;
2528const INDEX_HTML : & str = include_str ! ( "../static/index.html" ) ;
@@ -190,6 +193,30 @@ async fn start_oauth(
190193 ( jar, Redirect :: to ( & auth_url) )
191194}
192195
196+ impl OAuthCompleteError {
197+ fn to_error_response ( & self , engine : AppEngine ) -> Response {
198+ let ( _level, _desc) = match self {
199+ OAuthCompleteError :: Denied { .. } => {
200+ let status = StatusCode :: FORBIDDEN ;
201+ return ( status, RenderHtml ( "auth-fail" , engine, json ! ( { } ) ) ) . into_response ( ) ;
202+ }
203+ OAuthCompleteError :: Failed { .. } => (
204+ "error" ,
205+ "Something went wrong while requesting permission, sorry!" ,
206+ ) ,
207+ OAuthCompleteError :: CallbackFailed ( _) => (
208+ "error" ,
209+ "Something went wrong after permission was granted, sorry!" ,
210+ ) ,
211+ OAuthCompleteError :: NoDid => (
212+ "error" ,
213+ "Something went wrong when trying to confirm your identity, sorry!" ,
214+ ) ,
215+ } ;
216+ todo ! ( ) ;
217+ }
218+ }
219+
193220async fn complete_oauth (
194221 State ( AppState {
195222 engine,
@@ -198,11 +225,12 @@ async fn complete_oauth(
198225 shutdown,
199226 ..
200227 } ) : State < AppState > ,
201- Query ( params) : Query < OauthCallbackParams > ,
228+ Query ( params) : Query < OAuthCallbackParams > ,
202229 jar : SignedCookieJar ,
203- ) -> ( SignedCookieJar , impl IntoResponse ) {
204- let Ok ( did) = oauth. complete ( params) . await else {
205- panic ! ( "failed to do client callback" ) ;
230+ ) -> Result < ( SignedCookieJar , impl IntoResponse ) , Response > {
231+ let did = match oauth. complete ( params) . await {
232+ Ok ( did) => did,
233+ Err ( e) => return Err ( e. to_error_response ( engine) ) ,
206234 } ;
207235
208236 let cookie = Cookie :: build ( ( DID_COOKIE_KEY , did. to_string ( ) ) )
@@ -222,7 +250,7 @@ async fn complete_oauth(
222250 shutdown. child_token ( ) ,
223251 ) ;
224252
225- (
253+ Ok ( (
226254 jar,
227255 RenderHtml (
228256 "authorized" ,
@@ -232,5 +260,5 @@ async fn complete_oauth(
232260 "fetch_key" : fetch_key,
233261 } ) ,
234262 ) ,
235- )
263+ ) )
236264}
0 commit comments