-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathsession.rs
192 lines (173 loc) · 5.99 KB
/
session.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
use crate::controllers::frontend_prelude::*;
use crate::rate_limiter::RateLimiter;
use axum::extract::{FromRequestParts, Query};
use oauth2::reqwest::http_client;
use oauth2::{AuthorizationCode, CsrfToken, Scope, TokenResponse};
use tokio::runtime::Handle;
use crate::email::Emails;
use crate::middleware::log_request::RequestLogExt;
use crate::middleware::session::SessionExtension;
use crate::models::{NewUser, User};
use crate::schema::users;
use crate::util::errors::ReadOnlyMode;
use crate::views::EncodableMe;
use crates_io_github::GithubUser;
/// Handles the `GET /api/private/session/begin` 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 async fn begin(app: AppState, session: SessionExtension) -> Json<Value> {
let (url, state) = app
.github_oauth
.authorize_url(oauth2::CsrfToken::new_random)
.add_scope(Scope::new("read:org".to_string()))
.url();
let state = state.secret().to_string();
session.insert("github_oauth_state".to_string(), state.clone());
Json(json!({ "url": url.to_string(), "state": state }))
}
#[derive(Clone, Debug, Deserialize, FromRequestParts)]
#[from_request(via(Query))]
pub struct AuthorizeQuery {
code: AuthorizationCode,
state: CsrfToken,
}
/// Handles the `GET /api/private/session/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 async fn authorize(
query: AuthorizeQuery,
app: AppState,
session: SessionExtension,
req: Parts,
) -> AppResult<Json<EncodableMe>> {
let app_clone = app.clone();
let request_log = req.request_log().clone();
spawn_blocking(move || {
// Make sure that the state we just got matches the session state that we
// should have issued earlier.
let session_state = session.remove("github_oauth_state").map(CsrfToken::new);
if !session_state.is_some_and(|state| query.state.secret() == state.secret()) {
return Err(bad_request("invalid state parameter"));
}
// Fetch the access token from GitHub using the code we just got
let token = app
.github_oauth
.exchange_code(query.code)
.request(http_client)
.map_err(|err| {
request_log.add("cause", err);
server_error("Error obtaining token")
})?;
let token = token.access_token();
// Fetch the user info from GitHub using the access token we just got and create a user record
let ghuser = Handle::current().block_on(app.github.current_user(token))?;
let user = save_user_to_database(
&ghuser,
token.secret(),
&app.emails,
&app.rate_limiter,
&mut *app.db_write()?,
)?;
// Log in by setting a cookie and the middleware authentication
session.insert("user_id".to_string(), user.id.to_string());
Ok(())
})
.await?;
super::me::me(app_clone, req).await
}
fn save_user_to_database(
user: &GithubUser,
access_token: &str,
emails: &Emails,
rate_limiter: &RateLimiter,
conn: &mut PgConnection,
) -> AppResult<User> {
NewUser::new(
user.id,
&user.login,
user.name.as_deref(),
user.avatar_url.as_deref(),
access_token,
)
.create_or_update(user.email.as_deref(), emails, rate_limiter, conn)
.map_err(Into::into)
.or_else(|e: BoxedAppError| {
// If we're in read only mode, we can't update their details
// just look for an existing user
if e.is::<ReadOnlyMode>() {
users::table
.filter(users::gh_id.eq(user.id))
.first(conn)
.optional()?
.ok_or(e)
} else {
Err(e)
}
})
}
/// Handles the `DELETE /api/private/session` route.
pub async fn logout(session: SessionExtension) -> Json<bool> {
session.remove("user_id");
Json(true)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::test_db_connection;
#[test]
fn gh_user_with_invalid_email_doesnt_fail() {
let emails = Emails::new_in_memory();
let rate_limiter = RateLimiter::new(Default::default());
let (_test_db, conn) = &mut test_db_connection();
let gh_user = GithubUser {
email: Some("String.Format(\"{0}.{1}@live.com\", FirstName, LastName)".into()),
name: Some("My Name".into()),
login: "github_user".into(),
id: -1,
avatar_url: None,
};
let result =
save_user_to_database(&gh_user, "arbitrary_token", &emails, &rate_limiter, conn);
assert!(
result.is_ok(),
"Creating a User from a GitHub user failed when it shouldn't have, {result:?}"
);
}
}