Skip to content

Commit f0f5d5b

Browse files
committed
change: auth with actix-web
1 parent 43317a8 commit f0f5d5b

File tree

1 file changed

+20
-21
lines changed
  • atcoder-problems-backend/src/server

1 file changed

+20
-21
lines changed

atcoder-problems-backend/src/server/auth.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
use crate::server::AppData;
22
use async_trait::async_trait;
3-
use cookie::Cookie;
43
use serde::{Deserialize, Serialize};
54
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;
97

108
const REDIRECT_URL: &str = "https://kenkoooo.com/atcoder/";
119

1210
#[async_trait]
1311
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>;
1614
}
1715

1816
#[derive(Serialize)]
@@ -41,7 +39,7 @@ pub struct GitHubAuthentication {
4139

4240
#[async_trait]
4341
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> {
4543
let request = TokenRequest {
4644
client_id: self.client_id.to_owned(),
4745
client_secret: self.client_secret.to_owned(),
@@ -58,7 +56,7 @@ impl Authentication for GitHubAuthentication {
5856
.await?;
5957
Ok(response.access_token)
6058
}
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> {
6260
let token_header = format!("token {}", access_token);
6361
let client = reqwest::Client::new();
6462
let response: GitHubUserResponse = client
@@ -88,25 +86,26 @@ struct Query {
8886
}
8987

9088
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();
9695

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)?;
9998
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)?;
101100

102101
let cookie = Cookie::build("token", token).path("/").finish();
103102
let redirect_fragment = query
104-
.redirect_to
103+
.redirect_to.clone()
105104
.unwrap_or_else(|| "/login/user".to_string());
106105
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();
111110
Ok(response)
112111
}

0 commit comments

Comments
 (0)