Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Replace ad hoc auth header with internet standard bearer token auth header #3982

Merged
merged 8 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,5 @@ prometheus = { version = "0.13.3", features = ["process"], optional = true }
actix-web-prom = { version = "0.6.0", optional = true }
serial_test = { workspace = true }
clap = { version = "4.3.19", features = ["derive"] }
actix-web-httpauth = "0.8.1"
lemmy_federate = { version = "0.18.4", path = "crates/federate" }
2 changes: 1 addition & 1 deletion api_tests/src/comment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ test.skip("Remove a comment from admin and community on the same instance", asyn
test("Remove a comment from admin and community on different instance", async () => {
let alpha_user = await registerUser(alpha);
let newAlphaApi = new LemmyHttp(alphaUrl, {
headers: { auth: alpha_user.jwt ?? "" },
headers: { Authorization: `Bearer ${alpha_user.jwt ?? ""}` },
});

// New alpha user creates a community, post, and comment.
Expand Down
2 changes: 1 addition & 1 deletion api_tests/src/community.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ test("moderator view", async () => {
// register a new user with their own community on alpha and post to it
let registerUserRes = await registerUser(alpha);
let otherUser = new LemmyHttp(alphaUrl, {
headers: { auth: registerUserRes.jwt ?? "" },
headers: { Authorization: `Bearer ${registerUserRes.jwt ?? ""}` },
});

let otherCommunity = (await createCommunity(otherUser)).community_view;
Expand Down
2 changes: 1 addition & 1 deletion api_tests/src/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ test("Enforce site ban for federated user", async () => {
let alphaUserJwt = await registerUser(alpha);
expect(alphaUserJwt).toBeDefined();
let alpha_user = new LemmyHttp(alphaUrl, {
headers: { auth: alphaUserJwt.jwt ?? "" },
headers: { Authorization: `Bearer ${alphaUserJwt.jwt ?? ""}` },
});
let alphaUserActorId = (await getSite(alpha_user)).my_user?.local_user_view
.person.actor_id;
Expand Down
10 changes: 5 additions & 5 deletions api_tests/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ export async function setupLogins() {
resDelta,
resEpsilon,
]);
alpha.setHeaders({ auth: res[0].jwt ?? "" });
beta.setHeaders({ auth: res[1].jwt ?? "" });
gamma.setHeaders({ auth: res[2].jwt ?? "" });
delta.setHeaders({ auth: res[3].jwt ?? "" });
epsilon.setHeaders({ auth: res[4].jwt ?? "" });
alpha.setHeaders({ Authorization: `Bearer ${res[0].jwt ?? ""}` });
beta.setHeaders({ Authorization: `Bearer ${res[1].jwt ?? ""}` });
gamma.setHeaders({ Authorization: `Bearer ${res[2].jwt ?? ""}` });
delta.setHeaders({ Authorization: `Bearer ${res[3].jwt ?? ""}` });
epsilon.setHeaders({ Authorization: `Bearer ${res[4].jwt ?? ""}` });

// Registration applications are now enabled by default, need to disable them
let editSiteForm: EditSite = {
Expand Down
6 changes: 3 additions & 3 deletions api_tests/src/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ test("Create user", async () => {
let userRes = await registerUser(alpha);
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { auth: userRes.jwt ?? "" },
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});

let site = await getSite(user);
Expand All @@ -63,7 +63,7 @@ test("Delete user", async () => {
let userRes = await registerUser(alpha);
expect(userRes.jwt).toBeDefined();
let user = new LemmyHttp(alphaUrl, {
headers: { auth: userRes.jwt ?? "" },
headers: { Authorization: `Bearer ${userRes.jwt ?? ""}` },
});

// make a local post and comment
Expand Down Expand Up @@ -109,7 +109,7 @@ test("Delete user", async () => {

test("Requests with invalid auth should be treated as unauthenticated", async () => {
let invalid_auth = new LemmyHttp(alphaUrl, {
headers: { auth: "" },
headers: { Authorization: "Bearer foobar" },
});
let site = await getSite(invalid_auth);
expect(site.my_user).toBeUndefined();
Expand Down
2 changes: 1 addition & 1 deletion crates/utils/translations
11 changes: 4 additions & 7 deletions src/session_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use actix_web::{
body::MessageBody,
cookie::SameSite,
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
http::header::CACHE_CONTROL,
http::header::{Header, CACHE_CONTROL},
Error,
HttpMessage,
};
use actix_web_httpauth::headers::authorization::{Authorization, Bearer};
use chrono::{DateTime, Utc};
use core::future::Ready;
use futures_util::future::LocalBoxFuture;
Expand Down Expand Up @@ -76,13 +77,9 @@ where
let context = self.context.clone();

Box::pin(async move {
// Try reading jwt from auth header
let auth_header = req
.headers()
.get(AUTH_COOKIE_NAME)
.and_then(|h| h.to_str().ok());
let auth_header = Authorization::<Bearer>::parse(&req).ok();
let jwt = if let Some(a) = auth_header {
Some(a.to_string())
Some(a.as_ref().token().to_string())
}
// If that fails, try auth cookie. Dont use the `jwt` cookie from lemmy-ui because
// its not http-only.
Expand Down