mirror of
https://github.com/openziti/zrok.git
synced 2024-11-07 08:44:14 +01:00
pr comments
This commit is contained in:
parent
e5b749be71
commit
0734e7b511
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/jaevor/go-nanoid"
|
||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/invite"
|
||||
@ -12,36 +11,32 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(newGenerateCommand().cmd)
|
||||
adminCmd.AddCommand(newGenerateCommand().cmd)
|
||||
}
|
||||
|
||||
type generateCommand struct {
|
||||
cmd *cobra.Command
|
||||
amount int
|
||||
}
|
||||
|
||||
func newGenerateCommand() *generateCommand {
|
||||
cmd := &cobra.Command{
|
||||
Use: "generate <optional-amount>",
|
||||
Use: "generate",
|
||||
Short: "Generate invite tokens (default: 5)",
|
||||
Args: cobra.RangeArgs(0, 1),
|
||||
Args: cobra.ExactArgs(0),
|
||||
}
|
||||
command := &generateCommand{cmd: cmd}
|
||||
cmd.Run = command.run
|
||||
|
||||
cmd.Flags().IntVar(&command.amount, "amount", 5, "Amount of tokens to generate")
|
||||
|
||||
return command
|
||||
}
|
||||
|
||||
func (cmd *generateCommand) run(_ *cobra.Command, args []string) {
|
||||
var iterations int64 = 5
|
||||
if len(args) == 1 {
|
||||
i, err := strconv.ParseInt(args[0], 10, 64)
|
||||
if err != nil {
|
||||
showError("unable to parse amount", err)
|
||||
}
|
||||
iterations = i
|
||||
}
|
||||
var err error
|
||||
tokens := make([]string, iterations)
|
||||
for i := 0; i < int(iterations); i++ {
|
||||
tokens := make([]string, cmd.amount)
|
||||
for i := 0; i < int(cmd.amount); i++ {
|
||||
tokens[i], err = createToken()
|
||||
if err != nil {
|
||||
showError("error creating token", err)
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/account"
|
||||
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
||||
"github.com/openziti-test-kitchen/zrok/util"
|
||||
@ -16,6 +17,7 @@ func init() {
|
||||
|
||||
type inviteCommand struct {
|
||||
cmd *cobra.Command
|
||||
Token string
|
||||
}
|
||||
|
||||
func newInviteCommand() *inviteCommand {
|
||||
@ -24,8 +26,12 @@ func newInviteCommand() *inviteCommand {
|
||||
Short: "Invite a new user to zrok",
|
||||
Args: cobra.ExactArgs(0),
|
||||
}
|
||||
|
||||
command := &inviteCommand{cmd: cmd}
|
||||
cmd.Run = command.run
|
||||
|
||||
cmd.Flags().StringVar(&command.Token, "token", "", "Invite token required when Zrok running in token store mode")
|
||||
|
||||
return command
|
||||
}
|
||||
|
||||
@ -55,6 +61,7 @@ func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) {
|
||||
req := account.NewInviteParams()
|
||||
req.Body = &rest_model_zrok.InviteRequest{
|
||||
Email: email,
|
||||
Token: cmd.Token,
|
||||
}
|
||||
_, err = zrok.Account.Invite(req)
|
||||
if err != nil {
|
||||
|
@ -39,24 +39,22 @@ func (self *inviteHandler) Handle(params account.InviteParams) middleware.Respon
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if self.cfg.Registration.TokenStrategy == "store" {
|
||||
invite, err := str.GetInvite(tx)
|
||||
invite, err := str.GetInviteByToken(params.Body.Token, tx)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return account.NewInviteInternalServerError()
|
||||
return account.NewInviteBadRequest()
|
||||
}
|
||||
invite.Status = store.INVITE_STATUS_TAKEN
|
||||
if err := str.UpdateInvite(invite, tx); err != nil {
|
||||
if err := str.DeleteInvite(invite.Id, tx); err != nil {
|
||||
logrus.Error(err)
|
||||
return account.NewInviteInternalServerError()
|
||||
}
|
||||
token = invite.Token
|
||||
} else {
|
||||
}
|
||||
|
||||
token, err = createToken()
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return account.NewInviteInternalServerError()
|
||||
}
|
||||
}
|
||||
ar := &store.AccountRequest{
|
||||
Token: token,
|
||||
Email: params.Body.Email,
|
||||
@ -121,7 +119,6 @@ func (handler *inviteGenerateHandler) Handle(params invite.InviteGenerateParams)
|
||||
for i, token := range params.Body.Tokens {
|
||||
invites[i] = &store.Invite{
|
||||
Token: token,
|
||||
Status: store.INVITE_STATUS_UNUSED,
|
||||
}
|
||||
}
|
||||
tx, err := str.Begin()
|
||||
|
@ -11,24 +11,15 @@ import (
|
||||
type Invite struct {
|
||||
Model
|
||||
Token string
|
||||
Status string `db:"token_status"`
|
||||
}
|
||||
|
||||
const (
|
||||
INVITE_STATUS_UNUSED = "UNUSED"
|
||||
INVITE_STATUS_TAKEN = "TAKEN"
|
||||
)
|
||||
|
||||
func (str *Store) CreateInvites(invites []*Invite, tx *sqlx.Tx) error {
|
||||
sql := "insert into invites (token, token_status) values %s"
|
||||
invs := make([]any, len(invites)*2)
|
||||
sql := "insert into invites (token) values %s"
|
||||
invs := make([]any, len(invites))
|
||||
queries := make([]string, len(invites))
|
||||
ct := 1
|
||||
for i, inv := range invites {
|
||||
invs[i] = inv.Token
|
||||
invs[i+1] = inv.Status
|
||||
queries[i] = fmt.Sprintf("($%d, $%d)", ct, ct+1)
|
||||
ct = ct + 2
|
||||
queries[i] = fmt.Sprintf("($%d)", i+1)
|
||||
}
|
||||
stmt, err := tx.Prepare(fmt.Sprintf(sql, strings.Join(queries, ",")))
|
||||
if err != nil {
|
||||
@ -40,22 +31,34 @@ func (str *Store) CreateInvites(invites []*Invite, tx *sqlx.Tx) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (str *Store) GetInvite(tx *sqlx.Tx) (*Invite, error) {
|
||||
func (str *Store) GetInviteByToken(token string, tx *sqlx.Tx) (*Invite, error) {
|
||||
invite := &Invite{}
|
||||
if err := tx.QueryRowx("select * from invites where token_status = $1 limit 1", INVITE_STATUS_UNUSED).StructScan(invite); err != nil {
|
||||
if err := tx.QueryRowx("select * from invites where token = $1", token).StructScan(invite); err != nil {
|
||||
return nil, errors.Wrap(err, "error getting unused invite")
|
||||
}
|
||||
return invite, nil
|
||||
}
|
||||
|
||||
func (str *Store) UpdateInvite(invite *Invite, tx *sqlx.Tx) error {
|
||||
stmt, err := tx.Prepare("update invites set token = $1, token_status = $2")
|
||||
stmt, err := tx.Prepare("update invites set token = $1")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error perparing invites update statement")
|
||||
}
|
||||
_, err = stmt.Exec(invite.Token, invite.Status)
|
||||
_, err = stmt.Exec(invite.Token)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error executing invites update statement")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (str *Store) DeleteInvite(id int, tx *sqlx.Tx) error {
|
||||
stmt, err := tx.Prepare("delete from invites where id = $1")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error preparing invites delete statement")
|
||||
}
|
||||
_, err = stmt.Exec(id)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error executing invites delete statement")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -7,10 +7,8 @@
|
||||
create table invites (
|
||||
id serial primary key,
|
||||
token varchar(32) not null unique,
|
||||
token_status varchar(1024) not null unique,
|
||||
created_at timestamp not null default(current_timestamp),
|
||||
updated_at timestamp not null default(current_timestamp),
|
||||
created_at timestamptz not null default(current_timestamp),
|
||||
updated_at timestamptz not null default(current_timestamp),
|
||||
|
||||
constraint chk_token check(token <> ''),
|
||||
constraint chk_status check(token_status <> '')
|
||||
constraint chk_token check(token <> '')
|
||||
);
|
@ -5,12 +5,10 @@
|
||||
---
|
||||
|
||||
create table invites (
|
||||
id serial primary key,
|
||||
token varchar(32) not null unique,
|
||||
token_status varchar(1024) not null unique,
|
||||
created_at timestamp not null default(current_timestamp),
|
||||
updated_at timestamp not null default(current_timestamp),
|
||||
id integer primary key,
|
||||
token string not null unique,
|
||||
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||
updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
|
||||
|
||||
constraint chk_token check(token <> ''),
|
||||
constraint chk_status check(token_status <> '')
|
||||
);
|
@ -19,6 +19,9 @@ type InviteRequest struct {
|
||||
|
||||
// email
|
||||
Email string `json:"email,omitempty"`
|
||||
|
||||
// token
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
|
||||
// Validate validates this invite request
|
||||
|
@ -903,6 +903,9 @@ func init() {
|
||||
"properties": {
|
||||
"email": {
|
||||
"type": "string"
|
||||
},
|
||||
"token": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -2060,6 +2063,9 @@ func init() {
|
||||
"properties": {
|
||||
"email": {
|
||||
"type": "string"
|
||||
},
|
||||
"token": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -578,6 +578,8 @@ definitions:
|
||||
properties:
|
||||
email:
|
||||
type: string
|
||||
token:
|
||||
type: string
|
||||
|
||||
loginRequest:
|
||||
type: object
|
||||
|
@ -96,6 +96,7 @@
|
||||
* @memberof module:types
|
||||
*
|
||||
* @property {string} email
|
||||
* @property {string} token
|
||||
*/
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user