|
| 1 | +use actix_web::{ |
| 2 | + dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, |
| 3 | + Error, web, |
| 4 | +}; |
| 5 | +use futures_util::future::{LocalBoxFuture, ready, Ready}; |
| 6 | +use reqwest::Client; |
| 7 | +use serde::Deserialize; |
| 8 | + |
| 9 | +// Import any other modules that you reference in this file |
| 10 | +use fplus_database::database; |
| 11 | +#[derive(Deserialize, Debug)] |
| 12 | +struct RepoQuery { |
| 13 | + owner: String, |
| 14 | + repo: String, |
| 15 | +} |
| 16 | + |
| 17 | +pub struct GHAuth; |
| 18 | + |
| 19 | +impl<S, B> Transform<S, ServiceRequest> for GHAuth |
| 20 | +where |
| 21 | + S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, |
| 22 | + S::Future: 'static, |
| 23 | + B: 'static, |
| 24 | +{ |
| 25 | + type Response = ServiceResponse<B>; |
| 26 | + type Error = Error; |
| 27 | + type InitError = (); |
| 28 | + type Transform = GHAuthMiddleware<S>; |
| 29 | + type Future = Ready<Result<Self::Transform, Self::InitError>>; |
| 30 | + |
| 31 | + fn new_transform(&self, service: S) -> Self::Future { |
| 32 | + ready(Ok(GHAuthMiddleware { service })) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +pub struct GHAuthMiddleware<S> { |
| 37 | + service: S, |
| 38 | +} |
| 39 | + |
| 40 | +impl<S, B> Service<ServiceRequest> for GHAuthMiddleware<S> |
| 41 | +where |
| 42 | + S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>, |
| 43 | + S::Future: 'static, |
| 44 | + B: 'static, |
| 45 | +{ |
| 46 | + type Response = ServiceResponse<B>; |
| 47 | + type Error = Error; |
| 48 | + type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>; |
| 49 | + |
| 50 | + forward_ready!(service); |
| 51 | + |
| 52 | + fn call(&self, req: ServiceRequest) -> Self::Future { |
| 53 | + let query_string = req.query_string(); |
| 54 | + let query: Result<web::Query<RepoQuery>, _> = web::Query::from_query(query_string); |
| 55 | + let RepoQuery { owner, repo } = match query { |
| 56 | + Ok(q) => q.into_inner(), |
| 57 | + Err(_) => { |
| 58 | + return Box::pin(async { |
| 59 | + return Err(actix_web::error::ErrorBadRequest("Wrong query string format")); |
| 60 | + }); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + let auth_header_value = req.headers().get("Authorization") |
| 65 | + .and_then(|hv| hv.to_str().ok()) |
| 66 | + .filter(|hv| hv.starts_with("Bearer ")) |
| 67 | + .map(|hv| hv["Bearer ".len()..].to_string()); |
| 68 | + let fut = self.service.call(req); |
| 69 | + |
| 70 | + Box::pin(async move { |
| 71 | + let mut user_handle = String::new(); |
| 72 | + |
| 73 | + if let Some(token) = auth_header_value { |
| 74 | + // Make the asynchronous HTTP request here |
| 75 | + let client = Client::new(); |
| 76 | + let user_info_result = client.get("https://api.github.com/user") |
| 77 | + .header("Authorization", format!("Bearer {}", token)) |
| 78 | + .header("User-Agent", "Actix-web") |
| 79 | + .send() |
| 80 | + .await; |
| 81 | + |
| 82 | + match user_info_result { |
| 83 | + Ok(response) => { |
| 84 | + //Raise an actix test error |
| 85 | + if response.status().is_success() { |
| 86 | + let user_info = response |
| 87 | + .json::<serde_json::Value>() |
| 88 | + .await |
| 89 | + .expect("Failed to parse JSON"); |
| 90 | + |
| 91 | + if let Some(login) = user_info.get("login").and_then(|v| v.as_str()) { |
| 92 | + user_handle = login.to_string(); |
| 93 | + } else { |
| 94 | + println!("GitHub handle information not found."); |
| 95 | + return Err(actix_web::error::ErrorInternalServerError("GitHub handle information not found.")) |
| 96 | + } |
| 97 | + } else { |
| 98 | + println!("Failed to get GitHub user info"); |
| 99 | + return Err(actix_web::error::ErrorUnauthorized("Failed to get GitHub user info.")) |
| 100 | + } |
| 101 | + }, |
| 102 | + Err(e) => { |
| 103 | + println!("Request error: {:?}", e); |
| 104 | + return Err(actix_web::error::ErrorBadRequest(e)) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + match database::get_allocator(&owner, &repo).await { |
| 110 | + Ok(allocator) => { |
| 111 | + if let Some(allocator) = &allocator { |
| 112 | + if let Some(verifiers) = &allocator.verifiers_gh_handles { |
| 113 | + let verifier_handles: Vec<String> = verifiers.split(',') |
| 114 | + .map(|s| s.trim().to_lowercase()) |
| 115 | + .collect(); |
| 116 | + if verifier_handles.contains(&user_handle.to_lowercase()) { |
| 117 | + println!("{} is a verifier.", user_handle); |
| 118 | + } else { |
| 119 | + println!("The user is not a verifier."); |
| 120 | + return Err(actix_web::error::ErrorUnauthorized("The user is not a verifier.")) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + }, |
| 125 | + Err(e) => { |
| 126 | + println!("Failed to get allocator: {:?}", e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + let res = fut.await?; |
| 131 | + return Ok(res) |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
0 commit comments