Skip to content

Commit e374f43

Browse files
committed
fix(signin): accept application/x-www-form-urlencoded bodies
The legacy Ruby service accepts browser login forms posted as application/x-www-form-urlencoded, but action-controller only registers a JSON body parser by default, so POST /auth/signin returned 415 for form posts. SigninBody already defined .from_form; wire it up by registering a form-urlencoded body parser on the Application base controller so form logins are accepted (202 + Set-Cookie), matching the legacy behaviour.
1 parent f309202 commit e374f43

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

spec/controllers/sessions_spec.cr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ module PlaceOS::Auth
3030
user.try &.destroy
3131
end
3232

33+
it "accepts an application/x-www-form-urlencoded body (browser login form)", tags: "signin-form" do
34+
password = "ok-password-1234"
35+
user = create_user.call(password)
36+
37+
body = URI::Params.build do |fp|
38+
fp.add("email", user.email.to_s)
39+
fp.add("password", password)
40+
end
41+
headers = HTTP::Headers{
42+
"Host" => "localhost",
43+
"Content-Type" => "application/x-www-form-urlencoded",
44+
}
45+
result = client.post("/auth/signin", headers: headers, body: body)
46+
result.status_code.should eq 202
47+
result.headers["Set-Cookie"]?.should_not be_nil
48+
ensure
49+
user.try &.destroy
50+
end
51+
3352
it "redirects to a safe `continue` target when supplied" do
3453
password = "ok-password-1234"
3554
user = create_user.call(password)

src/placeos-auth/controllers/application.cr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ module PlaceOS::Auth
1111
Log = ::PlaceOS::Auth::Log.for(self)
1212
end
1313

14+
# The legacy Ruby service accepts browser login forms posted as
15+
# `application/x-www-form-urlencoded`; action-controller only ships a
16+
# JSON body parser out of the box. Register a form parser so typed
17+
# body arguments can be built from form posts. Body structs opt in by
18+
# defining `self.from_form(URI::Params)`.
19+
add_parser("application/x-www-form-urlencoded") do |klass, body_io, request|
20+
request_charset = ActionController::Support.charset(request.headers)
21+
body_io.set_encoding(request_charset) if request_charset
22+
klass.from_form(URI::Params.parse(body_io.gets_to_end))
23+
end
24+
1425
include Utils::CurrentUser
1526
include Utils::SessionHelper
1627

0 commit comments

Comments
 (0)