feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
+64
-46
@@ -1,12 +1,14 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
@@ -63,8 +65,14 @@ func (s *Store) BeginTOTPEnrollment(principalUUID string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,parameters_json,enabled,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,'{}',0,?,?) ON CONFLICT(kind,identifier) DO UPDATE SET secret_hash=excluded.secret_hash,parameters_json='{}',enabled=0,updated_at_ms=excluded.updated_at_ms`, uuid.NewString(), principalUUID, "totp-pending", principalUUID, encrypted, now, now)
|
||||
return secret, err
|
||||
err = s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,parameters_json,enabled,created_at_ms,updated_at_ms) VALUES(`+s.sqlValues(9)+`) ON CONFLICT(kind,identifier) DO UPDATE SET secret_hash=excluded.secret_hash,parameters_json='{}',enabled=0,updated_at_ms=excluded.updated_at_ms`, uuid.NewString(), principalUUID, "totp-pending", principalUUID, encrypted, "{}", 0, now, now)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return secret, nil
|
||||
}
|
||||
func totpCode(secret string, counter int64) (string, error) {
|
||||
key, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(secret))
|
||||
@@ -95,9 +103,9 @@ func verifyTOTPCode(secret, code string, last int64) (int64, bool) {
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
func (s *Store) confirmTOTP(principalUUID, kind, code string) (int64, error) {
|
||||
func (s *Store) confirmTOTP(ctx context.Context, tx *sql.Tx, principalUUID, kind, code string) (int64, error) {
|
||||
var encrypted, params string
|
||||
err := s.DB.QueryRow(`SELECT secret_hash,parameters_json FROM auth_credentials WHERE principal_uuid=? AND kind=? AND identifier=?`, principalUUID, kind, principalUUID).Scan(&encrypted, ¶ms)
|
||||
err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT secret_hash,parameters_json FROM auth_credentials WHERE principal_uuid=%s AND kind=%s AND identifier=%s AND (kind='totp-pending' OR enabled=1)`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), principalUUID, kind, principalUUID).Scan(&encrypted, ¶ms)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -108,7 +116,9 @@ func (s *Store) confirmTOTP(principalUUID, kind, code string) (int64, error) {
|
||||
var state struct {
|
||||
LastCounter int64 `json:"last_counter"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(params), &state)
|
||||
if json.Unmarshal([]byte(params), &state) != nil {
|
||||
return 0, ErrAccessDenied
|
||||
}
|
||||
counter, ok := verifyTOTPCode(string(plain), code, state.LastCounter)
|
||||
if !ok {
|
||||
return 0, ErrAccessDenied
|
||||
@@ -116,31 +126,37 @@ func (s *Store) confirmTOTP(principalUUID, kind, code string) (int64, error) {
|
||||
return counter, nil
|
||||
}
|
||||
func (s *Store) ConfirmTOTPEnrollment(principalUUID, code string) error {
|
||||
counter, err := s.confirmTOTP(principalUUID, "totp-pending", code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
_, _ = tx.Exec(`DELETE FROM auth_credentials WHERE principal_uuid=? AND kind='totp'`, principalUUID)
|
||||
_, err = tx.Exec(`UPDATE auth_credentials SET kind='totp',enabled=1,parameters_json=?,updated_at_ms=? WHERE principal_uuid=? AND kind='totp-pending'`, string(params), time.Now().UnixMilli(), principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
return s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
counter, err := s.confirmTOTP(ctx, tx, principalUUID, "totp-pending", code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind='totp'`, principalUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE auth_credentials SET kind='totp',enabled=1,parameters_json=%s,updated_at_ms=%s WHERE principal_uuid=%s AND kind='totp-pending'`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), string(params), time.Now().UnixMilli(), principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != 1 {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
func (s *Store) HasTOTP(principalUUID string) bool {
|
||||
var exists int
|
||||
_ = s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM auth_credentials WHERE principal_uuid=? AND kind='totp' AND enabled=1)`, principalUUID).Scan(&exists)
|
||||
return exists != 0
|
||||
var exists bool
|
||||
_ = s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind='totp' AND enabled=1)`, principalUUID).Scan(&exists)
|
||||
return exists
|
||||
}
|
||||
func (s *Store) PendingTOTPSecret(principalUUID string) string {
|
||||
var encrypted string
|
||||
if s.DB.QueryRow(`SELECT secret_hash FROM auth_credentials WHERE principal_uuid=? AND kind='totp-pending'`, principalUUID).Scan(&encrypted) != nil {
|
||||
if s.DB.QueryRow(`SELECT secret_hash FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind='totp-pending'`, principalUUID).Scan(&encrypted) != nil {
|
||||
return ""
|
||||
}
|
||||
plain, err := s.decryptAuthSecret(encrypted)
|
||||
@@ -150,24 +166,23 @@ func (s *Store) PendingTOTPSecret(principalUUID string) string {
|
||||
return string(plain)
|
||||
}
|
||||
func (s *Store) VerifyTOTP(principalUUID, code string) error {
|
||||
counter, err := s.confirmTOTP(principalUUID, "totp", code)
|
||||
if err != nil {
|
||||
return s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
// Validation and counter advancement share the same lock on both DBs.
|
||||
// No JSON SQL dialect is needed, and enrollment cannot race verification.
|
||||
counter, err := s.confirmTOTP(ctx, tx, principalUUID, "totp", code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
_, err = tx.ExecContext(ctx, fmt.Sprintf(`UPDATE auth_credentials SET parameters_json=%s,updated_at_ms=%s WHERE principal_uuid=%s AND kind='totp' AND enabled=1`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), string(params), time.Now().UnixMilli(), principalUUID)
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
result, err := s.DB.Exec(`UPDATE auth_credentials SET parameters_json=?,updated_at_ms=? WHERE principal_uuid=? AND kind='totp' AND CAST(COALESCE(json_extract(parameters_json,'$.last_counter'),0) AS INTEGER)<?`, string(params), time.Now().UnixMilli(), principalUUID, counter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, _ := result.RowsAffected()
|
||||
if changed != 1 {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
func (s *Store) DisableTOTP(principalUUID string) error {
|
||||
_, err := s.DB.Exec(`DELETE FROM auth_credentials WHERE principal_uuid=? AND kind IN ('totp','totp-pending')`, principalUUID)
|
||||
return err
|
||||
return s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `DELETE FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind IN ('totp','totp-pending')`, principalUUID)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func TOTPURI(secret, account, issuer string) string {
|
||||
@@ -186,8 +201,11 @@ func (s *Store) CreateTOTPLoginChallengeForLogin(domainUUID, principalUUID strin
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
payload, _ := json.Marshal(map[string]any{"principal_uuid": principalUUID, "remember": remember})
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, uuid.NewString(), domainUUID, principalUUID, "totp-login", hash, string(payload), now+5*60*1000, now)
|
||||
return token, err
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(`+s.sqlValues(8)+`)`, uuid.NewString(), domainUUID, principalUUID, "totp-login", hash, string(payload), now+5*60*1000, now)
|
||||
if err != nil {
|
||||
return "", s.sqlError("TOTP login challenge", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
func (s *Store) CompleteTOTPLoginChallenge(domainUUID, token, code string) (string, error) {
|
||||
id, _, err := s.CompleteTOTPLoginChallengeForLogin(domainUUID, token, code)
|
||||
@@ -200,7 +218,7 @@ func (s *Store) CompleteTOTPLoginChallengeForLogin(domainUUID, token, code strin
|
||||
var challengeUUID, principalUUID, payload string
|
||||
var expires int64
|
||||
var attempts int
|
||||
err := s.DB.QueryRow(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count,payload_json FROM auth_challenges WHERE domain_uuid=? AND kind='totp-login' AND token_hash=? AND consumed_at_ms=0`, domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts, &payload)
|
||||
err := s.DB.QueryRow(fmt.Sprintf(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count,payload_json FROM auth_challenges WHERE domain_uuid=%s AND kind='totp-login' AND token_hash=%s AND consumed_at_ms=0`, s.sqlParam(1), s.sqlParam(2)), domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts, &payload)
|
||||
if err != nil || expires < time.Now().UnixMilli() || attempts >= 5 {
|
||||
return "", false, ErrAccessDenied
|
||||
}
|
||||
@@ -211,12 +229,12 @@ func (s *Store) CompleteTOTPLoginChallengeForLogin(domainUUID, token, code strin
|
||||
return "", false, ErrAccessDenied
|
||||
}
|
||||
if err = s.VerifyTOTP(principalUUID, code); err != nil {
|
||||
_, _ = s.DB.Exec(`UPDATE auth_challenges SET attempt_count=attempt_count+1,consumed_at_ms=CASE WHEN attempt_count+1>=5 THEN ? ELSE consumed_at_ms END WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID)
|
||||
_, _ = s.DB.Exec(fmt.Sprintf(`UPDATE auth_challenges SET attempt_count=attempt_count+1,consumed_at_ms=CASE WHEN attempt_count+1>=5 THEN %s ELSE consumed_at_ms END WHERE uuid=%s AND consumed_at_ms=0`, s.sqlParam(1), s.sqlParam(2)), time.Now().UnixMilli(), challengeUUID)
|
||||
return "", false, err
|
||||
}
|
||||
result, err := s.DB.Exec(`UPDATE auth_challenges SET consumed_at_ms=? WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID)
|
||||
result, err := s.DB.Exec(fmt.Sprintf(`UPDATE auth_challenges SET consumed_at_ms=%s WHERE uuid=%s AND consumed_at_ms=0 AND attempt_count<5 AND expires_at_ms>%s`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), time.Now().UnixMilli(), challengeUUID, time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
return "", false, s.sqlError("TOTP challenge consumption", err)
|
||||
}
|
||||
changed, _ := result.RowsAffected()
|
||||
if changed != 1 {
|
||||
|
||||
Reference in new issue
Block a user