Skip to content

Commit 72a5e7e

Browse files
authored
Merge pull request #108 from omniauth/feat/json-body
2 parents 006487f + 94b4c93 commit 72a5e7e

37 files changed

+905
-201
lines changed

.rubocop_gradual.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"lib/omniauth-ldap/adaptor.rb:3925200886": [
33
[68, 7, 413, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 105664470]
44
],
5-
"spec/integration/middleware_spec.rb:4142891586": [
5+
"spec/integration/middleware_spec.rb:2185613788": [
66
[3, 16, 39, "RSpec/DescribeClass: The first argument to describe should be the class or module being tested.", 638096201],
77
[30, 14, 10, "RSpec/ExpectActual: Provide the actual value you are testing to `expect(...)`.", 837117997],
8-
[81, 5, 317, "RSpec/LeakyConstantDeclaration: Stub class constant instead of declaring explicitly.", 424933157]
8+
[130, 5, 317, "RSpec/LeakyConstantDeclaration: Stub class constant instead of declaring explicitly.", 424933157]
99
],
1010
"spec/integration/roda_integration_spec.rb:1921252381": [
1111
[3, 16, 50, "RSpec/DescribeClass: The first argument to describe should be the class or module being tested.", 3681952328],
@@ -23,14 +23,14 @@
2323
[47, 7, 38, "RSpec/AnyInstance: Avoid stubbing using `allow_any_instance_of`.", 3627954156],
2424
[84, 7, 48, "RSpec/AnyInstance: Avoid stubbing using `allow_any_instance_of`.", 2759780562]
2525
],
26-
"spec/omniauth/strategies/ldap_spec.rb:4166458344": [
27-
[126, 13, 9, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1130140517],
28-
[181, 17, 28, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 3444838747],
29-
[190, 17, 23, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1584148894],
30-
[201, 17, 32, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1515076977],
31-
[243, 19, 19, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2526348694],
32-
[269, 17, 56, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2413495789],
33-
[284, 13, 9, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 3182939526],
34-
[338, 15, 19, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2526348694]
26+
"spec/omniauth/strategies/ldap_spec.rb:2130811218": [
27+
[138, 13, 9, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1130140517],
28+
[193, 17, 28, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 3444838747],
29+
[202, 17, 23, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1584148894],
30+
[213, 17, 32, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1515076977],
31+
[255, 19, 19, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2526348694],
32+
[281, 17, 56, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2413495789],
33+
[296, 13, 9, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 3182939526],
34+
[350, 15, 19, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2526348694]
3535
]
3636
}

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,58 @@ end
329329

330330
Then link users to `/auth/ldap` in your app (for example, in a Devise sign-in page).
331331

332+
### Use JSON Body
333+
334+
This gem is compatible with JSON-encoded POST bodies as well as traditional form-encoded.
335+
336+
- Set header `Content-Type` to `application/json`.
337+
- Send a JSON object containing `username` and `password`.
338+
- Rails automatically exposes parsed JSON params via `env["action_dispatch.request.request_parameters"]`, which this strategy reads first. In non-Rails Rack apps, ensure you use a JSON parser middleware if you post raw JSON.
339+
340+
Examples
341+
342+
- curl (JSON):
343+
344+
```bash
345+
curl -i \
346+
-X POST \
347+
-H 'Content-Type: application/json' \
348+
-d '{"username":"alice","password":"secret"}' \
349+
http://localhost:3000/auth/ldap
350+
```
351+
352+
The request phase will redirect to `/auth/ldap/callback` when both fields are present.
353+
354+
- curl (form-encoded, still supported):
355+
356+
```bash
357+
curl -i \
358+
-X POST \
359+
-H 'Content-Type: application/x-www-form-urlencoded' \
360+
--data-urlencode 'username=alice' \
361+
--data-urlencode 'password=secret' \
362+
http://localhost:3000/auth/ldap
363+
```
364+
365+
- Browser (JavaScript fetch):
366+
367+
```js
368+
fetch('/auth/ldap', {
369+
method: 'POST',
370+
headers: { 'Content-Type': 'application/json' },
371+
body: JSON.stringify({ username: 'alice', password: 'secret' })
372+
}).then(res => {
373+
if (res.redirected) {
374+
window.location = res.url; // typically /auth/ldap/callback
375+
}
376+
});
377+
```
378+
379+
Notes
380+
381+
- You can still initiate authentication by visiting `GET /auth/ldap` to render the HTML form and then submitting it (form-encoded). JSON is an additional option, not a replacement.
382+
- In the callback phase (`POST /auth/ldap/callback`), the strategy reads JSON credentials the same way; Rails exposes them via `action_dispatch.request.request_parameters` and non-Rails apps should use a JSON parser middleware.
383+
332384
### Using a custom filter
333385

334386
If you need to restrict authentication to a group or use a more complex lookup, pass `:filter`. Use `%{username}` — it will be replaced with the processed username (after `:name_proc`).
@@ -708,6 +760,9 @@ See [LICENSE.txt][📄license] for the official [Copyright Notice][📄copyright
708760
</picture>
709761
</a>, and omniauth-ldap contributors.
710762
</li>
763+
<li>
764+
Copyright (C) 2014 David Benko
765+
</li>
711766
<li>
712767
Copyright (c) 2011 by Ping Yu and Intridea, Inc.
713768
</li>

docs/OmniAuth.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ <h2>Defined Under Namespace</h2>
107107
</div>
108108

109109
<div id="footer">
110-
Generated on Wed Nov 5 20:02:30 2025 by
110+
Generated on Thu Nov 6 02:24:32 2025 by
111111
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
112112
0.9.37 (ruby-3.4.7).
113113
</div>

docs/OmniAuth/LDAP.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ <h2>
135135
</div>
136136

137137
<div id="footer">
138-
Generated on Wed Nov 5 20:02:30 2025 by
138+
Generated on Thu Nov 6 02:24:32 2025 by
139139
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
140140
0.9.37 (ruby-3.4.7).
141141
</div>

0 commit comments

Comments
 (0)