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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use crate::db::{DBEntity, User};
use crate::web::ui::check_logged_in;
use crate::web::utils::create_api_token;
use std::fmt::Display;
use std::str::FromStr;
use std::sync::Arc;

use askama::Template;
use axum::extract::{Path, State};
use axum::response::{Html, Redirect};
use axum::routing::get;
use axum::routing::post;
use axum::{debug_handler, Form, Router};

use axum::http::Uri;
use chrono::{DateTime, Duration, Utc};
use enum_iterator::Sequence;
use oauth2::CsrfToken;
use serde::{Deserialize, Serialize};
use tower_sessions::Session;
use tracing::error;

use crate::db::UserAuthToken;
use crate::web::GoatState;

static SESSION_CSRFTOKEN_FIELD: &str = "api_token_csrf_token";

#[derive(Template)]
#[template(path = "user_settings.html")]
struct Settings {
    pub user_is_admin: bool,
}

/// The user settings page at /ui/settings
pub async fn settings(State(_state): State<GoatState>) -> Html<String> {
    let context = Settings {
        user_is_admin: true,
    };

    Html::from(context.render().unwrap())
}

#[derive(Template)]
#[template(path = "user_api_tokens.html")]
struct ApiTokensGetPage {
    csrftoken: String,
    tokens: Vec<Arc<UserAuthToken>>,
    tokenkey: Option<String>,
    token_value: Option<String>,
    pub user_is_admin: bool,
}

pub async fn validate_csrf_expiry(user_input: &str, session: &mut Session) -> bool {
    let session_token: String = match session.get(SESSION_CSRFTOKEN_FIELD).await.unwrap() {
        None => {
            session
                .remove_value(SESSION_CSRFTOKEN_FIELD)
                .await
                .map_err(|err| {
                    error!("Failed to remove CSRF token from session: {err:?}");
                    false
                })
                .unwrap();
            log::debug!("Couldn't get session token from storage");
            return false;
        }
        Some(value) => value,
    };

    let mut split = session_token.split('|');
    let csrf_token = match split.next() {
        None => {
            log::debug!("Didn't get split token");
            return false;
        }
        Some(value) => value,
    };

    if user_input != csrf_token {
        log::debug!("Session and form CSRF token failed to match! user={user_input} <> session={csrf_token}");
        return false;
    }

    let expiry = match split.next() {
        None => {
            log::debug!("Couldn't get timestamp from stored CSRF Token");
            session
                .remove_value(SESSION_CSRFTOKEN_FIELD)
                .await
                .map_err(|err| {
                    error!("Failed to remove CSRF token from session: {err:?}");
                    false
                })
                .unwrap();
            return false;
        }
        Some(value) => value,
    };

    let expiry: DateTime<Utc> = match DateTime::parse_from_rfc3339(expiry) {
        Err(error) => {
            log::debug!("Failed to parse {expiry:?} into datetime: {error:?}");
            session
                .remove_value(SESSION_CSRFTOKEN_FIELD)
                .await
                .map_err(|err| {
                    error!("Failed to remove CSRF token from session: {err:?}");
                    false
                })
                .unwrap();
            return false;
        }
        Ok(value) => value.into(),
    };
    let now = Utc::now();

    if expiry < now {
        log::debug!("Token has expired at {expiry:?}, time is now {now:?}");
        session
            .remove_value(SESSION_CSRFTOKEN_FIELD)
            .await
            .map_err(|err| {
                error!("Failed to remove CSRF token from session: {err:?}");
                false
            })
            .unwrap();
        return false;
    }
    log::debug!("CSRF Token was valid!");
    true
}

/// Store a CSRF token with an expiry in the session store
///
/// Expiry defaults to 5 (minutes)
async fn store_api_csrf_token(
    session: &mut Session,
    expiry_plus_seconds: Option<i64>,
) -> Result<String, String> {
    let csrftoken = CsrfToken::new_random();
    let csrftoken = csrftoken.secret().to_string();

    let csrf_expiry: DateTime<Utc> =
        Utc::now() + Duration::seconds(expiry_plus_seconds.unwrap_or(300));

    let stored_csrf = format!("{csrftoken}|{}", csrf_expiry.to_rfc3339());

    // store it in the session storage
    if let Err(error) = session.insert(SESSION_CSRFTOKEN_FIELD, stored_csrf).await {
        // TODO: nice errors are nice but secure errors are better
        return Err(format!("Failed to store CSRF Token for user: {error:?}"));
    };
    Ok(csrftoken)
}

/// The user settings page at /ui/settings/api_tokens
#[debug_handler]
pub async fn api_tokens_get(
    State(state): State<GoatState>,
    mut session: Session,
) -> Result<Html<String>, Redirect> {
    let user = check_logged_in(&mut session, Uri::from_static(URI_SETTINGS_API_TOKENS)).await?;

    let csrftoken = match store_api_csrf_token(&mut session, None).await {
        Ok(value) => value,
        Err(error) => {
            log::error!("Failed to store csrf token in DB: {error:?}");
            return Err(Redirect::to("/"));
        }
    };

    // pull token from the session store new_api_token
    let token_value: Option<String> = session.remove("new_api_token").await.unwrap();

    let tokenkey: Option<String> = session.remove("new_api_tokenkey").await.unwrap();

    let tokens =
        match UserAuthToken::get_all_user(&state.read().await.connpool, user.id.unwrap()).await {
            Err(error) => {
                log::error!("Failed to pull tokens for user {:#?}: {error:?}", user.id);
                vec![]
            }
            Ok(val) => val,
        };

    let context = ApiTokensGetPage {
        csrftoken,
        tokens,
        tokenkey,
        token_value,
        user_is_admin: user.admin,
    };

    Ok(Html::from(context.render().unwrap()))
}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Sequence)]
pub enum ApiTokenLifetime {
    EightHours,
    TwentyFourHours,
    ThirtyDays,
    Forever,
}

impl From<ApiTokenLifetime> for i32 {
    fn from(input: ApiTokenLifetime) -> Self {
        match input {
            ApiTokenLifetime::EightHours => 8 * 60 * 60,
            ApiTokenLifetime::TwentyFourHours => 24 * 60 * 60,
            ApiTokenLifetime::ThirtyDays => 30 * 86400,
            ApiTokenLifetime::Forever => -1,
        }
    }
}

impl Display for ApiTokenLifetime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            ApiTokenLifetime::EightHours => "Eight Hours (8h)",
            ApiTokenLifetime::TwentyFourHours => "Twenty-Four Hours (24h)",
            ApiTokenLifetime::ThirtyDays => "Thirty Days (30d)",
            ApiTokenLifetime::Forever => "No Expiry",
        })
    }
}

impl ApiTokenLifetime {
    fn variant_str(&self) -> String {
        match self {
            ApiTokenLifetime::EightHours => "EightHours".to_string(),
            ApiTokenLifetime::TwentyFourHours => "TwentyFourHours".to_string(),
            ApiTokenLifetime::ThirtyDays => "ThirtyDays".to_string(),
            ApiTokenLifetime::Forever => "Forever".to_string(),
        }
    }
}

/// WHere are they in their token-creation-lifecycle?
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
pub enum ApiTokenCreatePageState {
    Start,
    Generating,
    Finished,
    Error,
}

impl Display for ApiTokenCreatePageState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ApiTokenCreatePageState::Start => f.write_fmt(format_args!("Start")),
            ApiTokenCreatePageState::Generating => f.write_fmt(format_args!("Generating")),
            ApiTokenCreatePageState::Finished => f.write_fmt(format_args!("Finished")),
            ApiTokenCreatePageState::Error => f.write_fmt(format_args!("Error")),
        }
    }
}

impl Default for ApiTokenCreatePageState {
    fn default() -> Self {
        Self::Start
    }
}

impl ApiTokenCreatePageState {
    pub fn next(&self) -> Self {
        match &self {
            ApiTokenCreatePageState::Start => Self::Generating,
            ApiTokenCreatePageState::Generating => Self::Finished,
            ApiTokenCreatePageState::Finished => Self::Error,
            ApiTokenCreatePageState::Error => Self::Start,
        }
    }
}

/// Form handler for the api tokens post endpoint
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Template, Default)]
#[template(path = "user_api_token_form.html")]
pub struct ApiTokenPage {
    pub token_name: Option<String>,
    pub tokenkey: Option<String>,
    /// For when we want show the token
    pub token_value: Option<String>,
    pub csrftoken: String,
    pub state: ApiTokenCreatePageState,
    /// When the user selects the lifetime in the creation form
    pub lifetime: Option<ApiTokenLifetime>,
    /// Used to show the possible lifetimes in the creation form
    pub lifetimes: Option<Vec<(String, String)>>,
    pub user_is_admin: bool,
}

/// The user settings page at /ui/settings
// #[debug_handler]
pub async fn api_tokens_post(
    mut session: Session,
    State(state): State<GoatState>,
    Form(form): Form<ApiTokenPage>,
) -> Result<Html<String>, Redirect> {
    eprintln!("Got form: {form:?}");

    let user = check_logged_in(&mut session, Uri::from_static(URI_SETTINGS_API_TOKENS)).await?;

    if !validate_csrf_expiry(&form.csrftoken, &mut session).await {
        // TODO: redirect to the start
        log::debug!("Failed to validate csrf expiry");
        return Err(Redirect::to("/ui/settings"));
    }

    let context = match form.state {
        // Present the "select a lifetime" page
        ApiTokenCreatePageState::Start => {
            // present the user with the lifetime form
            let lifetimes: Vec<(String, String)> = enum_iterator::all::<ApiTokenLifetime>()
                .map(|l| (l.to_string(), l.variant_str()))
                .collect();
            eprintln!("Lifetimes: {lifetimes:?}");
            ApiTokenPage {
                csrftoken: store_api_csrf_token(&mut session, None).await.unwrap(),
                state: ApiTokenCreatePageState::Start,
                token_name: None,
                lifetimes: Some(lifetimes),
                lifetime: None,
                tokenkey: None,
                token_value: None,
                user_is_admin: user.admin,
            }
        }
        // The user has set a lifetime and we're generating a token
        ApiTokenCreatePageState::Generating => {
            log::debug!("In the 'Generating' state");

            // generate the credential

            let state_reader = state.read().await;
            let api_cookie_secret = state_reader.config.api_cookie_secret();
            let lifetime: i32 = form.lifetime.unwrap().into();
            // get the user id from the session store, we should be able to safely unwrap here because we checked they were logged in up higher
            let user: User = match session.get("user").await.unwrap() {
                Some(val) => val,
                None => {
                    log::debug!("Couldn't get user from session store");
                    return Err(Redirect::to("/ui"));
                }
            };
            let userid: i64 = user.id.unwrap();

            let api_token = create_api_token(api_cookie_secret, lifetime, userid);

            // store the token in the database
            log::trace!("Starting to store token in the DB, grabbing writer...");
            println!("got writer...");
            let uat = UserAuthToken {
                id: None,
                name: form.token_name.unwrap(),
                issued: api_token.issued,
                expiry: api_token.expiry,
                userid,
                tokenkey: api_token.token_key.to_owned(),
                tokenhash: api_token.token_hash,
            };
            log::trace!("Starting to store token in the DB, grabbing transaction...");

            let mut txn = match state_reader.connpool.begin().await {
                Ok(val) => val,
                Err(error) => todo!(
                    "Need to handle failing to pick up a txn for api token storage: {error:?}"
                ),
            };

            log::trace!("Starting to store token in the DB, saving...");
            match uat.save_with_txn(&mut txn).await {
                Err(error) => todo!("Need to handle this! {error:?}"),
                Ok(_) => {
                    // store the api token in the session store
                    if let Err(error) = session
                        .insert("new_api_token", &api_token.token_secret)
                        .await
                    {
                        log::error!(
                            "Failed to store new API token in the session, ruh roh? {error:?}"
                        );
                        txn.rollback()
                            .await
                            .map_err(|e| {
                                log::error!("Txn rollback fail: {e:?}");
                                todo!()
                            })
                            .unwrap();
                        // TODO: bail, which should roll back the txn
                        todo!("Failed to store new API token in the session, ruh roh? {error:?}");
                    };

                    if let Err(error) = session
                        .insert("new_api_tokenkey", &api_token.token_key)
                        .await
                    {
                        log::error!(
                            "Failed to store new API tokenkey in the session, ruh roh? {error:?}"
                        );
                        txn.rollback()
                            .await
                            .map_err(|e| {
                                log::error!("Txn rollback fail: {e:?}");
                                todo!()
                            })
                            .unwrap();
                        // TODO: bail, which should roll back the txn
                        todo!(
                            "Failed to store new API tokenkey in the session, ruh roh? {error:?}"
                        );
                    };

                    if let Err(error) = txn.commit().await {
                        log::error!("Failed to save the API token to storage, oh no?");
                        todo!("Failed to save the API token to storage, oh no? {error:?}");
                    };
                }
            };
            // redirect the user to the display page
            let csrftoken = store_api_csrf_token(&mut session, Some(30)).await.unwrap();
            return Err(Redirect::to(&format!(
                "/ui/settings/api_tokens?state={csrftoken}?token_created=1"
            )));
        }

        ApiTokenCreatePageState::Finished => todo!(),
        ApiTokenCreatePageState::Error => todo!(),
    };
    Ok(Html::from(context.render().unwrap()))

    // Html::from("Welcome to the api tokens page".to_string())
}

#[derive(Deserialize, Serialize, Debug, Clone, Template)]
#[template(path = "user_api_token_delete.html")]
pub struct ApiTokenDelete {
    pub id: i64,
    pub token_name: Option<String>,
    pub csrftoken: String,
    pub user_is_admin: bool,
}

const URI_SETTINGS_API_TOKENS: &str = "/ui/settings/api_tokens";

// #[debug_handler]
pub async fn api_tokens_delete_get(
    axum::extract::State(state): axum::extract::State<GoatState>,
    // Form(form): Form<ApiTokenPage>,
    Path(id): Path<String>,
    mut session: Session,
) -> Result<Html<String>, Redirect> {
    let user = check_logged_in(&mut session, Uri::from_static(URI_SETTINGS_API_TOKENS)).await?;

    let csrftoken = match store_api_csrf_token(&mut session, None).await {
        Ok(val) => val,
        Err(err) => {
            log::error!("Failed to store CSRF token in the session store: {err:?}");
            return Err(Redirect::to(URI_SETTINGS_API_TOKENS));
        }
    };

    let id = match i64::from_str(&id) {
        Ok(val) => val,
        Err(error) => {
            log::debug!("Got an invalid id parsing the URL: {error:?}");
            return Err(Redirect::to("/"));
        }
    };

    let state_reader = state.read().await;
    let pool = state_reader.connpool.clone();
    let uat = match UserAuthToken::get(&pool, id).await {
        Err(err) => {
            log::debug!("Requested delete for token: {err:?}");
            return Err(Redirect::to(URI_SETTINGS_API_TOKENS));
        }
        Ok(res) => {
            if res.userid != user.id.unwrap() {
                log::debug!(
                    "You can't delete another user's tokens! uid={} token.userid={}",
                    user.id.unwrap(),
                    res.userid
                );
                return Err(Redirect::to(URI_SETTINGS_API_TOKENS));
            };

            res
        }
    };

    let context = ApiTokenDelete {
        id,
        token_name: Some(uat.name.clone()),
        csrftoken,
        user_is_admin: user.admin,
    };

    Ok(Html::from(context.render().unwrap()))
}

// #[debug_handler]
pub async fn api_tokens_delete_post(
    State(state): State<GoatState>,
    mut session: Session,
    Form(form): Form<ApiTokenDelete>,
) -> Result<Html<String>, Redirect> {
    check_logged_in(&mut session, Uri::from_static(URI_SETTINGS_API_TOKENS)).await?;

    if !validate_csrf_expiry(&form.csrftoken, &mut session).await {
        // TODO: redirect to the start
        log::debug!("Failed to validate csrf expiry");
        return Err(Redirect::to("/ui/settings"));
    }

    log::debug!("Deleting token from Form: {form:?}");
    let user: User = match session.get("user").await.unwrap() {
        Some(val) => val,
        None => {
            log::debug!("Couldn't get user from session store");
            return Err(Redirect::to("/ui"));
        }
    };
    let pool = state.read().await.connpool.clone();
    let uat = match UserAuthToken::get(&pool, form.id).await {
        Err(err) => {
            log::debug!("Requested delete for existing token: {err:?}");
            return Err(Redirect::to(URI_SETTINGS_API_TOKENS));
        }
        Ok(res) => {
            if res.userid != user.id.unwrap() {
                log::debug!(
                    "You can't delete another user's tokens! uid={} token.userid={}",
                    user.id.unwrap(),
                    res.userid
                );
                return Err(Redirect::to(URI_SETTINGS_API_TOKENS));
            };

            res
        }
    };

    if let Err(error) = uat.delete(&pool).await {
        log::debug!("Failed to delete token {:?}: {error:?}", uat.id);
    };

    log::info!(
        "id={} action=api_token_delete token_id={}",
        uat.userid,
        uat.id.unwrap()
    );
    Err(Redirect::to(URI_SETTINGS_API_TOKENS))
}
/// Build the router for user settings
pub fn router() -> Router<GoatState> {
    Router::new()
        .route("/", get(settings))
        .route("/api_tokens", get(api_tokens_get))
        .route("/api_tokens", post(api_tokens_post))
        .route("/api_tokens/delete/:id", get(api_tokens_delete_get))
        .route("/api_tokens/delete/:id", post(api_tokens_delete_post))
}