Implement ListTokens

This commit is contained in:
santhoshivan23 2023-06-08 00:39:58 +05:30
parent 99fa964b62
commit 4b73df5335
9 changed files with 157 additions and 22 deletions

View file

@ -21,3 +21,11 @@ type ExtraPublicRoomsProvider interface {
// Rooms returns the extra rooms. This is called on-demand by clients, so cache appropriately.
Rooms() []fclient.PublicRoom
}
type RegistrationToken struct {
Token *string `json:"token"`
UsesAllowed *int32 `json:"uses_allowed"`
Pending *int32 `json:"pending"`
Completed *int32 `json:"completed"`
ExpiryTime *int64 `json:"expiry_time"`
}

View file

@ -8,6 +8,7 @@ import (
"math/rand"
"net/http"
"regexp"
"strconv"
"strings"
"time"
@ -20,6 +21,7 @@ import (
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
clientapi "github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/internal/httputil"
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/setup/config"
@ -101,13 +103,20 @@ func AdminCreateNewRegistrationToken(req *http.Request, cfg *config.ClientAPI, u
string(spec.ErrorInvalidParam),
"expiry_time must not be in the past")
}
pending := 0
completed := 0
pending := int32(0)
completed := int32(0)
// If usesAllowed or expiryTime is 0, it means they are not present in the request. NULL (indicating unlimited uses / no expiration will be persisted in DB)
created, err := userAPI.PerformAdminCreateRegistrationToken(req.Context(), token, usesAllowed, expiryTime)
registrationToken := &clientapi.RegistrationToken{
Token: &token,
UsesAllowed: &usesAllowed,
Pending: &pending,
Completed: &completed,
ExpiryTime: &expiryTime,
}
created, err := userAPI.PerformAdminCreateRegistrationToken(req.Context(), registrationToken)
if err != nil {
return util.MatrixErrorResponse(
http.StatusInternalServerError,
http.StatusBadRequest,
string(spec.ErrorUnknown),
err.Error(),
)
@ -148,6 +157,38 @@ func getReturnValueForUsesAllowed(usesAllowed int32) interface{} {
return usesAllowed
}
func AdminListRegistrationTokens(req *http.Request, cfg *config.ClientAPI, userAPI userapi.ClientUserAPI) util.JSONResponse {
vars, err := httputil.URLDecodeMapValues(mux.Vars(req))
if err != nil {
return util.MatrixErrorResponse(
http.StatusInternalServerError,
string(spec.ErrorInvalidParam),
"unable to parse query params",
)
}
returnAll := true
validQuery, ok := vars["valid"]
if ok {
returnAll = false
}
valid, err := strconv.ParseBool(validQuery)
tokens, err := userAPI.PerformAdminListRegistrationTokens(req.Context(), returnAll, valid)
if err != nil {
return util.MatrixErrorResponse(
http.StatusInternalServerError,
string(spec.ErrorUnknown),
"error fetching registration tokens",
)
}
return util.JSONResponse{
Code: 200,
JSON: map[string]interface{}{
"registration_tokens": tokens,
},
}
}
func getReturnValueExpiryTime(expiryTime int64) interface{} {
if expiryTime == 0 {
return nil

View file

@ -168,11 +168,17 @@ func Setup(
}),
).Methods(http.MethodPost, http.MethodOptions)
dendriteAdminRouter.Handle("/admin/registrationTokens",
httputil.MakeAdminAPI("admin_registration_tokens", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
return AdminListRegistrationTokens(req, cfg, userAPI)
}),
).Methods(http.MethodGet, http.MethodOptions)
dendriteAdminRouter.Handle("/admin/evacuateRoom/{roomID}",
httputil.MakeAdminAPI("admin_evacuate_room", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
return AdminEvacuateRoom(req, rsAPI)
}),
).Methods(http.MethodPost, http.MethodOptions)
).Methods(http.MethodGet, http.MethodOptions)
dendriteAdminRouter.Handle("/admin/evacuateUser/{userID}",
httputil.MakeAdminAPI("admin_evacuate_user", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {

View file

@ -27,6 +27,7 @@ import (
"github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/matrix-org/gomatrixserverlib/spec"
clientapi "github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/pushrules"
)
@ -94,7 +95,8 @@ type ClientUserAPI interface {
QueryPushers(ctx context.Context, req *QueryPushersRequest, res *QueryPushersResponse) error
QueryPushRules(ctx context.Context, userID string) (*pushrules.AccountRuleSets, error)
QueryAccountAvailability(ctx context.Context, req *QueryAccountAvailabilityRequest, res *QueryAccountAvailabilityResponse) error
PerformAdminCreateRegistrationToken(ctx context.Context, token string, usesAllowed int32, expiryTime int64) (bool, error)
PerformAdminCreateRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error)
PerformAdminListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
PerformAccountCreation(ctx context.Context, req *PerformAccountCreationRequest, res *PerformAccountCreationResponse) error
PerformDeviceCreation(ctx context.Context, req *PerformDeviceCreationRequest, res *PerformDeviceCreationResponse) error
PerformDeviceUpdate(ctx context.Context, req *PerformDeviceUpdateRequest, res *PerformDeviceUpdateResponse) error

View file

@ -33,6 +33,7 @@ import (
"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"
clientapi "github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/clientapi/userutil"
"github.com/matrix-org/dendrite/internal/eventutil"
"github.com/matrix-org/dendrite/internal/pushgateway"
@ -63,21 +64,29 @@ type UserInternalAPI struct {
Updater *DeviceListUpdater
}
func (a *UserInternalAPI) PerformAdminCreateRegistrationToken(ctx context.Context, token string, usesAllowed int32, expiryTime int64) (bool, error) {
exists, err := a.DB.RegistrationTokenExists(ctx, token)
func (a *UserInternalAPI) PerformAdminCreateRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error) {
exists, err := a.DB.RegistrationTokenExists(ctx, *registrationToken.Token)
if err != nil {
return false, err
}
if exists {
return false, fmt.Errorf("token: %s already exists", token)
return false, fmt.Errorf("token: %s already exists", *registrationToken.Token)
}
_, err = a.DB.InsertRegistrationToken(ctx, token, usesAllowed, expiryTime)
_, err = a.DB.InsertRegistrationToken(ctx, registrationToken)
if err != nil {
return false, fmt.Errorf("Error creating token: %s"+err.Error(), token)
return false, fmt.Errorf("Error creating token: %s"+err.Error(), *registrationToken.Token)
}
return true, nil
}
func (a *UserInternalAPI) PerformAdminListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error) {
tokens, err := a.DB.ListRegistrationTokens(ctx, returnAll, valid)
if err != nil {
return nil, err
}
return tokens, nil
}
func (a *UserInternalAPI) InputAccountData(ctx context.Context, req *api.InputAccountDataRequest, res *api.InputAccountDataResponse) error {
local, domain, err := gomatrixserverlib.SplitID('@', req.UserID)
if err != nil {

View file

@ -23,6 +23,7 @@ import (
"github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/matrix-org/gomatrixserverlib/spec"
clientapi "github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/pushrules"
"github.com/matrix-org/dendrite/userapi/api"
@ -32,7 +33,8 @@ import (
type RegistrationTokens interface {
RegistrationTokenExists(ctx context.Context, token string) (bool, error)
InsertRegistrationToken(ctx context.Context, token string, usesAllowed int32, expiryTime int64) (bool, error)
InsertRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (bool, error)
ListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
}
type Profile interface {

View file

@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/dendrite/userapi/storage/tables"
)
@ -24,9 +25,13 @@ const selectTokenSQL = "" +
const insertTokenSQL = "" +
"INSERT INTO userapi_registration_tokens (token, uses_allowed, expiry_time, pending, completed) VALUES ($1, $2, $3, $4, $5)"
const listTokensSQL = "" +
"SELECT * FROM userapi_registration_tokens"
type registrationTokenStatements struct {
selectTokenStatement *sql.Stmt
insertTokenStatment *sql.Stmt
insertTokenStatement *sql.Stmt
listTokensStatement *sql.Stmt
}
func NewPostgresRegistrationTokensTable(db *sql.DB) (tables.RegistrationTokensTable, error) {
@ -37,7 +42,8 @@ func NewPostgresRegistrationTokensTable(db *sql.DB) (tables.RegistrationTokensTa
}
return s, sqlutil.StatementList{
{&s.selectTokenStatement, selectTokenSQL},
{&s.insertTokenStatment, insertTokenSQL},
{&s.insertTokenStatement, insertTokenSQL},
{&s.listTokensStatement, listTokensSQL},
}.Prepare(db)
}
@ -54,11 +60,15 @@ func (s *registrationTokenStatements) RegistrationTokenExists(ctx context.Contex
return true, nil
}
func (s *registrationTokenStatements) InsertRegistrationToken(ctx context.Context, tx *sql.Tx, token string, usesAllowed int32, expiryTime int64) (bool, error) {
stmt := sqlutil.TxStmt(tx, s.insertTokenStatment)
pending := 0
completed := 0
_, err := stmt.ExecContext(ctx, token, nullIfZeroInt32(usesAllowed), nullIfZero(expiryTime), pending, completed)
func (s *registrationTokenStatements) InsertRegistrationToken(ctx context.Context, tx *sql.Tx, registrationToken *api.RegistrationToken) (bool, error) {
stmt := sqlutil.TxStmt(tx, s.insertTokenStatement)
_, err := stmt.ExecContext(
ctx,
*registrationToken.Token,
nullIfZeroInt32(*registrationToken.UsesAllowed),
nullIfZero(*registrationToken.ExpiryTime),
*registrationToken.Pending,
*registrationToken.Completed)
if err != nil {
return false, err
}
@ -78,3 +88,53 @@ func nullIfZeroInt32(value int32) interface{} {
}
return value
}
func (s *registrationTokenStatements) ListRegistrationTokens(ctx context.Context, tx *sql.Tx, returnAll bool, valid bool) ([]api.RegistrationToken, error) {
var stmt *sql.Stmt
var tokens []api.RegistrationToken
var tokenString sql.NullString
var pending, completed, usesAllowed sql.NullInt32
var expiryTime sql.NullInt64
if returnAll {
stmt = s.listTokensStatement
} else if valid {
// TODO: Statement to Get All Valid Tokens
} else {
// TODO: Statement to Get All Invalid Tokens
}
rows, err := stmt.QueryContext(ctx)
if err != nil {
return tokens, err
}
for rows.Next() {
err = rows.Scan(&tokenString, &pending, &completed, &usesAllowed, &expiryTime)
if err != nil {
return tokens, err
}
tokenMap := api.RegistrationToken{
Token: &tokenString.String,
Pending: &pending.Int32,
Completed: &pending.Int32,
UsesAllowed: getReturnValueForInt32(usesAllowed),
ExpiryTime: getReturnValueForInt64(expiryTime),
}
tokens = append(tokens, tokenMap)
}
return tokens, nil
}
func getReturnValueForInt32(value sql.NullInt32) *int32 {
if value.Valid {
returnValue := value.Int32
return &returnValue
}
return nil
}
func getReturnValueForInt64(value sql.NullInt64) *int64 {
if value.Valid {
returnValue := value.Int64
return &returnValue
}
return nil
}

View file

@ -31,6 +31,7 @@ import (
"github.com/matrix-org/gomatrixserverlib/spec"
"golang.org/x/crypto/bcrypt"
clientapi "github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/internal/pushrules"
"github.com/matrix-org/dendrite/internal/sqlutil"
@ -83,14 +84,18 @@ func (d *Database) RegistrationTokenExists(ctx context.Context, token string) (b
return d.RegistrationTokens.RegistrationTokenExists(ctx, nil, token)
}
func (d *Database) InsertRegistrationToken(ctx context.Context, token string, usesAllowed int32, expiryTime int64) (created bool, err error) {
func (d *Database) InsertRegistrationToken(ctx context.Context, registrationToken *clientapi.RegistrationToken) (created bool, err error) {
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
created, err = d.RegistrationTokens.InsertRegistrationToken(ctx, txn, token, usesAllowed, expiryTime)
created, err = d.RegistrationTokens.InsertRegistrationToken(ctx, txn, registrationToken)
return err
})
return
}
func (d *Database) ListRegistrationTokens(ctx context.Context, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error) {
return d.RegistrationTokens.ListRegistrationTokens(ctx, nil, returnAll, valid)
}
// GetAccountByPassword returns the account associated with the given localpart and password.
// Returns sql.ErrNoRows if no account exists which matches the given localpart.
func (d *Database) GetAccountByPassword(

View file

@ -25,13 +25,15 @@ import (
"github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/matrix-org/gomatrixserverlib/spec"
clientapi "github.com/matrix-org/dendrite/clientapi/api"
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
"github.com/matrix-org/dendrite/userapi/types"
)
type RegistrationTokensTable interface {
RegistrationTokenExists(ctx context.Context, txn *sql.Tx, token string) (bool, error)
InsertRegistrationToken(ctx context.Context, txn *sql.Tx, token string, usesAllowed int32, expiryTime int64) (bool, error)
InsertRegistrationToken(ctx context.Context, txn *sql.Tx, registrationToken *clientapi.RegistrationToken) (bool, error)
ListRegistrationTokens(ctx context.Context, txn *sql.Tx, returnAll bool, valid bool) ([]clientapi.RegistrationToken, error)
}
type AccountDataTable interface {