Generate random runner tokens

This commit is contained in:
Joscha 2023-08-10 16:58:28 +02:00
parent f84a5b288e
commit 8005718584
3 changed files with 15 additions and 12 deletions

14
src/id.rs Normal file
View file

@ -0,0 +1,14 @@
use rand::{rngs::OsRng, seq::IteratorRandom};
const ID_CHARS: &str = "0123456789abcdefghijklmnopqrstuvwxyz";
fn random_id(prefix: &str, length: usize) -> String {
prefix
.chars()
.chain((0..length).map(|_| ID_CHARS.chars().choose(&mut OsRng).unwrap()))
.collect()
}
pub fn random_runner_token() -> String {
random_id("t", 30)
}

View file

@ -1,5 +1,6 @@
mod args;
mod config;
mod id;
mod runner;
mod server;
mod shared;

View file

@ -1,22 +1,10 @@
use std::time::Duration;
use gix::{actor::IdentityRef, date::Time};
use rand::{rngs::OsRng, seq::IteratorRandom};
use time::{macros::format_description, OffsetDateTime, UtcOffset};
use crate::somehow;
const RUN_ID_PREFIX: &str = "r-";
const RUN_ID_CHARS: &str = "0123456789abcdefghijklmnopqrstuvwxyz";
const RUN_ID_LEN: usize = 30; // log(16^40, base=len(RUN_ID_CHARS)) ~ 31
pub fn new_run_id() -> String {
RUN_ID_PREFIX
.chars()
.chain((0..RUN_ID_LEN).map(|_| RUN_ID_CHARS.chars().choose(&mut OsRng).unwrap()))
.collect()
}
pub fn time_to_offset_datetime(time: Time) -> somehow::Result<OffsetDateTime> {
Ok(OffsetDateTime::from_unix_timestamp(time.seconds)?
.to_offset(UtcOffset::from_whole_seconds(time.offset)?))