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
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use crate::{Fossology, FossologyError, FossologyResponse};
pub fn tokens(fossology: &Fossology, params: &TokensParameters) -> Result<Token, FossologyError> {
let response = fossology
.client
.post(&format!("{}/tokens", fossology.uri))
.json(¶ms)
.send()?
.json::<FossologyResponse<Token>>()?;
match response {
FossologyResponse::Response(res) => Ok(res),
FossologyResponse::ApiError(err) => Err(FossologyError::Other(err.message)),
}
}
#[derive(Debug, Serialize)]
pub struct TokensParameters {
username: String,
password: String,
token_name: String,
token_scope: TokenScope,
token_expire: NaiveDate,
}
#[derive(Debug, Serialize, Clone, Copy)]
#[serde(rename_all = "lowercase")]
pub enum TokenScope {
Read,
Write,
}
impl TokensParameters {
pub fn new(
username: &str,
password: &str,
token_name: &str,
token_scope: TokenScope,
token_expire: NaiveDate,
) -> Self {
Self {
username: username.to_string(),
password: password.to_string(),
token_name: token_name.to_string(),
token_scope,
token_expire,
}
}
}
#[derive(Debug, Deserialize)]
pub struct Token {
#[serde(rename = "Authorization")]
pub authorization: String,
}
#[cfg(test)]
pub(crate) mod test {
use chrono::{Duration, Utc};
use rand::{distributions::Alphanumeric, Rng};
use super::*;
pub fn create_test_fossology_with_writetoken(uri: &str) -> Fossology {
let fossology = Fossology::new(uri, "token").unwrap();
let token_name = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect::<String>();
let expiration_date = Utc::now()
.checked_add_signed(Duration::days(5))
.unwrap()
.naive_utc()
.date();
let params = TokensParameters::new(
"fossy",
"fossy",
&token_name,
TokenScope::Write,
expiration_date,
);
let token = tokens(&fossology, ¶ms).unwrap();
Fossology::new(
"http://localhost:8080/repo/api/v1",
token.authorization.strip_prefix("Bearer ").unwrap(),
)
.unwrap()
}
#[test]
fn generate_read_token() {
let fossology = Fossology::new("http://localhost:8080/repo/api/v1", "token").unwrap();
let token_name = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect::<String>();
let params = TokensParameters::new(
"fossy",
"fossy",
&token_name,
TokenScope::Read,
Utc::now()
.checked_add_signed(Duration::days(5))
.unwrap()
.naive_utc()
.date(),
);
let token = tokens(&fossology, ¶ms).unwrap();
assert!(token.authorization.starts_with("Bearer"));
}
#[test]
fn generate_write_token() {
let fossology = Fossology::new("http://localhost:8080/repo/api/v1", "token").unwrap();
let token_name = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect::<String>();
let params = TokensParameters::new(
"fossy",
"fossy",
&token_name,
TokenScope::Write,
Utc::now()
.checked_add_signed(Duration::days(5))
.unwrap()
.naive_utc()
.date(),
);
let tokens = tokens(&fossology, ¶ms).unwrap();
assert!(tokens.authorization.starts_with("Bearer"));
}
}