Forward fresh-login requirements to the authentication provider
All checks were successful
Build and Publish Container Image / build-and-push (push) Successful in 44s
All checks were successful
Build and Publish Container Image / build-and-push (push) Successful in 44s
Assistant: codex Assistant-Model: gpt-6-astra Assistant-Session: 01a092fe-13b1-7f12-ac74-7d258af4d79c
This commit is contained in:
parent
139994cfac
commit
8d4336e944
7 changed files with 103 additions and 3 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -54,6 +55,12 @@ func (a *AutheliaAdapter) AuthorizeURL(_ context.Context, req domain.AuthRequest
|
|||
q.Set("response_type", "code")
|
||||
q.Set("state", req.State)
|
||||
q.Set("scope", "openid profile email groups")
|
||||
if req.PromptLogin {
|
||||
q.Set("prompt", "login")
|
||||
}
|
||||
if req.MaxAge != nil {
|
||||
q.Set("max_age", strconv.FormatInt(int64(req.MaxAge.Seconds()), 10))
|
||||
}
|
||||
|
||||
return base + "?" + q.Encode(), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -452,3 +452,23 @@ func TestHandleCallback_AuthResultContainsNoRawTokens(t *testing.T) {
|
|||
t.Error("AuthResult.Claims must not expose raw access_token — security boundary violation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeURLForwardsFreshLoginRequirements(t *testing.T) {
|
||||
adapter := authelia.New(testConfig(), &mockHTTPClient{})
|
||||
for _, seconds := range []int{0, 60} {
|
||||
age := time.Duration(seconds) * time.Second
|
||||
target, err := adapter.AuthorizeURL(context.Background(), domain.AuthRequest{PromptLogin: true, MaxAge: &age})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
parsed, _ := url.Parse(target)
|
||||
if parsed.Query().Get("prompt") != "login" || parsed.Query().Get("max_age") != fmt.Sprint(seconds) {
|
||||
t.Fatalf("freshness requirements missing from provider URL: %s", target)
|
||||
}
|
||||
}
|
||||
target, _ := adapter.AuthorizeURL(context.Background(), domain.AuthRequest{})
|
||||
parsed, _ := url.Parse(target)
|
||||
if parsed.Query().Has("prompt") || parsed.Query().Has("max_age") {
|
||||
t.Fatal("ordinary SSO was changed")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -386,8 +386,8 @@ func TestServiceClientExampleContracts(t *testing.T) {
|
|||
if errs := config.ValidateConfig(cfg); len(errs) != 0 {
|
||||
t.Fatalf("service client examples must validate: %v", errs)
|
||||
}
|
||||
if len(cfg.Clients) != 4 {
|
||||
t.Fatalf("service client examples: want 4, got %d", len(cfg.Clients))
|
||||
if len(cfg.Clients) != 5 {
|
||||
t.Fatalf("client examples: want 4 service clients and 1 human client, got %d", len(cfg.Clients))
|
||||
}
|
||||
|
||||
codingAgent := cfg.Clients[0]
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package domain
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AuthProvider handles login UI delegation and session management.
|
||||
|
|
@ -18,6 +19,9 @@ type AuthProvider interface {
|
|||
|
||||
// AuthRequest contains the parameters for initiating an auth flow.
|
||||
type AuthRequest struct {
|
||||
// Forward interactive freshness requirements to the actual login provider.
|
||||
PromptLogin bool
|
||||
MaxAge *time.Duration
|
||||
ClientID string
|
||||
RedirectURI string
|
||||
State string
|
||||
|
|
|
|||
|
|
@ -246,6 +246,8 @@ func (h *AuthorizeHandler) serveAuthorize(w http.ResponseWriter, r *http.Request
|
|||
|
||||
// Delegate to Auth provider.
|
||||
authURL, err := h.Auth.AuthorizeURL(ctx, domain.AuthRequest{
|
||||
PromptLogin: promptLogin,
|
||||
MaxAge: maxAge,
|
||||
ClientID: clientID,
|
||||
RedirectURI: redirectURI,
|
||||
State: state,
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import (
|
|||
|
||||
// mockAuthProvider implements domain.AuthProvider.
|
||||
type mockAuthProvider struct {
|
||||
request domain.AuthRequest
|
||||
authorizeURL string
|
||||
authorizeErr error
|
||||
|
||||
|
|
@ -29,7 +30,8 @@ type mockAuthProvider struct {
|
|||
callbackErr error
|
||||
}
|
||||
|
||||
func (m *mockAuthProvider) AuthorizeURL(_ context.Context, _ domain.AuthRequest) (string, error) {
|
||||
func (m *mockAuthProvider) AuthorizeURL(_ context.Context, req domain.AuthRequest) (string, error) {
|
||||
m.request = req
|
||||
if m.authorizeErr != nil {
|
||||
return "", m.authorizeErr
|
||||
}
|
||||
|
|
@ -871,3 +873,22 @@ func TestAuthorizeHandler_ServeHTTP_DispatchesToCallback(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreshLoginRequirementsReachProvider(t *testing.T) {
|
||||
for _, age := range []string{"0", "60"} {
|
||||
auth := &mockAuthProvider{authorizeURL: "https://auth.example/login"}
|
||||
handler := newAuthorizeHandler(auth, &mockMFAProvider{}, &captureEmitter{})
|
||||
params := validAuthorizeParams()
|
||||
params.Set("prompt", "login")
|
||||
params.Set("max_age", age)
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, authorizeRequest(params))
|
||||
if response.Code != http.StatusFound {
|
||||
t.Fatalf("authorize failed: %d", response.Code)
|
||||
}
|
||||
expected, _ := time.ParseDuration(age + "s")
|
||||
if !auth.request.PromptLogin || auth.request.MaxAge == nil || *auth.request.MaxAge != expected {
|
||||
t.Fatalf("fresh login requirements lost before provider: %+v", auth.request)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue