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
use std::fmt;
use base64::{encode as b64encode};
use hyper;
use hyper::{Client};
use hyper::header::{Header, HeaderFormat};
use serde_json;
use B2Error;
use B2AuthHeader;
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct B2Credentials {
pub id: String,
pub key: String
}
impl B2Credentials {
pub fn id_key(&self) -> String {
format!("{}:{}", self.id, self.key)
}
pub fn auth_string(&self) -> String {
format!("Basic {}", b64encode(&self.id_key()))
}
pub fn authorize<'a>(&'a self, client: &Client) -> Result<B2Authorization<'a>,B2Error> {
let resp = try!(client.get("https://api.backblazeb2.com/b2api/v1/b2_authorize_account")
.header(self.clone())
.send());
if resp.status != hyper::status::StatusCode::Ok {
Err(B2Error::from_response(resp))
} else {
Ok(B2Authorization::from(self, try!(serde_json::from_reader(resp))))
}
}
}
impl HeaderFormat for B2Credentials {
fn fmt_header(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.auth_string().as_str())
}
}
impl Header for B2Credentials {
fn header_name() -> &'static str {
"Authorization"
}
#[allow(unused_variables)]
fn parse_header(raw: &[Vec<u8>]) -> hyper::Result<B2Credentials> {
panic!("we are not the b2 server");
}
}
#[derive(Serialize,Deserialize)]
#[serde(rename_all = "camelCase")]
struct B2AuthResponse {
authorization_token: String,
api_url: String,
download_url: String,
recommended_part_size: usize,
absolute_minimum_part_size: usize
}
#[derive(Debug)]
pub struct B2Authorization<'a> {
pub credentials: &'a B2Credentials,
pub authorization_token: String,
pub api_url: String,
pub download_url: String,
pub recommended_part_size: usize,
pub absolute_minimum_part_size: usize
}
impl<'a> B2Authorization<'a> {
fn from(credentials: &'a B2Credentials, resp: B2AuthResponse) -> B2Authorization<'a> {
B2Authorization {
credentials: credentials,
authorization_token: resp.authorization_token,
api_url: resp.api_url,
download_url: resp.download_url,
recommended_part_size: resp.recommended_part_size,
absolute_minimum_part_size: resp.absolute_minimum_part_size
}
}
pub fn auth_header(&self) -> B2AuthHeader {
B2AuthHeader(self.authorization_token.clone())
}
}