aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/auth.rs
blob: a9ac394fa03b942697cd1fa4c61072869759d664 (plain) (blame)
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
use std::collections::HashMap;

use axum::{extract::Query, Extension};
use axum_macros::debug_handler;
use pasetors::{claims::Claims, keys::{AsymmetricKeyPair, Generate}, version4::V4};
use uuid::Uuid;

use crate::{errors::{NoneResult, ServiceError}, State};


/**
 * Takes in a request to create a new token with a secret key that gets printed
 * to stdout and, if it matches, returns a valid PASETO token that can be used
 * for future authentication
 */
#[debug_handler]
pub async fn begin(Query(params): Query<HashMap<String, String>>, Extension(state): Extension<State>) -> NoneResult {
    if let Some(k) = params.get("key") {
        if k == &state.gen_key {
            let mut claims = Claims::new()?; 
            claims.non_expiring();
            claims.audience("solard")?;
            claims.add_additional("uuid", Uuid::new_v4().to_string())?;

            let kp = AsymmetricKeyPair::<V4>::generate()?;
        } else {
            return Err(ServiceError::NotAuthorized);
        }
    } else {
        return Err(ServiceError::Generic("No key supplied".to_string()));
    }

    Ok(())
}