1
1
use crate :: server:: AppData ;
2
2
use async_trait:: async_trait;
3
- use cookie:: Cookie ;
4
3
use serde:: { Deserialize , Serialize } ;
5
4
use sql_client:: internal:: user_manager:: UserManager ;
6
- use tide:: http:: headers:: LOCATION ;
7
- use tide:: StatusCode ;
8
- use tide:: { Request , Response , Result } ;
5
+ use actix_web:: { error, web, HttpRequest , HttpResponse , Result , cookie:: Cookie } ;
6
+ use actix_web:: http:: header:: LOCATION ;
9
7
10
8
const REDIRECT_URL : & str = "https://kenkoooo.com/atcoder/" ;
11
9
12
10
#[ async_trait]
13
11
pub trait Authentication {
14
- async fn get_token ( & self , code : & str ) -> Result < String > ;
15
- async fn get_user_id ( & self , token : & str ) -> Result < GitHubUserResponse > ;
12
+ async fn get_token ( & self , code : & str ) -> Result < String , reqwest :: Error > ;
13
+ async fn get_user_id ( & self , token : & str ) -> Result < GitHubUserResponse , reqwest :: Error > ;
16
14
}
17
15
18
16
#[ derive( Serialize ) ]
@@ -41,7 +39,7 @@ pub struct GitHubAuthentication {
41
39
42
40
#[ async_trait]
43
41
impl Authentication for GitHubAuthentication {
44
- async fn get_token ( & self , code : & str ) -> Result < String > {
42
+ async fn get_token ( & self , code : & str ) -> Result < String , reqwest :: Error > {
45
43
let request = TokenRequest {
46
44
client_id : self . client_id . to_owned ( ) ,
47
45
client_secret : self . client_secret . to_owned ( ) ,
@@ -58,7 +56,7 @@ impl Authentication for GitHubAuthentication {
58
56
. await ?;
59
57
Ok ( response. access_token )
60
58
}
61
- async fn get_user_id ( & self , access_token : & str ) -> Result < GitHubUserResponse > {
59
+ async fn get_user_id ( & self , access_token : & str ) -> Result < GitHubUserResponse , reqwest :: Error > {
62
60
let token_header = format ! ( "token {}" , access_token) ;
63
61
let client = reqwest:: Client :: new ( ) ;
64
62
let response: GitHubUserResponse = client
@@ -88,25 +86,26 @@ struct Query {
88
86
}
89
87
90
88
pub ( crate ) async fn get_token < A : Authentication + Clone > (
91
- request : Request < AppData < A > > ,
92
- ) -> Result < Response > {
93
- let query = request. query :: < Query > ( ) ?;
94
- let client = request. state ( ) . authentication . clone ( ) ;
95
- let conn = request. state ( ) . pg_pool . clone ( ) ;
89
+ request : HttpRequest ,
90
+ data : web:: Data < AppData < A > > ,
91
+ query : web:: Query < Query >
92
+ ) -> Result < HttpResponse > {
93
+ let client = data. authentication . clone ( ) ;
94
+ let conn = data. pg_pool . clone ( ) ;
96
95
97
- let token = client. get_token ( & query. code ) . await ?;
98
- let response = client. get_user_id ( & token) . await ?;
96
+ let token = client. get_token ( & query. code ) . await . map_err ( error :: ErrorInternalServerError ) ?;
97
+ let response = client. get_user_id ( & token) . await . map_err ( error :: ErrorInternalServerError ) ?;
99
98
let internal_user_id = response. id . to_string ( ) ;
100
- conn. register_user ( & internal_user_id) . await ?;
99
+ conn. register_user ( & internal_user_id) . await . map_err ( error :: ErrorInternalServerError ) ?;
101
100
102
101
let cookie = Cookie :: build ( "token" , token) . path ( "/" ) . finish ( ) ;
103
102
let redirect_fragment = query
104
- . redirect_to
103
+ . redirect_to . clone ( )
105
104
. unwrap_or_else ( || "/login/user" . to_string ( ) ) ;
106
105
let redirect_url = format ! ( "{}#{}" , REDIRECT_URL , redirect_fragment) ;
107
- let mut response = Response :: builder ( StatusCode :: Found )
108
- . header ( LOCATION , redirect_url)
109
- . build ( ) ;
110
- response . insert_cookie ( cookie ) ;
106
+ let response = HttpResponse :: Found ( )
107
+ . insert_header ( ( LOCATION , redirect_url) )
108
+ . cookie ( cookie )
109
+ . finish ( ) ;
111
110
Ok ( response)
112
111
}
0 commit comments