-
Notifications
You must be signed in to change notification settings - Fork 610
/
mod.rs
335 lines (300 loc) · 11.7 KB
/
mod.rs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use std::collections::HashMap;
use conduit::{Request, Response};
use conduit_cookie::{RequestSession};
use pg::GenericConnection;
use pg::rows::Row;
use pg::types::Slice;
use rand::{thread_rng, Rng};
use {Model, Version};
use app::RequestApp;
use db::RequestTransaction;
use krate::{Crate, EncodableCrate};
use util::errors::NotFound;
use util::{RequestUtils, CargoResult, internal, ChainError, human};
use version::EncodableVersion;
use http;
pub use self::middleware::{Middleware, RequestUser};
pub mod middleware;
/// The model representing a row in the `users` database table.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct User {
pub id: i32,
pub gh_login: String,
pub name: Option<String>,
pub email: Option<String>,
pub avatar: Option<String>,
pub gh_access_token: String,
pub api_token: String,
}
/// The serialization format for the `User` model.
#[derive(RustcDecodable, RustcEncodable)]
pub struct EncodableUser {
pub id: i32,
pub login: String,
pub email: Option<String>,
pub name: Option<String>,
pub avatar: Option<String>,
}
impl User {
/// Queries the database for a user with a certain `gh_login` value.
pub fn find_by_login(conn: &GenericConnection,
login: &str) -> CargoResult<User> {
let stmt = try!(conn.prepare("SELECT * FROM users
WHERE gh_login = $1"));
let rows = try!(stmt.query(&[&login]));
let row = try!(rows.iter().next().chain_error(|| {
NotFound
}));
Ok(Model::from_row(&row))
}
/// Queries the database for a user with a certain `api_token` value.
pub fn find_by_api_token(conn: &GenericConnection,
token: &str) -> CargoResult<User> {
let stmt = try!(conn.prepare("SELECT * FROM users \
WHERE api_token = $1 LIMIT 1"));
let rows = try!(stmt.query(&[&token]));
rows.iter().next().map(|r| Model::from_row(&r)).chain_error(|| {
NotFound
})
}
/// Updates a user or inserts a new user into the database.
pub fn find_or_insert(conn: &GenericConnection,
login: &str,
email: Option<&str>,
name: Option<&str>,
avatar: Option<&str>,
access_token: &str,
api_token: &str) -> CargoResult<User> {
// TODO: this is racy, but it looks like any other solution is...
// interesting! For now just do the racy thing which will report
// more errors than it needs to.
let stmt = try!(conn.prepare("UPDATE users
SET gh_access_token = $1,
email = $2,
name = $3,
gh_avatar = $4
WHERE gh_login = $5
RETURNING *"));
let rows = try!(stmt.query(&[&access_token,
&email, &name, &avatar,
&login]));
match rows.iter().next() {
Some(ref row) => return Ok(Model::from_row(row)),
None => {}
}
let stmt = try!(conn.prepare("INSERT INTO users
(email, gh_access_token, api_token,
gh_login, name, gh_avatar)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *"));
let rows = try!(stmt.query(&[&email,
&access_token,
&api_token,
&login,
&name, &avatar]));
Ok(Model::from_row(&try!(rows.iter().next().chain_error(|| {
internal("no user with email we just found")
}))))
}
/// Generates a new crates.io API token.
pub fn new_api_token() -> String {
thread_rng().gen_ascii_chars().take(32).collect()
}
/// Converts this `User` model into an `EncodableUser` for JSON serialization.
pub fn encodable(self) -> EncodableUser {
let User { id, email, api_token: _, gh_access_token: _,
name, gh_login, avatar } = self;
EncodableUser {
id: id,
email: email,
avatar: avatar,
login: gh_login,
name: name,
}
}
}
impl Model for User {
fn from_row(row: &Row) -> User {
User {
id: row.get("id"),
email: row.get("email"),
gh_access_token: row.get("gh_access_token"),
api_token: row.get("api_token"),
gh_login: row.get("gh_login"),
name: row.get("name"),
avatar: row.get("gh_avatar"),
}
}
fn table_name(_: Option<User>) -> &'static str { "users" }
}
/// Handles the `GET /authorize_url` route.
///
/// This route will return an authorization URL for the GitHub OAuth flow including the crates.io
/// `client_id` and a randomly generated `state` secret.
///
/// see https://developer.github.com/v3/oauth/#redirect-users-to-request-github-access
///
/// ## Response Body Example
///
/// ```json
/// {
/// "state": "b84a63c4ea3fcb4ac84",
/// "url": "https://github.com/login/oauth/authorize?client_id=...&state=...&scope=read%3Aorg"
/// }
/// ```
pub fn github_authorize(req: &mut Request) -> CargoResult<Response> {
// Generate a random 16 char ASCII string
let state: String = thread_rng().gen_ascii_chars().take(16).collect();
req.session().insert("github_oauth_state".to_string(), state.clone());
let url = req.app().github.authorize_url(state.clone());
#[derive(RustcEncodable)]
struct R { url: String, state: String }
Ok(req.json(&R { url: url.to_string(), state: state }))
}
/// Handles the `GET /authorize` route.
///
/// This route is called from the GitHub API OAuth flow after the user accepted or rejected
/// the data access permissions. It will check the `state` parameter and then call the GitHub API
/// to exchange the temporary `code` for an API token. The API token is returned together with
/// the corresponding user information.
///
/// see https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site
///
/// ## Query Parameters
///
/// - `code` – temporary code received from the GitHub API **(Required)**
/// - `state` – state parameter received from the GitHub API **(Required)**
///
/// ## Response Body Example
///
/// ```json
/// {
/// "api_token": "b84a63c4ea3fcb4ac84",
/// "user": {
/// "email": "[email protected]",
/// "name": "Foo Bar",
/// "login": "foobar",
/// "avatar": "https://avatars.githubusercontent.com/u/1234",
/// "url": null
/// }
/// }
/// ```
pub fn github_access_token(req: &mut Request) -> CargoResult<Response> {
// Parse the url query
let mut query = req.query();
let code = query.remove("code").unwrap_or(String::new());
let state = query.remove("state").unwrap_or(String::new());
// Make sure that the state we just got matches the session state that we
// should have issued earlier.
{
let session_state = req.session().remove(&"github_oauth_state".to_string());
let session_state = session_state.as_ref().map(|a| &a[..]);
if Some(&state[..]) != session_state {
return Err(human("invalid state parameter"))
}
}
#[derive(RustcDecodable)]
struct GithubUser {
email: Option<String>,
name: Option<String>,
login: String,
avatar_url: Option<String>,
}
// Fetch the access token from github using the code we just got
let token = match req.app().github.exchange(code.clone()) {
Ok(token) => token,
Err(s) => return Err(human(s)),
};
let resp = try!(http::github(req.app(), "/user", &token));
let ghuser: GithubUser = try!(http::parse_github_response(resp));
// Into the database!
let api_token = User::new_api_token();
let user = try!(User::find_or_insert(try!(req.tx()),
&ghuser.login,
ghuser.email.as_ref()
.map(|s| &s[..]),
ghuser.name.as_ref()
.map(|s| &s[..]),
ghuser.avatar_url.as_ref()
.map(|s| &s[..]),
&token.access_token,
&api_token));
req.session().insert("user_id".to_string(), user.id.to_string());
req.mut_extensions().insert(user);
me(req)
}
/// Handles the `GET /logout` route.
pub fn logout(req: &mut Request) -> CargoResult<Response> {
req.session().remove(&"user_id".to_string());
Ok(req.json(&true))
}
/// Handles the `GET /me/reset_token` route.
pub fn reset_token(req: &mut Request) -> CargoResult<Response> {
let user = try!(req.user());
let token = User::new_api_token();
let conn = try!(req.tx());
try!(conn.execute("UPDATE users SET api_token = $1 WHERE id = $2",
&[&token, &user.id]));
#[derive(RustcEncodable)]
struct R { api_token: String }
Ok(req.json(&R { api_token: token }))
}
/// Handles the `GET /me` route.
pub fn me(req: &mut Request) -> CargoResult<Response> {
let user = try!(req.user());
#[derive(RustcEncodable)]
struct R { user: EncodableUser, api_token: String }
let token = user.api_token.clone();
Ok(req.json(&R{ user: user.clone().encodable(), api_token: token }))
}
/// Handles the `GET /me/updates` route.
pub fn updates(req: &mut Request) -> CargoResult<Response> {
let user = try!(req.user());
let (offset, limit) = try!(req.pagination(10, 100));
let tx = try!(req.tx());
let sql = "SELECT versions.* FROM versions
INNER JOIN follows
ON follows.user_id = $1 AND
follows.crate_id = versions.crate_id
ORDER BY versions.created_at DESC OFFSET $2 LIMIT $3";
// Load all versions
let stmt = try!(tx.prepare(sql));
let mut versions = Vec::new();
let mut crate_ids = Vec::new();
for row in try!(stmt.query(&[&user.id, &offset, &limit])).iter() {
let version: Version = Model::from_row(&row);
crate_ids.push(version.crate_id);
versions.push(version);
}
// Load all crates
let mut map = HashMap::new();
let mut crates = Vec::new();
if crate_ids.len() > 0 {
let stmt = try!(tx.prepare("SELECT * FROM crates WHERE id = ANY($1)"));
for row in try!(stmt.query(&[&Slice(&crate_ids)])).iter() {
let krate: Crate = Model::from_row(&row);
map.insert(krate.id, krate.name.clone());
crates.push(krate);
}
}
// Encode everything!
let crates = crates.into_iter().map(|c| c.encodable(None, None)).collect();
let versions = versions.into_iter().map(|v| {
let id = v.crate_id;
v.encodable(&map[&id])
}).collect();
// Check if we have another
let sql = format!("SELECT 1 WHERE EXISTS({})", sql);
let stmt = try!(tx.prepare(&sql));
let more = try!(stmt.query(&[&user.id, &(offset + limit), &limit]))
.iter().next().is_some();
#[derive(RustcEncodable)]
struct R {
versions: Vec<EncodableVersion>,
crates: Vec<EncodableCrate>,
meta: Meta,
}
#[derive(RustcEncodable)]
struct Meta { more: bool }
Ok(req.json(&R{ versions: versions, crates: crates, meta: Meta { more: more } }))
}