feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PendingOIDCLogin struct {
|
||||
Provider string
|
||||
Subject string
|
||||
Email string
|
||||
DisplayName string
|
||||
EmailVerified bool
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (s *Store) CreateScopedOIDCState(ctx context.Context, state string, userID *uint64, mode, verifier, nonce, provider, sessionToken string, expires time.Time) error {
|
||||
hash := sha256.Sum256([]byte(state))
|
||||
var parent any
|
||||
if sessionToken != "" {
|
||||
value := sha256.Sum256([]byte(sessionToken))
|
||||
parent = value[:]
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx, `INSERT INTO oidc_states(state_hash,user_id,mode,verifier,nonce,expires_at,provider_key,session_token_hash) VALUES(?,?,?,?,?,?,?,?)`, hash[:], userID, mode, verifier, nonce, expires, provider, parent)
|
||||
return err
|
||||
}
|
||||
func (s *Store) CreatePendingOIDCLogin(ctx context.Context, token string, p PendingOIDCLogin) error {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
_, err := s.db.ExecContext(ctx, `DELETE FROM oidc_pending_logins WHERE expires_at<=?`, time.Now().UTC())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, `INSERT INTO oidc_pending_logins(token_hash,provider,subject,email,display_name,email_verified,expires_at) VALUES(?,?,?,?,?,?,?)`, hash[:], p.Provider, p.Subject, p.Email, p.DisplayName, p.EmailVerified, p.ExpiresAt)
|
||||
return err
|
||||
}
|
||||
func (s *Store) PendingOIDCLogin(ctx context.Context, token string) (PendingOIDCLogin, error) {
|
||||
var p PendingOIDCLogin
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
err := s.db.QueryRowContext(ctx, `SELECT provider,subject,email,display_name,email_verified,expires_at FROM oidc_pending_logins WHERE token_hash=? AND expires_at>?`, hash[:], time.Now().UTC()).Scan(&p.Provider, &p.Subject, &p.Email, &p.DisplayName, &p.EmailVerified, &p.ExpiresAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
err = ErrNotFound
|
||||
}
|
||||
return p, err
|
||||
}
|
||||
|
||||
// CompletePendingOIDC commits the identity and optional ordinary account together.
|
||||
// A consumed ticket, duplicate identity or duplicate email cannot leave an orphan account.
|
||||
func (s *Store) CompletePendingOIDC(ctx context.Context, token, expectedProvider string, targetID uint64, input *NewTenant) (uint64, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
var p PendingOIDCLogin
|
||||
err = tx.QueryRowContext(ctx, `SELECT provider,subject,email,display_name,email_verified,expires_at FROM oidc_pending_logins WHERE token_hash=? FOR UPDATE`, hash[:]).Scan(&p.Provider, &p.Subject, &p.Email, &p.DisplayName, &p.EmailVerified, &p.ExpiresAt)
|
||||
if err != nil || p.Provider != expectedProvider || !p.ExpiresAt.After(time.Now().UTC()) {
|
||||
return 0, ErrNotFound
|
||||
}
|
||||
if input != nil {
|
||||
if !p.EmailVerified || p.Email != input.Email {
|
||||
return 0, errors.New("SSO 邮箱尚未验证")
|
||||
}
|
||||
// The supplied password is already an irreversible hash of an unguessable secret.
|
||||
result, e := tx.ExecContext(ctx, `INSERT INTO tenants(slug,name,plan_id) VALUES(?,?,?)`, input.Slug, input.Name, input.PlanID)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
namespace, e := result.LastInsertId()
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
result, e = tx.ExecContext(ctx, `INSERT INTO users(tenant_id,login_name,email,display_name,password_hash,role) VALUES(?,?,?,?,?,'tenant')`, namespace, input.Email, input.Email, input.DisplayName, input.Password)
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
id, e := result.LastInsertId()
|
||||
if e != nil {
|
||||
return 0, e
|
||||
}
|
||||
targetID = uint64(id)
|
||||
}
|
||||
var role, status string
|
||||
if err = tx.QueryRowContext(ctx, `SELECT u.role,u.status FROM users u LEFT JOIN tenants t ON t.id=u.tenant_id WHERE u.id=? AND (u.tenant_id IS NULL OR (t.status='active' AND t.deleted_at IS NULL)) FOR UPDATE`, targetID).Scan(&role, &status); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if role == "host" || status != "active" {
|
||||
return 0, errors.New("该账号不能绑定 SSO")
|
||||
}
|
||||
// Never silently replace an existing binding of the target account.
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO sso_identities(user_id,provider,subject,email) VALUES(?,?,?,?)`, targetID, p.Provider, p.Subject, p.Email); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM oidc_pending_logins WHERE token_hash=?`, hash[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return targetID, nil
|
||||
}
|
||||
|
||||
func (s *Store) ActiveOIDCBindSession(ctx context.Context, hash []byte, userID uint64) bool {
|
||||
if len(hash) != 32 {
|
||||
return false
|
||||
}
|
||||
var id uint64
|
||||
if s.db.QueryRowContext(ctx, `SELECT user_id FROM sessions WHERE token_hash=? AND user_id=? AND expires_at>NOW()`, hash, userID).Scan(&id) != nil {
|
||||
return false
|
||||
}
|
||||
u, e := s.UserByID(ctx, id)
|
||||
return e == nil && u.Role != "host"
|
||||
}
|
||||
Reference in new issue
Block a user