Enable CLI language-aware editing and reuse OIDC authentication
This commit is contained in:
1 parent
5821254535
commit
93f4ecb8f4
11 files changed
+264
-84
No files matched your search
@@ -124,17 +124,18 @@ func cliChanged(result sql.Result, err error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// The browser token, confirmation code and current membership are all checked
|
||||
// at mutation time. Attempts are spent before checking TOTP to bound guessing.
|
||||
func (s *Store) DecideCLIAuth(ctx context.Context, domain, origin, id, browser, code, totp string, approve bool) error {
|
||||
// The verified OIDC callback is the only production caller of BindCLIAuth.
|
||||
// Authenticate at the provider, then require explicit local consent, not a
|
||||
// second local TOTP challenge. This does not claim the provider performed MFA.
|
||||
func (s *Store) DecideCLIAuth(ctx context.Context, domain, origin, id, browser, code, _ string, approve bool) error {
|
||||
if s.ReadOnly || !strings.HasPrefix(browser, "pw_browser_") {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
var principal, expectedCode string
|
||||
var expectedCode string
|
||||
err := s.DB.QueryRowContext(ctx, `UPDATE auth_cli_requests SET attempts=attempts+1
|
||||
WHERE id=? AND domain_uuid=? AND origin=? AND state='pending' AND browser_hash=? AND attempts<5 AND expires_ms>?
|
||||
RETURNING principal_uuid,code_hash`, id, domain, origin, hashAuthToken(browser), now).Scan(&principal, &expectedCode)
|
||||
RETURNING code_hash`, id, domain, origin, hashAuthToken(browser), now).Scan(&expectedCode)
|
||||
if err != nil {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
@@ -143,17 +144,6 @@ func (s *Store) DecideCLIAuth(ctx context.Context, domain, origin, id, browser,
|
||||
if hashAuthToken(strings.ToUpper(strings.ReplaceAll(strings.TrimSpace(code), "-", ""))) != expectedCode {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
// Query errors must not silently bypass enrolled MFA.
|
||||
var has int
|
||||
if err := s.DB.QueryRowContext(ctx, `SELECT count(*) FROM auth_credentials WHERE principal_uuid=? AND kind='totp' AND enabled=1`, principal).Scan(&has); err != nil {
|
||||
return err
|
||||
}
|
||||
if has > 0 {
|
||||
if err := s.VerifyTOTP(principal, totp); err != nil {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
level = 1
|
||||
}
|
||||
}
|
||||
state := "denied"
|
||||
if approve {
|
||||
@@ -224,7 +214,7 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver
|
||||
SELECT ?,?,p.uuid,a.domain_uuid,a.auth_time_ms,min(?,a.expires_at_ms),?,max(a.mfa_level,g.mfa_level)
|
||||
FROM auth_cli_requests g JOIN auth_sessions a ON a.token_hash=g.browser_hash JOIN principals p ON p.uuid=a.principal_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=a.domain_uuid
|
||||
WHERE a.token_hash=? AND a.domain_uuid=? AND a.revoked_at_ms=0 AND a.expires_at_ms>? AND p.state IN ('active','readonly') AND m.state='active'
|
||||
AND g.id=? AND (g.mfa_level>0 OR NOT EXISTS(SELECT 1 FROM auth_credentials WHERE principal_uuid=p.uuid AND kind='totp' AND enabled=1))
|
||||
AND g.id=?
|
||||
RETURNING principal_uuid,expires_at_ms`, uuid.NewString(), hashAuthToken(token), now+lifetime, now, browser, domain, now, id).Scan(&username, &expires)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
err = ErrAccessDenied
|
||||
|
||||
@@ -88,9 +88,9 @@ func TestCLIAuthExchangeIsAtomicAndSingleUse(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCLIAuthBoundariesAndRevocation(t *testing.T) {
|
||||
for _, mode := range []string{"domain", "origin", "verifier", "device", "expired", "denied", "suspended", "membership", "browser revoked", "new totp"} {
|
||||
for _, mode := range []string{"domain", "origin", "verifier", "device", "expired", "denied", "suspended", "membership", "browser revoked"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
s, principal, browser := cliAuthFixture(t)
|
||||
s, _, browser := cliAuthFixture(t)
|
||||
ctx := context.Background()
|
||||
verifier := strings.Repeat("b", 43)
|
||||
id, device, code, err := s.StartCLIAuth(ctx, "domain", cliTestOrigin, hashAuthToken(verifier))
|
||||
@@ -122,11 +122,6 @@ func TestCLIAuthBoundariesAndRevocation(t *testing.T) {
|
||||
query = `UPDATE domain_memberships SET state='suspended'`
|
||||
case "browser revoked":
|
||||
query = `UPDATE auth_sessions SET revoked_at_ms=1`
|
||||
case "new totp":
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,created_at_ms,updated_at_ms,enabled) VALUES('totp',?,'totp',?,'invalid',1,1,1)`, principal, principal)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if query != "" {
|
||||
if _, err := s.DB.Exec(query); err != nil {
|
||||
@@ -202,7 +197,7 @@ func TestCLIAuthSessionFailureRollsBackConsumption(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIAuthRequiresTOTPAndPreservesLevel(t *testing.T) {
|
||||
func TestCLIAuthOIDCDoesNotRepeatLocalTOTPOrInventMFA(t *testing.T) {
|
||||
s, principal, browser := cliAuthFixture(t)
|
||||
ctx := context.Background()
|
||||
verifier := strings.Repeat("t", 43)
|
||||
@@ -232,14 +227,7 @@ func TestCLIAuthRequiresTOTPAndPreservesLevel(t *testing.T) {
|
||||
if err := s.BindCLIAuth(ctx, "domain", cliTestOrigin, id, browser); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.DecideCLIAuth(ctx, "domain", cliTestOrigin, id, browser, userCode, "", true); err == nil {
|
||||
t.Fatal("TOTP bypass")
|
||||
}
|
||||
code, err = totpCode(secret, time.Now().Unix()/30+1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.DecideCLIAuth(ctx, "domain", cliTestOrigin, id, browser, userCode, code, true); err != nil {
|
||||
if err := s.DecideCLIAuth(ctx, "domain", cliTestOrigin, id, browser, userCode, "", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, _, _, err := s.ExchangeCLIAuth(ctx, "domain", cliTestOrigin, device, verifier)
|
||||
@@ -247,7 +235,29 @@ func TestCLIAuthRequiresTOTPAndPreservesLevel(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var level int
|
||||
if err := s.DB.QueryRow(`SELECT mfa_level FROM auth_sessions WHERE token_hash=?`, hashAuthToken(token)).Scan(&level); err != nil || level != 0 {
|
||||
t.Fatalf("OIDC must not invent local MFA: %d %v", level, err)
|
||||
}
|
||||
// Preserve an actually verified level from the bound browser session.
|
||||
strongBrowser, err := s.CreateBrowserAuthSession("domain", principal, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id, device, userCode, err = s.StartCLIAuth(ctx, "domain", cliTestOrigin, hashAuthToken(verifier))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.BindCLIAuth(ctx, "domain", cliTestOrigin, id, strongBrowser); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.DecideCLIAuth(ctx, "domain", cliTestOrigin, id, strongBrowser, userCode, "", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, _, _, err = s.ExchangeCLIAuth(ctx, "domain", cliTestOrigin, device, verifier)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.DB.QueryRow(`SELECT mfa_level FROM auth_sessions WHERE token_hash=?`, hashAuthToken(token)).Scan(&level); err != nil || level != 1 {
|
||||
t.Fatalf("MFA not preserved: %d %v", level, err)
|
||||
t.Fatalf("verified MFA level lost: %d %v", level, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user