Add HTTP endpoint authentication for username/password#7918
Add HTTP endpoint authentication for username/password#7918emamihe wants to merge 2 commits intonats-io:mainfrom
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7d0982e97d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
server/auth.go
Outdated
| // 2xx status codes indicate successful authentication | ||
| return resp.StatusCode >= 200 && resp.StatusCode < 300 |
There was a problem hiding this comment.
Reject redirected auth_http responses
checkAuthHTTP treats any final 2xx as success, but http.Client follows redirects by default, so a failed auth flow that returns 302 (common for login endpoints) and lands on a 200 page will be accepted as valid credentials. This can let invalid usernames/passwords authenticate whenever the configured endpoint signals failure via redirects instead of direct 4xx/5xx responses.
Useful? React with 👍 / 👎.
| if _, err := url.Parse(ah.URL); err != nil { | ||
| return nil, &configErr{tk, fmt.Sprintf("auth_http url is not valid: %v", err)} |
There was a problem hiding this comment.
Require an absolute URL for auth_http
The parser only checks url.Parse, which accepts relative or hostless values (for example /verify or auth-service:8080/verify) as “valid”. Those configs load successfully, but runtime auth calls then fail with request errors (unsupported protocol scheme / missing host), causing all username/password logins to fail until reconfigured.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3bba00a9d7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } else if opts.AuthHTTP != nil { | ||
| s.info.AuthRequired = true |
There was a problem hiding this comment.
Clear stale local credentials when auth_http is configured
When configureAuthorization takes the new opts.AuthHTTP != nil branch, it marks auth as required but does not reset s.users/s.nkeys. After a config reload from static users/nkeys to auth_http-only, those old in-memory credentials remain populated and are evaluated before checkAuthHTTP in processClientOrLeafAuthentication, so previously valid local usernames can still authenticate and bypass the external HTTP verifier until restart.
Useful? React with 👍 / 👎.
|
Delegating auth to an external auth provider is already supported through auth callout, and that is intended as the extension point. Wouldn't it be possible to write an auth callout service that interfaces with the auth service you need? This seems like reimplementing auth callout, but then making it HTTP specific too. |
|
Thanks for your attention @MauriceVanVeen . |
|
In that case it would be better to open an issue and discuss the problem there first, instead of raising a PR with a specific solution without knowing it can or will be accepted. See also: https://github.com/nats-io/nats-server/blob/main/CONTRIBUTING.md#contributing-changes For example, why did your NATS setup go down? That sounds like the primary problem here that needs to be looked into, not whether auth callout is used or not. And if the backoff was too long, couldn't that be decreased? Etc. |
Summary
Adds support for delegating username/password authentication to an external HTTP endpoint via the new
auth_httpconfiguration option. When clients connect with credentials, the server validates them by POSTing to the configured URL instead of checking against locally configured users.The auth endpoint can optionally return permissions in its response to restrict which subjects each user can publish to and subscribe to. If omitted, the user gets full access.
This enables integration with existing identity providers, LDAP, OAuth backends, or custom auth services without requiring a NATS-aware auth service (unlike
auth_calloutwhich uses NATS subjects).Configuration
Add the
auth_httpblock inside yourauthorizationsection:Request/Response Protocol
Request: The server POSTs JSON to the endpoint:
Response:
2xx = authentication success. The response body may optionally include permissions:
{
"permissions": {
"publish": { "allow": ["foo.", "bar.>"], "deny": ["secret.>"] },
"subscribe": { "allow": ["foo.", "bar.>"], "deny": ["secret.>"] }
}
}
Omit
permissionsor leave the body empty for full access.4xx/5xx = authentication failure.
Changes
auth_httpblock inauthorizationwithurland optionaltimeout(default 5s)permissionsfrom response bodyauth_httpis configured and a client sends username/password, credentials are validated via HTTP; on success the client is registered with the returned permissions (or full access if none)auth_httpandauth_calloutare mutually exclusive;auth_httpis not supported for cluster/gateway authorizationFiles changed
server/opts.go– AuthHTTP struct, config parsingserver/auth.go– HTTP auth check logic, permissions parsing, configureAuthorizationserver/reload.go– Reload support for AuthHTTPserver/opts_test.go– TestAuthHTTPConfigserver/auth_test.go– TestAuthHTTPWithPermissions (integration test)README.md– Documentation and examplesTesting
TestAuthHTTPConfig– verifies config parsingTestAuthHTTPWithPermissions– integration test with mock auth endpoint returning permissions; verifies publish/subscribe to allowed subjects succeeds and to denied subjects failsSigned-off-by: Hamid emami.he@gmail.com