-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
70 lines (62 loc) · 1.82 KB
/
lib.rs
File metadata and controls
70 lines (62 loc) · 1.82 KB
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
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum SecretType {
OpenIdConnect(OIDCConfig),
GitLabProjectAccessToken(GitlabClientConfig),
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct OIDCConfig {
pub is_public: bool,
pub redirect_urls: Vec<String>,
}
impl OIDCConfig {
pub fn client_type(&self, name: &str) -> String {
format!("{}-{}", name, if self.is_public { "public" } else { "private" })
}
pub fn secret_type(&self) -> String {
if !self.is_public {
generate_secret()
} else {
String::with_capacity(0)
}
}
//use case federation id
pub fn flipped_client_type(&self, name: &str) -> String {
format!("{}-{}", name, if self.is_public { "private" } else { "public" })
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GitlabClientConfig {
/// Which GitLab server to use, e.g. 'verbis' or 'bbmri'
pub gitlab_instance: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum SecretResult {
AlreadyValid,
Created(String),
AlreadyExisted(String),
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum RequestType {
ValidateOrCreate(String),
Create,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SecretRequest {
pub request_type: RequestType,
pub secret_type: SecretType,
}
pub fn generate_secret() -> String {
use rand::Rng;
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789";
const PASSWORD_LEN: usize = 30;
let mut rng = rand::rng();
(0..PASSWORD_LEN)
.map(|_| {
let idx = rng.random_range(0..CHARSET.len());
CHARSET[idx] as char
})
.collect()
}