fix: use tenant database auth schema
This commit is contained in:
+9
-210
@@ -1690,21 +1690,7 @@ func (a *adapter) TenantAuthAddRecord(tenantID t.TenantID, uid t.Uid, scheme, un
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.AuthAddRecord(uid, scheme, unique, authLvl, secret, expires)
|
||||
}
|
||||
var exp *time.Time
|
||||
if !expires.IsZero() {
|
||||
exp = &expires
|
||||
}
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
_, err = a.db.ExecContext(ctx,
|
||||
"INSERT INTO im_auth(tenant_id,uname,userid,scheme,authlvl,secret,expires) VALUES(?,?,?,?,?,?,?)",
|
||||
tenantID, unique, store.DecodeUid(uid), scheme, authLvl, secret, exp)
|
||||
if isDupe(err) {
|
||||
return t.ErrDuplicate
|
||||
}
|
||||
return err
|
||||
return a.AuthAddRecord(uid, scheme, unique, authLvl, secret, expires)
|
||||
}
|
||||
|
||||
// TenantAuthDelScheme deletes an authentication scheme only in the selected tenant.
|
||||
@@ -1716,13 +1702,7 @@ func (a *adapter) TenantAuthDelScheme(tenantID t.TenantID, uid t.Uid, scheme str
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.AuthDelScheme(uid, scheme)
|
||||
}
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
_, err = a.db.ExecContext(ctx, "DELETE FROM im_auth WHERE tenant_id=? AND userid=? AND scheme=?",
|
||||
tenantID, store.DecodeUid(uid), scheme)
|
||||
return err
|
||||
return a.AuthDelScheme(uid, scheme)
|
||||
}
|
||||
|
||||
// TenantAuthUpdRecord updates an authentication record only in the selected tenant.
|
||||
@@ -1735,38 +1715,7 @@ func (a *adapter) TenantAuthUpdRecord(tenantID t.TenantID, uid t.Uid, scheme, un
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.AuthUpdRecord(uid, scheme, unique, authLvl, secret, expires)
|
||||
}
|
||||
params := []string{"authlvl=?"}
|
||||
args := []any{authLvl}
|
||||
if unique != "" {
|
||||
params = append(params, "uname=?")
|
||||
args = append(args, unique)
|
||||
}
|
||||
if len(secret) > 0 {
|
||||
params = append(params, "secret=?")
|
||||
args = append(args, secret)
|
||||
}
|
||||
if !expires.IsZero() {
|
||||
params = append(params, "expires=?")
|
||||
args = append(args, expires)
|
||||
}
|
||||
args = append(args, tenantID, store.DecodeUid(uid), scheme)
|
||||
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
resp, err := a.db.ExecContext(ctx, "UPDATE im_auth SET "+strings.Join(params, ",")+
|
||||
" WHERE tenant_id=? AND userid=? AND scheme=?", args...)
|
||||
if isDupe(err) {
|
||||
return t.ErrDuplicate
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count, _ := resp.RowsAffected(); count <= 0 {
|
||||
return t.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
return a.AuthUpdRecord(uid, scheme, unique, authLvl, secret, expires)
|
||||
}
|
||||
|
||||
// TenantAuthGetRecord retrieves a user's authentication record in one tenant.
|
||||
@@ -1780,29 +1729,7 @@ func (a *adapter) TenantAuthGetRecord(tenantID t.TenantID, uid t.Uid, scheme str
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.AuthGetRecord(uid, scheme)
|
||||
}
|
||||
var record struct {
|
||||
Uname string
|
||||
Authlvl auth.Level
|
||||
Secret []byte
|
||||
Expires *time.Time
|
||||
}
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
err = a.db.GetContext(ctx, &record,
|
||||
"SELECT uname,secret,expires,authlvl FROM im_auth WHERE tenant_id=? AND userid=? AND scheme=?",
|
||||
tenantID, store.DecodeUid(uid), scheme)
|
||||
if err == sql.ErrNoRows {
|
||||
err = t.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return "", auth.LevelNone, nil, expires, err
|
||||
}
|
||||
if record.Expires != nil {
|
||||
expires = *record.Expires
|
||||
}
|
||||
return record.Uname, record.Authlvl, record.Secret, expires, nil
|
||||
return a.AuthGetRecord(uid, scheme)
|
||||
}
|
||||
|
||||
// TenantAuthGetUniqueRecord retrieves an authentication record by tenant-local login.
|
||||
@@ -1816,28 +1743,7 @@ func (a *adapter) TenantAuthGetUniqueRecord(tenantID t.TenantID, unique string)
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.AuthGetUniqueRecord(unique)
|
||||
}
|
||||
var record struct {
|
||||
Userid int64
|
||||
Authlvl auth.Level
|
||||
Secret []byte
|
||||
Expires *time.Time
|
||||
}
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
err = a.db.GetContext(ctx, &record,
|
||||
"SELECT userid,secret,expires,authlvl FROM im_auth WHERE tenant_id=? AND uname=?", tenantID, unique)
|
||||
if err == sql.ErrNoRows {
|
||||
return t.ZeroUid, auth.LevelNone, nil, expires, nil
|
||||
}
|
||||
if err != nil {
|
||||
return t.ZeroUid, auth.LevelNone, nil, expires, err
|
||||
}
|
||||
if record.Expires != nil {
|
||||
expires = *record.Expires
|
||||
}
|
||||
return store.EncodeUid(record.Userid), record.Authlvl, record.Secret, expires, nil
|
||||
return a.AuthGetUniqueRecord(unique)
|
||||
}
|
||||
|
||||
// Add user's authentication record
|
||||
@@ -2365,20 +2271,7 @@ func (a *adapter) TenantUserGetByCred(tenantID t.TenantID, method, value string)
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.UserGetByCred(method, value)
|
||||
}
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
var decodedUID int64
|
||||
err = a.db.GetContext(ctx, &decodedUID,
|
||||
"SELECT userid FROM im_credentials WHERE tenant_id=? AND synthetic=?", tenantID, method+":"+value)
|
||||
if err == sql.ErrNoRows {
|
||||
return t.ZeroUid, nil
|
||||
}
|
||||
if err != nil {
|
||||
return t.ZeroUid, err
|
||||
}
|
||||
return store.EncodeUid(decodedUID), nil
|
||||
return a.UserGetByCred(method, value)
|
||||
}
|
||||
|
||||
// UserUnreadCount returns the total number of unread messages in all topics with
|
||||
@@ -4122,64 +4015,7 @@ func (a *adapter) TenantCredUpsert(tenantID t.TenantID, cred *t.Credential) (boo
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.CredUpsert(cred)
|
||||
}
|
||||
ctx, cancel := a.getContextForTx()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
tx, err := a.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
now := t.TimeNow()
|
||||
userID := common.DecodeUidString(cred.User)
|
||||
synth := cred.Method + ":" + cred.Value
|
||||
if !cred.Done {
|
||||
var done bool
|
||||
err = tx.Get(&done, "SELECT done FROM im_credentials WHERE tenant_id=? AND synthetic=?", tenantID, synth)
|
||||
if err == nil {
|
||||
return false, t.ErrDuplicate
|
||||
}
|
||||
if err != sql.ErrNoRows {
|
||||
return false, err
|
||||
}
|
||||
synth = cred.User + ":" + synth
|
||||
_, err = tx.Exec("UPDATE im_credentials SET deletedat=? WHERE tenant_id=? AND userid=? AND method=? AND done=FALSE",
|
||||
now, tenantID, userID, cred.Method)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
res, err := tx.Exec("UPDATE im_credentials SET updatedat=?,deletedat=NULL,resp=?,done=FALSE "+
|
||||
"WHERE tenant_id=? AND synthetic=?", cred.UpdatedAt, cred.Resp, tenantID, synth)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if numRows, _ := res.RowsAffected(); numRows > 0 {
|
||||
return false, tx.Commit()
|
||||
}
|
||||
} else {
|
||||
_, err = tx.Exec("DELETE FROM im_credentials WHERE tenant_id=? AND synthetic=?",
|
||||
tenantID, cred.User+":"+synth)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
_, err = tx.Exec("INSERT INTO im_credentials(tenant_id,createdat,updatedat,method,value,synthetic,userid,resp,done) "+
|
||||
"VALUES(?,?,?,?,?,?,?,?,?)", tenantID, cred.CreatedAt, cred.UpdatedAt, cred.Method, cred.Value,
|
||||
synth, userID, cred.Resp, cred.Done)
|
||||
if isDupe(err) {
|
||||
return true, t.ErrDuplicate
|
||||
}
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
return true, tx.Commit()
|
||||
return a.CredUpsert(cred)
|
||||
}
|
||||
|
||||
func tenantCredDel(tx *sqlx.Tx, tenantID t.TenantID, uid t.Uid, method, value string) error {
|
||||
@@ -4440,22 +4276,7 @@ func (a *adapter) TenantCredGetActive(tenantID t.TenantID, uid t.Uid, method str
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.CredGetActive(uid, method)
|
||||
}
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
var cred t.Credential
|
||||
err = a.db.GetContext(ctx, &cred, "SELECT createdat,updatedat,method,value,resp,done,retries "+
|
||||
"FROM im_credentials WHERE tenant_id=? AND userid=? AND deletedat IS NULL AND method=? AND done=FALSE",
|
||||
tenantID, store.DecodeUid(uid), method)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cred.User = uid.String()
|
||||
return &cred, nil
|
||||
return a.CredGetActive(uid, method)
|
||||
}
|
||||
|
||||
// CredGetAll returns credential records for the given user and method, all or validated only.
|
||||
@@ -4498,29 +4319,7 @@ func (a *adapter) TenantCredGetAll(tenantID t.TenantID, uid t.Uid, method string
|
||||
if tenantAdp != a {
|
||||
return tenantAdp.CredGetAll(uid, method, validatedOnly)
|
||||
}
|
||||
query := "SELECT createdat,updatedat,method,value,resp,done,retries FROM im_credentials " +
|
||||
"WHERE tenant_id=? AND userid=? AND deletedat IS NULL"
|
||||
args := []any{tenantID, store.DecodeUid(uid)}
|
||||
if method != "" {
|
||||
query += " AND method=?"
|
||||
args = append(args, method)
|
||||
}
|
||||
if validatedOnly {
|
||||
query += " AND done=TRUE"
|
||||
}
|
||||
ctx, cancel := a.getContext()
|
||||
if cancel != nil {
|
||||
defer cancel()
|
||||
}
|
||||
var credentials []t.Credential
|
||||
if err := a.db.SelectContext(ctx, &credentials, query, args...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user := uid.String()
|
||||
for i := range credentials {
|
||||
credentials[i].User = user
|
||||
}
|
||||
return credentials, nil
|
||||
return a.CredGetAll(uid, method, validatedOnly)
|
||||
}
|
||||
|
||||
// FileUploads
|
||||
|
||||
@@ -302,10 +302,10 @@ func (a *adapter) TenantUserDelete(id t.TenantID, uid t.Uid, hard bool) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec("DELETE FROM im_auth WHERE tenant_id=? AND userid=?", id, decoded); err != nil {
|
||||
if _, err = tx.Exec("DELETE FROM im_auth WHERE userid=?", decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec("DELETE FROM im_credentials WHERE tenant_id=? AND userid=?", id, decoded); err != nil {
|
||||
if _, err = tx.Exec("DELETE FROM im_credentials WHERE userid=?", decoded); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec("DELETE FROM im_usertags WHERE tenant_id=? AND userid=?", id, decoded); err != nil {
|
||||
@@ -528,9 +528,9 @@ func (a *adapter) TenantUserGetUnvalidated(id t.TenantID, before time.Time, limi
|
||||
}
|
||||
rows, err := a.db.QueryxContext(ctx,
|
||||
"SELECT u.id,IFNULL(SUM(c.done),0) AS total FROM im_users AS u "+
|
||||
"LEFT JOIN im_credentials AS c ON u.tenant_id=c.tenant_id AND u.id=c.userid "+
|
||||
"WHERE u.tenant_id=? AND u.lastseen IS NULL AND u.updatedat<? "+
|
||||
"GROUP BY u.id,u.updatedat HAVING total=0 ORDER BY u.updatedat ASC LIMIT ?", id, before, limit)
|
||||
"LEFT JOIN im_credentials AS c ON u.id=c.userid "+
|
||||
"WHERE u.lastseen IS NULL AND u.updatedat<? "+
|
||||
"GROUP BY u.id,u.updatedat HAVING total=0 ORDER BY u.updatedat ASC LIMIT ?", before, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user