Generate random runner tokens
This commit is contained in:
parent
f84a5b288e
commit
8005718584
3 changed files with 15 additions and 12 deletions
14
src/id.rs
Normal file
14
src/id.rs
Normal 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)
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
mod args;
|
mod args;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod id;
|
||||||
mod runner;
|
mod runner;
|
||||||
mod server;
|
mod server;
|
||||||
mod shared;
|
mod shared;
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,10 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use gix::{actor::IdentityRef, date::Time};
|
use gix::{actor::IdentityRef, date::Time};
|
||||||
use rand::{rngs::OsRng, seq::IteratorRandom};
|
|
||||||
use time::{macros::format_description, OffsetDateTime, UtcOffset};
|
use time::{macros::format_description, OffsetDateTime, UtcOffset};
|
||||||
|
|
||||||
use crate::somehow;
|
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> {
|
pub fn time_to_offset_datetime(time: Time) -> somehow::Result<OffsetDateTime> {
|
||||||
Ok(OffsetDateTime::from_unix_timestamp(time.seconds)?
|
Ok(OffsetDateTime::from_unix_timestamp(time.seconds)?
|
||||||
.to_offset(UtcOffset::from_whole_seconds(time.offset)?))
|
.to_offset(UtcOffset::from_whole_seconds(time.offset)?))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue