portfu_admin 1.3.3

Library of Admin tools build on toip of Portfu
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use crate::auth::Claims;
use crate::services::{redirect_to_url, send_internal_error};
use crate::users::UserRole;
use http::HeaderValue;
use hyper::{header, StatusCode};
use oauth2::basic::BasicClient;
use oauth2::reqwest::async_http_client;
use oauth2::{
    AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, PkceCodeChallenge, RedirectUrl,
    Scope, TokenResponse, TokenUrl,
};
use octocrab::models::orgs::Organization;
use octocrab::models::Author;
use portfu::pfcore::service::{ServiceBuilder, ServiceGroup};
use portfu::pfcore::{FromRequest, Json, Query, ServiceData, ServiceHandler, ServiceType, State};
use portfu::prelude::async_trait;
use portfu::wrappers::sessions::Session;
use serde::{Deserialize, Serialize};
use std::env;
use std::future::Future;
use std::io::{Error, ErrorKind};
use std::num::ParseIntError;
use std::pin::Pin;
use std::sync::Arc;
use time::OffsetDateTime;
use tokio::sync::RwLock;

pub struct OAuthConfig {
    pub client: BasicClient,
    pub client_id: ClientId,
    pub client_secret: ClientSecret,
    pub oauthserver: String,
    pub auth_url: AuthUrl,
    pub token_url: TokenUrl,
    pub api_base_url: String,
    pub on_success_redirect: String,
    pub on_failure_redirect: String,
    pub claims_audience: String,
    pub claims_issuer: String,
    pub claims_expire_time: usize,
    pub allowed_organizations: Vec<u64>,
    pub allowed_users: Vec<u64>,
    pub admin_users: Vec<u64>,
}

#[derive(Default, Clone, Deserialize)]
pub struct UserData {
    pub user_id: u64,
    pub email: String,
    pub org_ids: Vec<u64>,
    pub user_role: UserRole,
}

#[derive(Default, Clone, Deserialize)]
pub struct AuthRequest {
    code: String,
    state: String,
}

pub type CallbackFn =
    Pin<Box<dyn Fn(usize) -> Box<dyn Future<Output = Result<(), Error>>> + Send + Sync + 'static>>;

pub enum OAuthCallbackFn {
    OnSuccess(CallbackFn),
    OnFailure(CallbackFn),
}

pub struct OAuthLoginHandler {
    config: Arc<OAuthConfig>,
}

#[derive(Deserialize, Serialize, Clone)]
pub struct OAuthLoginRedirectParams {
    redirect_url: String,
}
#[async_trait::async_trait]
impl ServiceHandler for OAuthLoginHandler {
    fn name(&self) -> &str {
        "login"
    }
    async fn handle(
        &self,
        mut data: portfu::prelude::ServiceData,
    ) -> Result<ServiceData, (ServiceData, Error)> {
        //Check if there is a current_page query
        let redirect_params = if let Ok(Some(q)) =
            Query::<Option<OAuthLoginRedirectParams>>::from_request(&mut data.request, "")
                .await
                .map(|q| q.inner())
        {
            Some(q)
        } else {
            None
        };
        // Create a PKCE code verifier and SHA-256 encode it as a code challenge.
        let (pkce_code_challenge, _pkce_code_verifier) = PkceCodeChallenge::new_random_sha256();
        // Generate the authorization URL to which we'll redirect the user.
        let client = &self.config.client;
        let auth_request = client
            .authorize_url(CsrfToken::new_random)
            // Set the desired scopes.
            .add_scope(Scope::new("read:user user:email read:org".to_string()))
            // Set the PKCE code challenge.
            .set_pkce_challenge(pkce_code_challenge);
        if let Some(redirect_params) = redirect_params {
            if let Ok(session) = State::<RwLock<Session>>::from_request(&mut data.request, "")
                .await
                .map(|q| q.inner())
            {
                session.write().await.data.insert(redirect_params);
            }
        }
        let (auth_url, _csrf_token) = auth_request.url();
        *data.response.status_mut() = StatusCode::FOUND;
        data.response.headers_mut().insert(
            header::LOCATION,
            HeaderValue::from_str(auth_url.as_str()).unwrap_or(HeaderValue::from_static("/")),
        );
        Ok(data)
    }

    fn service_type(&self) -> ServiceType {
        ServiceType::API
    }
}
pub struct OAuthAuthHandler {
    config: Arc<OAuthConfig>,
}
#[async_trait::async_trait]
impl ServiceHandler for OAuthAuthHandler {
    fn name(&self) -> &str {
        "auth"
    }
    async fn handle(
        &self,
        mut data: portfu::prelude::ServiceData,
    ) -> Result<ServiceData, (ServiceData, Error)> {
        let body: Option<AuthRequest> = match Json::from_request(&mut data.request, "").await {
            Ok(json) => json.inner(),
            Err(_) => None,
        };
        let body: AuthRequest = match body {
            None => match Query::<Option<AuthRequest>>::from_request(&mut data.request, "").await {
                Ok(v) => match v.inner() {
                    Some(v) => v,
                    None => {
                        return send_internal_error(data, "Failed to extract AuthRequest");
                    }
                },
                Err(e) => {
                    return send_internal_error(
                        data,
                        format!("Failed to extract Query as AuthRequest, {e:?}"),
                    );
                }
            },
            Some(v) => v,
        };
        let session = if let Some(session) = data.request.get_mut::<Arc<RwLock<Session>>>() {
            session
        } else {
            return send_internal_error(data, "Failed to Find Session to Auth");
        };
        let code = AuthorizationCode::new(body.code.clone());
        let _token_state = CsrfToken::new(body.state.clone());
        let client = &self.config.client;
        let token = if let Ok(token) = client
            .exchange_code(code)
            .request_async(async_http_client)
            .await
        {
            token
        } else {
            return redirect_to_url(data, self.config.on_failure_redirect.as_str());
        };
        let token_val = format!("Bearer {}", token.access_token().secret());
        let client = reqwest::Client::builder().build().unwrap();
        let user_info: Option<Author> = if let Ok(user_info) = client
            .get("https://api.github.com/user")
            .header("Authorization", &token_val)
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "portfu-login-service")
            .header("X-GitHub-Api-Version", "2022-11-28")
            .send()
            .await
        {
            user_info.json().await.ok()
        } else {
            return redirect_to_url(data, self.config.on_failure_redirect.as_str());
        };
        let org_info: Option<Vec<Organization>> = if let Ok(org_info) = client
            .get("https://api.github.com/user/orgs")
            .header("Authorization", &token_val)
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "portfu-login-service")
            .header("X-GitHub-Api-Version", "2022-11-28")
            .send()
            .await
        {
            org_info.json().await.ok()
        } else {
            return redirect_to_url(data, self.config.on_failure_redirect.as_str());
        };
        let mut claims: Claims = session.read().await.data.get().cloned().unwrap_or(Claims {
            aud: self.config.claims_audience.clone(),
            exp: self.config.claims_expire_time, //30 * 60, //30 Minutes
            iat: OffsetDateTime::now_utc().unix_timestamp() as usize,
            iss: self.config.claims_issuer.clone(),
            nbf: OffsetDateTime::now_utc().unix_timestamp() as usize,
            sub: "".to_string(),
            eml: "".to_string(),
            rol: UserRole::None,
            org: vec![],
        });
        if let Some(org_list) = &org_info {
            for org in org_list {
                claims.org.push(org.id.0);
                if self.config.allowed_organizations.contains(&org.id.0) {
                    claims.rol = UserRole::User;
                }
            }
        }
        if let Some(user_info) = user_info {
            if self.config.admin_users.contains(&user_info.id.0) {
                claims.rol = UserRole::Admin;
            } else if self.config.allowed_users.contains(&user_info.id.0) {
                claims.rol = UserRole::User;
            }
            claims.sub = user_info.id.to_string();
            claims.eml = user_info.email.unwrap_or_default();
        }
        session.write().await.data.insert(claims);
        if let Ok(session) = State::<RwLock<Session>>::from_request(&mut data.request, "")
            .await
            .map(|q| q.inner())
        {
            if let Some(redirect) = session
                .write()
                .await
                .data
                .remove::<OAuthLoginRedirectParams>()
            {
                return redirect_to_url(data, redirect.redirect_url.as_str());
            }
        }
        redirect_to_url(data, self.config.on_success_redirect.as_str())
    }

    fn service_type(&self) -> ServiceType {
        ServiceType::API
    }
}

#[derive(Default)]
pub struct OAuthLoginBuilder {
    pub client_id: Option<ClientId>,
    pub client_secret: Option<ClientSecret>,
    pub oauthserver: Option<String>,
    pub auth_url: Option<AuthUrl>,
    pub token_url: Option<TokenUrl>,
    pub api_base_url: Option<String>,
    pub redirect_url: Option<RedirectUrl>,
    pub on_success_redirect: Option<String>,
    pub on_failure_redirect: Option<String>,
    pub claims_audience: Option<String>,
    pub claims_issuer: Option<String>,
    pub claims_expire_time: Option<usize>,
    pub allowed_organizations: Vec<u64>,
    pub callbacks: Vec<OAuthCallbackFn>,
    pub allowed_users: Vec<u64>,
    pub admin_users: Vec<u64>,
}
impl OAuthLoginBuilder {
    pub fn from_env() -> Self {
        let oauthserver =
            env::var("OAUTH_SERVER").expect("Missing the OAUTH_SERVER environment variable.");
        OAuthLoginBuilder::new()
            .client_id(ClientId::new(
                env::var("OAUTH_CLIENT_ID")
                    .expect("Missing the OAUTH_CLIENT_ID environment variable."),
            ))
            .client_secret(ClientSecret::new(
                env::var("OAUTH_CLIENT_SECRET")
                    .expect("Missing the OAUTH_CLIENT_SECRET environment variable."),
            ))
            .oauthserver(oauthserver.clone())
            .auth_url(
                AuthUrl::new(format!("https://{}/oauth/authorize", oauthserver))
                    .expect("Invalid authorization endpoint URL"),
            )
            .on_success_redirect(
                env::var("OAUTH_SUCCESS_URL").unwrap_or_else(|_| String::from("/")),
            )
            .on_failure_redirect(
                env::var("OAUTH_FAILURE_URL").unwrap_or_else(|_| String::from("/")),
            )
            .claims_issuer(env::var("OAUTH_ISSUER").unwrap_or_else(|_| String::from("localhost")))
            .claims_audience(
                env::var("OAUTH_AUDIENCE").unwrap_or_else(|_| String::from("localhost")),
            )
            .claims_expire_time(
                env::var("OAUTH_EXPIRE_TIME")
                    .map(|s| s.parse().unwrap_or(30usize * 60usize))
                    .unwrap_or(30usize * 60usize),
            )
            .token_url(
                TokenUrl::new(format!("https://{}/oauth/access_token", oauthserver))
                    .expect("Invalid token endpoint URL"),
            )
            .api_base_url(format!("https://{}/api/v4", oauthserver))
            .allowed_organizations(
                &env::var("OAUTH_ORGANIZATIONS")
                    .unwrap_or_default()
                    .split(',')
                    .try_fold(vec![], |mut a, v| {
                        a.push(v.parse()?);
                        Ok::<Vec<u64>, ParseIntError>(a)
                    })
                    .unwrap_or_default(),
            )
            .allowed_users(
                &env::var("OAUTH_USERS")
                    .unwrap_or_default()
                    .split(',')
                    .try_fold(vec![], |mut a, v| {
                        a.push(v.parse()?);
                        Ok::<Vec<u64>, ParseIntError>(a)
                    })
                    .unwrap_or_default(),
            )
            .admin_users(
                &env::var("OAUTH_ADMINS")
                    .unwrap_or_default()
                    .split(',')
                    .try_fold(vec![], |mut a, v| {
                        a.push(v.parse()?);
                        Ok::<Vec<u64>, ParseIntError>(a)
                    })
                    .unwrap_or_default(),
            )
            .redirect_url(
                RedirectUrl::new(
                    env::var("OAUTH_REDIRECT_URL")
                        .expect("Missing the OAUTH_REDIRECT_URL environment variable."),
                )
                .expect("Invalid redirect URL"),
            )
    }
    pub fn new() -> Self {
        Default::default()
    }
    pub fn client_id(self, client_id: ClientId) -> Self {
        let mut s = self;
        s.client_id = Some(client_id);
        s
    }
    pub fn client_secret(self, client_secret: ClientSecret) -> Self {
        let mut s = self;
        s.client_secret = Some(client_secret);
        s
    }
    pub fn oauthserver(self, oauthserver: String) -> Self {
        let mut s = self;
        s.oauthserver = Some(oauthserver);
        s
    }
    pub fn auth_url(self, auth_url: AuthUrl) -> Self {
        let mut s = self;
        s.auth_url = Some(auth_url);
        s
    }
    pub fn on_success_redirect(self, on_success_redirect: String) -> Self {
        let mut s = self;
        s.on_success_redirect = Some(on_success_redirect);
        s
    }
    pub fn claims_audience(self, claims_audience: String) -> Self {
        let mut s = self;
        s.claims_audience = Some(claims_audience);
        s
    }
    pub fn claims_issuer(self, claims_issuer: String) -> Self {
        let mut s = self;
        s.claims_issuer = Some(claims_issuer);
        s
    }
    pub fn claims_expire_time(self, claims_expire_time: usize) -> Self {
        let mut s = self;
        s.claims_expire_time = Some(claims_expire_time);
        s
    }
    pub fn on_failure_redirect(self, on_failure_redirect: String) -> Self {
        let mut s = self;
        s.on_failure_redirect = Some(on_failure_redirect);
        s
    }
    pub fn token_url(self, token_url: TokenUrl) -> Self {
        let mut s = self;
        s.token_url = Some(token_url);
        s
    }
    pub fn api_base_url(self, api_base_url: String) -> Self {
        let mut s = self;
        s.api_base_url = Some(api_base_url);
        s
    }
    pub fn redirect_url(self, redirect_url: RedirectUrl) -> Self {
        let mut s = self;
        s.redirect_url = Some(redirect_url);
        s
    }
    pub fn allowed_organizations(self, allowed_organizations: &[u64]) -> Self {
        let mut s = self;
        s.allowed_organizations.extend(allowed_organizations);
        s
    }
    pub fn allowed_users(self, allowed_users: &[u64]) -> Self {
        let mut s = self;
        s.allowed_users.extend(allowed_users);
        s
    }
    pub fn admin_users(self, admin_users: &[u64]) -> Self {
        let mut s = self;
        s.admin_users.extend(admin_users);
        s
    }
    pub fn callbacks(self, callback: OAuthCallbackFn) -> Self {
        let mut s = self;
        s.callbacks.push(callback);
        s
    }
    pub fn build(self) -> Result<ServiceGroup, Error> {
        let client_id = self.client_id.ok_or(Error::new(
            ErrorKind::InvalidInput,
            "OAuth client_id not set",
        ))?;
        let client_secret = self.client_secret.ok_or(Error::new(
            ErrorKind::InvalidInput,
            "OAuth client_secret not set",
        ))?;
        let oauthserver = self.oauthserver.ok_or(Error::new(
            ErrorKind::InvalidInput,
            "OAuth oauthserver not set",
        ))?;
        let auth_url = self.auth_url.ok_or(Error::new(
            ErrorKind::InvalidInput,
            "OAuth auth_url not set",
        ))?;
        let token_url = self.token_url.ok_or(Error::new(
            ErrorKind::InvalidInput,
            "OAuth token_url not set",
        ))?;
        let api_base_url = self.api_base_url.ok_or(Error::new(
            ErrorKind::InvalidInput,
            "OAuth api_base_url not set",
        ))?;
        let redirect_url = self.redirect_url.ok_or(Error::new(
            ErrorKind::InvalidInput,
            "OAuth redirect_url not set",
        ))?;
        let config = Arc::new(OAuthConfig {
            client: BasicClient::new(
                client_id.clone(),
                Some(client_secret.clone()),
                auth_url.clone(),
                Some(token_url.clone()),
            )
            .set_redirect_uri(redirect_url),
            client_id,
            client_secret,
            oauthserver,
            auth_url,
            token_url,
            api_base_url,
            allowed_organizations: self.allowed_organizations,
            on_success_redirect: self
                .on_success_redirect
                .unwrap_or_else(|| String::from("/")),
            on_failure_redirect: self
                .on_failure_redirect
                .unwrap_or_else(|| String::from("/")),
            claims_audience: "".to_string(),
            claims_issuer: "".to_string(),
            allowed_users: self.allowed_users,
            admin_users: self.admin_users,
            claims_expire_time: 0,
        });
        let login_service = ServiceBuilder::new("/github/login")
            .name("index")
            .handler(Arc::new(OAuthLoginHandler {
                config: config.clone(),
            }))
            .build();
        let auth_service = ServiceBuilder::new("/github/auth")
            .name("index")
            .handler(Arc::new(OAuthAuthHandler {
                config: config.clone(),
            }))
            .build();
        Ok(ServiceGroup::default()
            .service(login_service)
            .service(auth_service))
    }
}