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
pub mod charges;
pub mod gamertag;
pub mod keysend;
pub mod ln_address;
pub mod login_with_zbd;
pub mod payments;
pub mod utilities;
pub mod wallet;
pub mod withdrawal_request;
use rand::Rng;
use serde::Deserialize;
use sha2::{Digest, Sha256};
use validator::Validate;
#[derive(Clone, Debug)]
pub struct ZebedeeClient {
pub domain: String,
pub reqw_cli: reqwest::Client,
pub apikey: String,
pub oauth: ZebedeeOauth,
}
impl ZebedeeClient {
pub fn new() -> Self {
ZebedeeClient::default()
}
pub fn domain(mut self, domain: String) -> Self {
self.domain = domain;
self
}
pub fn apikey(mut self, apikey: String) -> Self {
self.apikey = apikey;
self
}
pub fn reqw_cli(mut self, reqw_cli: reqwest::Client) -> Self {
self.reqw_cli = reqw_cli;
self
}
pub fn oauth(
mut self,
client_id: String,
secret: String,
redirect_uri: String,
state: String,
) -> Self {
let oauth = ZebedeeOauth::new(client_id, secret, redirect_uri, state);
self.oauth = oauth;
self
}
pub fn build(self) -> Self {
ZebedeeClient {
domain: self.domain,
reqw_cli: self.reqw_cli,
apikey: self.apikey,
oauth: self.oauth,
}
}
}
#[derive(Default, Clone, Validate, Deserialize, Debug)]
pub struct ZebedeeOauth {
#[validate(length(equal = 36))]
pub client_id: String,
#[validate(length(equal = 36))]
pub secret: String,
#[validate(url)]
pub redirect_uri: String,
#[validate(length(equal = 36))]
pub state: String,
}
impl ZebedeeOauth {
fn new(client_id: String, secret: String, redirect_uri: String, state: String) -> Self {
ZebedeeOauth {
client_id,
secret,
redirect_uri,
state,
}
}
}
impl Default for ZebedeeClient {
fn default() -> Self {
ZebedeeClient {
domain: String::from("https://api.zebedee.io"),
reqw_cli: reqwest::Client::new(),
apikey: String::from("errornotset"),
oauth: Default::default(),
}
}
}
#[derive(Clone, Debug, Validate, Deserialize)]
pub struct PKCE {
#[validate(length(equal = 43))]
pub verifier: String,
#[validate(length(equal = 43))]
pub challenge: String,
}
impl PKCE {
pub fn new(input: [u8; 32]) -> Self {
let verifier = base64_url::encode(&input);
let mut hasher_2 = Sha256::new();
hasher_2.update(verifier.clone());
let hash_result_2 = hasher_2.finalize();
let challenge = base64_url::encode(&hash_result_2);
let p = PKCE {
verifier,
challenge,
};
p.validate().unwrap();
p
}
pub fn new_from_string(input: String) -> Self {
let mut hasher = Sha256::new();
hasher.update(input);
let hash_result: [u8; 32] = hasher
.finalize()
.as_slice()
.try_into()
.expect("hashing went wrong from string");
Self::new(hash_result)
}
pub fn new_rand() -> Self {
let random_bytes = rand::thread_rng().gen::<[u8; 32]>();
Self::new(random_bytes)
}
}