username -> email (#50, #51)

This commit is contained in:
Michael Quigley 2022-09-09 10:20:05 -04:00
parent 370fd78402
commit c95e84b53e
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
15 changed files with 55 additions and 55 deletions

View File

@ -34,7 +34,7 @@ func newCreateAccountCommand() *createAccountCommand {
}
func (cmd *createAccountCommand) run(_ *cobra.Command, _ []string) {
username, err := term.Prompt("New Username: ")
email, err := term.Prompt("New Email: ")
if err != nil {
panic(err)
}
@ -53,7 +53,7 @@ func (cmd *createAccountCommand) run(_ *cobra.Command, _ []string) {
zrok := newZrokClient()
req := identity.NewCreateAccountParams()
req.Body = &rest_model_zrok.AccountRequest{
Username: username,
Email: email,
Password: password,
}
resp, err := zrok.Identity.CreateAccount(req)

View File

@ -11,10 +11,10 @@ import (
)
func createAccountHandler(params identity.CreateAccountParams) middleware.Responder {
logrus.Infof("received account request for username '%v'", params.Body.Username)
if params.Body == nil || params.Body.Username == "" || params.Body.Password == "" {
logrus.Errorf("missing username or password")
return identity.NewCreateAccountBadRequest().WithPayload("missing username or password")
logrus.Infof("received account request for email '%v'", params.Body.Email)
if params.Body == nil || params.Body.Email == "" || params.Body.Password == "" {
logrus.Errorf("missing email or password")
return identity.NewCreateAccountBadRequest().WithPayload("missing email or password")
}
token, err := generateApiToken()
@ -23,7 +23,7 @@ func createAccountHandler(params identity.CreateAccountParams) middleware.Respon
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
a := &store.Account{
Username: params.Body.Username,
Email: params.Body.Email,
Password: hashPassword(params.Body.Password),
Token: token,
}

View File

@ -41,7 +41,7 @@ func (self *enableHandler) Handle(params identity.EnableParams, principal *rest_
logrus.Errorf("error getting edge client: %v", err)
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
ident, err := self.createIdentity(principal.Username, client)
ident, err := self.createIdentity(principal.Email, client)
if err != nil {
logrus.Error(err)
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
@ -73,7 +73,7 @@ func (self *enableHandler) Handle(params identity.EnableParams, principal *rest_
logrus.Errorf("error committing: %v", err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
logrus.Infof("recorded identity '%v' with id '%v' for '%v'", ident.Payload.Data.ID, envId, principal.Username)
logrus.Infof("recorded identity '%v' with id '%v' for '%v'", ident.Payload.Data.ID, envId, principal.Email)
resp := identity.NewEnableCreated().WithPayload(&rest_model_zrok.EnableResponse{
Identity: ident.Payload.Data.ID,
@ -91,13 +91,13 @@ func (self *enableHandler) Handle(params identity.EnableParams, principal *rest_
return resp
}
func (_ *enableHandler) createIdentity(username string, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) {
func (_ *enableHandler) createIdentity(email string, client *rest_management_api_client.ZitiEdgeManagement) (*identity_edge.CreateIdentityCreated, error) {
iIsAdmin := false
iId, err := randomId()
if err != nil {
return nil, err
}
name := fmt.Sprintf("%v-%v", username, iId)
name := fmt.Sprintf("%v-%v", email, iId)
identityType := rest_model_edge.IdentityTypeUser
i := &rest_model_edge.IdentityCreate{
Enrollment: &rest_model_edge.IdentityCreateEnrollment{Ott: true},

View File

@ -21,7 +21,7 @@ func loginHandler(params identity.LoginParams) middleware.Responder {
return identity.NewLoginUnauthorized()
}
defer func() { _ = tx.Rollback() }()
a, err := str.FindAccountWithUsername(params.Body.Email, tx)
a, err := str.FindAccountWithEmail(params.Body.Email, tx)
if err != nil {
logrus.Errorf("error finding account '%v': %v", params.Body.Email, err)
return identity.NewLoginUnauthorized()

View File

@ -16,7 +16,7 @@ func overviewHandler(_ metadata.OverviewParams, principal *rest_model_zrok.Princ
defer func() { _ = tx.Rollback() }()
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil {
logrus.Errorf("error finding environments for '%v': %v", principal.Username, err)
logrus.Errorf("error finding environments for '%v': %v", principal.Email, err)
return metadata.NewOverviewInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
var out rest_model_zrok.EnvironmentServicesList

View File

@ -7,17 +7,17 @@ import (
type Account struct {
Model
Username string
Email string
Password string
Token string
}
func (self *Store) CreateAccount(a *Account, tx *sqlx.Tx) (int, error) {
stmt, err := tx.Prepare("insert into accounts (username, password, token) values (?, ?, ?)")
stmt, err := tx.Prepare("insert into accounts (email, password, token) values (?, ?, ?)")
if err != nil {
return 0, errors.Wrap(err, "error preparing accounts insert statement")
}
res, err := stmt.Exec(a.Username, a.Password, a.Token)
res, err := stmt.Exec(a.Email, a.Password, a.Token)
if err != nil {
return 0, errors.Wrap(err, "error executing accounts insert statement")
}
@ -36,10 +36,10 @@ func (self *Store) GetAccount(id int, tx *sqlx.Tx) (*Account, error) {
return a, nil
}
func (self *Store) FindAccountWithUsername(username string, tx *sqlx.Tx) (*Account, error) {
func (self *Store) FindAccountWithEmail(email string, tx *sqlx.Tx) (*Account, error) {
a := &Account{}
if err := tx.QueryRowx("select * from accounts where username = ?", username).StructScan(a); err != nil {
return nil, errors.Wrap(err, "error selecting account by username")
if err := tx.QueryRowx("select * from accounts where email = ?", email).StructScan(a); err != nil {
return nil, errors.Wrap(err, "error selecting account by email")
}
return a, nil
}

View File

@ -5,14 +5,14 @@
--
create table accounts (
id integer primary key,
username string not null unique,
email string not null unique,
password string not null,
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_username check (username <> ''),
constraint chk_password check (username <> ''),
constraint chk_email check (email <> ''),
constraint chk_password check (password <> ''),
constraint chk_token check(token <> '')
);

View File

@ -28,7 +28,7 @@ func newTunnelHandler(cfg *Config) *tunnelHandler {
}
func (self *tunnelHandler) Handle(params tunnel.TunnelParams, principal *rest_model_zrok.Principal) middleware.Responder {
logrus.Infof("tunneling for '%v' (%v)", principal.Username, principal.Token)
logrus.Infof("tunneling for '%v' (%v)", principal.Email, principal.Token)
tx, err := str.Begin()
if err != nil {
@ -42,17 +42,17 @@ func (self *tunnelHandler) Handle(params tunnel.TunnelParams, principal *rest_mo
found := false
for _, env := range envs {
if env.ZitiIdentityId == envId {
logrus.Infof("found identity '%v' for user '%v'", envId, principal.Username)
logrus.Infof("found identity '%v' for user '%v'", envId, principal.Email)
found = true
break
}
}
if !found {
logrus.Errorf("environment '%v' not found for user '%v'", envId, principal.Username)
logrus.Errorf("environment '%v' not found for user '%v'", envId, principal.Email)
return tunnel.NewTunnelUnauthorized().WithPayload("bad environment identity")
}
} else {
logrus.Errorf("error finding environments for account '%v'", principal.Username)
logrus.Errorf("error finding environments for account '%v'", principal.Email)
return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
@ -104,7 +104,7 @@ func (self *tunnelHandler) Handle(params tunnel.TunnelParams, principal *rest_mo
logrus.Errorf("error committing service record: %v", err)
return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
logrus.Infof("recorded service '%v' with id '%v' for '%v'", svcId, sid, principal.Username)
logrus.Infof("recorded service '%v' with id '%v' for '%v'", svcId, sid, principal.Email)
return tunnel.NewTunnelCreated().WithPayload(&rest_model_zrok.TunnelResponse{
ProxyEndpoint: self.proxyUrl(svcName),

View File

@ -26,7 +26,7 @@ func newUntunnelHandler(cfg *Config) *untunnelHandler {
}
func (self *untunnelHandler) Handle(params tunnel.UntunnelParams, principal *rest_model_zrok.Principal) middleware.Responder {
logrus.Infof("untunneling for '%v' (%v)", principal.Username, principal.Token)
logrus.Infof("untunneling for '%v' (%v)", principal.Email, principal.Token)
tx, err := str.Begin()
if err != nil {
@ -55,12 +55,12 @@ func (self *untunnelHandler) Handle(params tunnel.UntunnelParams, principal *res
}
}
if senv == nil {
err := errors.Errorf("environment with id '%v' not found for '%v", params.Body.ZitiIdentityID, principal.Username)
err := errors.Errorf("environment with id '%v' not found for '%v", params.Body.ZitiIdentityID, principal.Email)
logrus.Error(err)
return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
} else {
logrus.Errorf("error finding environments for account '%v': %v", principal.Username, err)
logrus.Errorf("error finding environments for account '%v': %v", principal.Email, err)
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
@ -73,12 +73,12 @@ func (self *untunnelHandler) Handle(params tunnel.UntunnelParams, principal *res
}
}
if ssvc == nil {
err := errors.Errorf("service with id '%v' not found for '%v'", svcId, principal.Username)
err := errors.Errorf("service with id '%v' not found for '%v'", svcId, principal.Email)
logrus.Error(err)
return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
} else {
logrus.Errorf("error finding services for account '%v': %v", principal.Username, err)
logrus.Errorf("error finding services for account '%v': %v", principal.Email, err)
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}

View File

@ -19,9 +19,9 @@ func ZrokAuthenticate(token string) (*rest_model_zrok.Principal, error) {
defer func() { _ = tx.Rollback() }()
if a, err := str.FindAccountWithToken(token, tx); err == nil {
principal := rest_model_zrok.Principal{
ID: int64(a.Id),
Token: a.Token,
Username: a.Username,
ID: int64(a.Id),
Token: a.Token,
Email: a.Email,
}
return &principal, nil
} else {

View File

@ -17,11 +17,11 @@ import (
// swagger:model accountRequest
type AccountRequest struct {
// email
Email string `json:"email,omitempty"`
// password
Password string `json:"password,omitempty"`
// username
Username string `json:"username,omitempty"`
}
// Validate validates this account request

View File

@ -17,14 +17,14 @@ import (
// swagger:model principal
type Principal struct {
// email
Email string `json:"email,omitempty"`
// id
ID int64 `json:"id,omitempty"`
// token
Token string `json:"token,omitempty"`
// username
Username string `json:"username,omitempty"`
}
// Validate validates this principal

View File

@ -307,10 +307,10 @@ func init() {
"accountRequest": {
"type": "object",
"properties": {
"password": {
"email": {
"type": "string"
},
"username": {
"password": {
"type": "string"
}
}
@ -433,14 +433,14 @@ func init() {
"principal": {
"type": "object",
"properties": {
"email": {
"type": "string"
},
"id": {
"type": "integer"
},
"token": {
"type": "string"
},
"username": {
"type": "string"
}
}
},
@ -814,10 +814,10 @@ func init() {
"accountRequest": {
"type": "object",
"properties": {
"password": {
"email": {
"type": "string"
},
"username": {
"password": {
"type": "string"
}
}
@ -940,14 +940,14 @@ func init() {
"principal": {
"type": "object",
"properties": {
"email": {
"type": "string"
},
"id": {
"type": "integer"
},
"token": {
"type": "string"
},
"username": {
"type": "string"
}
}
},

View File

@ -183,7 +183,7 @@ definitions:
accountRequest:
type: object
properties:
username:
email:
type: string
password:
type: string
@ -274,7 +274,7 @@ definitions:
properties:
id:
type: integer
username:
email:
type: string
token:
type: string

View File

@ -5,7 +5,7 @@
* @typedef accountRequest
* @memberof module:types
*
* @property {string} username
* @property {string} email
* @property {string} password
*/
@ -81,7 +81,7 @@
* @memberof module:types
*
* @property {number} id
* @property {string} username
* @property {string} email
* @property {string} token
*/