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
|
||||
|
||||
Reference in new issue
Block a user