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
use collections::BTreeMap;
use rustc_serialize::{Decodable, json};
use rustc_serialize::json::{Json, ToJson};
use url::{Url};
use login::Login;
use protocol::{
KeyId, Method, Secret, SessionId, Timestamp, UserId
};
use user::User;
use question;
use question::{QuestionError, from_json};
#[derive(PartialEq, Eq, Debug, RustcDecodable, RustcEncodable)]
pub struct Realm {
key_id: KeyId,
secret: Secret,
api_url: Url
}
impl Realm {
pub fn new(key_id: KeyId, secret: Secret, url: Url) -> Realm {
Realm {
key_id: key_id,
secret: secret,
api_url: url,
}
}
pub fn raw_call(&self, method: &Method, params: &json::Object
) -> Result<Json, QuestionError> {
let &Realm{ ref key_id, ref secret, ref api_url } = self;
question::send_request(api_url, key_id, secret, method, params)
}
pub fn verify_login(&self, signed_data: &str, signature: &str
) -> Result<Login, QuestionError> {
if question::check_signature(&self.secret, signature, signed_data) {
question::unpack::<Login>(signed_data)
}
else {
Err(QuestionError::InvalidSignature)
}
}
pub fn check_valid_login(&self, uid: &UserId, sid: &SessionId, expires_at: &Timestamp
) -> Result<bool, question::QuestionError> {
let mut q: json::Object = BTreeMap::new();
q.insert("user_id" .to_string(), uid .to_json());
q.insert("session_id".to_string(), sid .to_json());
q.insert("expires_at".to_string(), expires_at.to_json());
self.raw_call(&Method::from_slice("realm.check_valid_login"), &q)
.and_then(|resp| {
match resp {
Json::Object(obj) => Ok(obj),
_ => Err(QuestionError::BadlyFormedResponse),
}
})
.and_then(|obj| {
match obj.get("return") {
Some(&Json::String(ref s)) => Ok(s.as_slice() == "true"),
_ => Err(QuestionError::BadlyFormedResponse),
}
})
}
pub fn question_challenge<A, B>(&self, question: &A, user_id: &Option<UserId>
) -> Result<B, QuestionError>
where A: ToJson, B: Decodable {
let mut q: json::Object = BTreeMap::new();
q.insert("question".to_string(), question.to_json());
match user_id {
&Some(ref uid) => q.insert("user_id".to_string(), uid.to_json()),
_ => None,
};
self.raw_call(&Method::from_slice("realm.question_challenge"), &q)
.and_then(|resp| {
match resp {
Json::Object(obj) => Ok(obj),
_ => Err(QuestionError::BadlyFormedResponse),
}
})
.and_then(|obj| {
match obj.get("results") {
Some(js) => from_json(js).map_err(QuestionError::DecoderError),
None => Err(QuestionError::BadlyFormedResponse),
}
})
}
pub fn user_get(&self, user_id: &UserId) -> Result<User, QuestionError> {
let mut q: json::Object = BTreeMap::new();
q.insert("user_id".to_string(), user_id.to_json());
self.raw_call(&Method::from_slice("realm.user_get"), &q)
.and_then(|resp| {
match resp {
Json::Object(obj) => Ok(obj),
_ => Err(QuestionError::BadlyFormedResponse),
}
})
.and_then(|obj| {
match obj.get("results") {
Some(js) => from_json(js).map_err(QuestionError::DecoderError),
None => Err(QuestionError::BadlyFormedResponse),
}
})
}
}