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
use crate::{
decode_auth_token_hex, AuthToken, AuthTokenHexString, AuthTokenStringDeserializationError,
};
use std::collections::VecDeque;
use std::fs;
use std::path::Path;
pub const USER_AUTH_TOKEN_FILE_NAME: &str = ".user_auth_token";
pub const REFLECTOR_AUTH_TOKEN_DEFAULT_FILE_NAME: &str = ".modality-reflector-auth-token";
pub fn write_user_auth_token_file(
path: &Path,
auth_token: AuthToken,
) -> Result<(), std::io::Error> {
let mut value: String = AuthTokenHexString::from(auth_token).into();
value.push('\n');
fs::write(path, value.as_bytes())
}
pub struct UserAuthTokenFileContents {
pub auth_token: AuthToken,
}
pub fn read_user_auth_token_file(
path: &Path,
) -> Result<Option<UserAuthTokenFileContents>, TokenUserFileReadError> {
if path.exists() {
let contents = fs::read_to_string(path)?;
if contents.trim().is_empty() {
return Ok(None);
}
let mut lines: VecDeque<&str> = contents.lines().collect();
if let Some(hex_line) = lines.pop_front() {
let auth_token = decode_auth_token_hex(hex_line)?;
Ok(Some(UserAuthTokenFileContents { auth_token }))
} else {
Ok(None)
}
} else {
Ok(None)
}
}
#[derive(Debug, thiserror::Error)]
pub enum TokenUserFileReadError {
#[error("IO Error")]
Io(
#[source]
#[from]
std::io::Error,
),
#[error("Auth token representation error")]
AuthTokenRepresentation(
#[source]
#[from]
AuthTokenStringDeserializationError,
),
}