Skip to content

Commit c0dc37e

Browse files
committed
Move 3 backend routes to /api/private/session
1 parent 307930e commit c0dc37e

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

app/routes/github-authorize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default Route.extend({
1919
async beforeModel(transition) {
2020
try {
2121
let queryParams = serializeQueryParams(transition.queryParams);
22-
let resp = await fetch(`/authorize?${queryParams}`);
22+
let resp = await fetch(`/api/private/session/authorize?${queryParams}`);
2323
let json = await resp.json();
2424
let item = JSON.stringify({ ok: resp.ok, data: json });
2525
if (window.opener) {

app/routes/github-login.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Route from '@ember/routing/route';
22
import ajax from 'ember-fetch/ajax';
33

44
/**
5-
* Calling this route will query the `/authorize_url` API endpoint
5+
* Calling this route will query the `/api/private/session/begin` API endpoint
66
* and redirect to the received URL to initiate the OAuth flow.
77
*
88
* Example URL:
@@ -16,7 +16,7 @@ import ajax from 'ember-fetch/ajax';
1616
*/
1717
export default Route.extend({
1818
async beforeModel() {
19-
let url = await ajax(`/authorize_url`);
19+
let url = await ajax(`/api/private/session/begin`);
2020
window.location = url.url;
2121
},
2222
});

app/routes/logout.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default Route.extend({
77
session: service(),
88

99
async activate() {
10-
await ajax(`/logout`, { method: 'DELETE' });
10+
await ajax(`/api/private/session`, { method: 'DELETE' });
1111
run(() => {
1212
this.session.logoutUser();
1313
this.transitionTo('index');

src/controllers/user/session.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::models::{NewUser, User};
88
use crate::schema::users;
99
use crate::util::errors::{CargoError, ReadOnlyMode};
1010

11-
/// Handles the `GET /authorize_url` route.
11+
/// Handles the `GET /api/private/session/begin` route.
1212
///
1313
/// This route will return an authorization URL for the GitHub OAuth flow including the crates.io
1414
/// `client_id` and a randomly generated `state` secret.
@@ -23,7 +23,7 @@ use crate::util::errors::{CargoError, ReadOnlyMode};
2323
/// "url": "https://github.com/login/oauth/authorize?client_id=...&state=...&scope=read%3Aorg"
2424
/// }
2525
/// ```
26-
pub fn github_authorize(req: &mut dyn Request) -> CargoResult<Response> {
26+
pub fn begin(req: &mut dyn Request) -> CargoResult<Response> {
2727
let (url, state) = req
2828
.app()
2929
.github
@@ -43,7 +43,7 @@ pub fn github_authorize(req: &mut dyn Request) -> CargoResult<Response> {
4343
}))
4444
}
4545

46-
/// Handles the `GET /authorize` route.
46+
/// Handles the `GET /api/private/session/authorize` route.
4747
///
4848
/// This route is called from the GitHub API OAuth flow after the user accepted or rejected
4949
/// the data access permissions. It will check the `state` parameter and then call the GitHub API
@@ -71,7 +71,7 @@ pub fn github_authorize(req: &mut dyn Request) -> CargoResult<Response> {
7171
/// }
7272
/// }
7373
/// ```
74-
pub fn github_access_token(req: &mut dyn Request) -> CargoResult<Response> {
74+
pub fn authorize(req: &mut dyn Request) -> CargoResult<Response> {
7575
// Parse the url query
7676
let mut query = req.query();
7777
let code = query.remove("code").unwrap_or_default();
@@ -141,7 +141,7 @@ impl GithubUser {
141141
}
142142
}
143143

144-
/// Handles the `GET /logout` route.
144+
/// Handles the `DELETE /api/private/session` route.
145145
pub fn logout(req: &mut dyn Request) -> CargoResult<Response> {
146146
req.session().remove(&"user_id".to_string());
147147
Ok(req.json(&true))

src/router.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ pub fn build_router(app: &App) -> R404 {
112112
router.head("/api/v1/*path", R(Arc::clone(&api_router)));
113113
router.delete("/api/v1/*path", R(api_router));
114114

115-
router.get("/authorize_url", C(user::session::github_authorize));
116-
router.get("/authorize", C(user::session::github_access_token));
117-
router.delete("/logout", C(user::session::logout));
115+
// Session management
116+
router.get("/api/private/session/begin", C(user::session::begin));
117+
router.get(
118+
"/api/private/session/authorize",
119+
C(user::session::authorize),
120+
);
121+
router.delete("/api/private/session", C(user::session::logout));
118122

119123
// Only serve the local checkout of the git index in development mode.
120124
// In production, for crates.io, cargo gets the index from

src/tests/user.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,16 @@ impl crate::util::MockAnonymousUser {
106106
#[test]
107107
fn auth_gives_a_token() {
108108
let (_, anon) = TestApp::init().empty();
109-
let json: AuthResponse = anon.get("/authorize_url").good();
109+
let json: AuthResponse = anon.get("/api/private/session/begin").good();
110110
assert!(json.url.contains(&json.state));
111111
}
112112

113113
#[test]
114114
fn access_token_needs_data() {
115115
let (_, anon) = TestApp::init().empty();
116-
let json = anon.get::<()>("/authorize").bad_with_status(200); // Change endpoint to 400?
116+
let json = anon
117+
.get::<()>("/api/private/session/authorize")
118+
.bad_with_status(200); // Change endpoint to 400?
117119
assert!(json.errors[0].detail.contains("invalid state"));
118120
}
119121

0 commit comments

Comments
 (0)