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

View File

@ -11,10 +11,10 @@ import (
) )
func createAccountHandler(params identity.CreateAccountParams) middleware.Responder { func createAccountHandler(params identity.CreateAccountParams) middleware.Responder {
logrus.Infof("received account request for username '%v'", params.Body.Username) logrus.Infof("received account request for email '%v'", params.Body.Email)
if params.Body == nil || params.Body.Username == "" || params.Body.Password == "" { if params.Body == nil || params.Body.Email == "" || params.Body.Password == "" {
logrus.Errorf("missing username or password") logrus.Errorf("missing email or password")
return identity.NewCreateAccountBadRequest().WithPayload("missing username or password") return identity.NewCreateAccountBadRequest().WithPayload("missing email or password")
} }
token, err := generateApiToken() token, err := generateApiToken()
@ -23,7 +23,7 @@ func createAccountHandler(params identity.CreateAccountParams) middleware.Respon
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
} }
a := &store.Account{ a := &store.Account{
Username: params.Body.Username, Email: params.Body.Email,
Password: hashPassword(params.Body.Password), Password: hashPassword(params.Body.Password),
Token: token, 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) logrus.Errorf("error getting edge client: %v", err)
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) 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 { if err != nil {
logrus.Error(err) logrus.Error(err)
return identity.NewEnableInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) 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) logrus.Errorf("error committing: %v", err)
return identity.NewCreateAccountInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) 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{ resp := identity.NewEnableCreated().WithPayload(&rest_model_zrok.EnableResponse{
Identity: ident.Payload.Data.ID, Identity: ident.Payload.Data.ID,
@ -91,13 +91,13 @@ func (self *enableHandler) Handle(params identity.EnableParams, principal *rest_
return resp 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 iIsAdmin := false
iId, err := randomId() iId, err := randomId()
if err != nil { if err != nil {
return nil, err return nil, err
} }
name := fmt.Sprintf("%v-%v", username, iId) name := fmt.Sprintf("%v-%v", email, iId)
identityType := rest_model_edge.IdentityTypeUser identityType := rest_model_edge.IdentityTypeUser
i := &rest_model_edge.IdentityCreate{ i := &rest_model_edge.IdentityCreate{
Enrollment: &rest_model_edge.IdentityCreateEnrollment{Ott: true}, Enrollment: &rest_model_edge.IdentityCreateEnrollment{Ott: true},

View File

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

View File

@ -16,7 +16,7 @@ func overviewHandler(_ metadata.OverviewParams, principal *rest_model_zrok.Princ
defer func() { _ = tx.Rollback() }() defer func() { _ = tx.Rollback() }()
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx) envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil { 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())) return metadata.NewOverviewInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
} }
var out rest_model_zrok.EnvironmentServicesList var out rest_model_zrok.EnvironmentServicesList

View File

@ -7,17 +7,17 @@ import (
type Account struct { type Account struct {
Model Model
Username string Email string
Password string Password string
Token string Token string
} }
func (self *Store) CreateAccount(a *Account, tx *sqlx.Tx) (int, error) { 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 { if err != nil {
return 0, errors.Wrap(err, "error preparing accounts insert statement") 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 { if err != nil {
return 0, errors.Wrap(err, "error executing accounts insert statement") 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 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{} a := &Account{}
if err := tx.QueryRowx("select * from accounts where username = ?", username).StructScan(a); err != nil { if err := tx.QueryRowx("select * from accounts where email = ?", email).StructScan(a); err != nil {
return nil, errors.Wrap(err, "error selecting account by username") return nil, errors.Wrap(err, "error selecting account by email")
} }
return a, nil return a, nil
} }

View File

@ -5,14 +5,14 @@
-- --
create table accounts ( create table accounts (
id integer primary key, id integer primary key,
username string not null unique, email string not null unique,
password string not null, password string not null,
token string not null unique, token string not null unique,
created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')), 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')), updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')),
constraint chk_username check (username <> ''), constraint chk_email check (email <> ''),
constraint chk_password check (username <> ''), constraint chk_password check (password <> ''),
constraint chk_token check(token <> '') 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 { 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() tx, err := str.Begin()
if err != nil { if err != nil {
@ -42,17 +42,17 @@ func (self *tunnelHandler) Handle(params tunnel.TunnelParams, principal *rest_mo
found := false found := false
for _, env := range envs { for _, env := range envs {
if env.ZitiIdentityId == envId { 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 found = true
break break
} }
} }
if !found { 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") return tunnel.NewTunnelUnauthorized().WithPayload("bad environment identity")
} }
} else { } 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())) 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) logrus.Errorf("error committing service record: %v", err)
return tunnel.NewTunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) 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{ return tunnel.NewTunnelCreated().WithPayload(&rest_model_zrok.TunnelResponse{
ProxyEndpoint: self.proxyUrl(svcName), 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 { 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() tx, err := str.Begin()
if err != nil { if err != nil {
@ -55,12 +55,12 @@ func (self *untunnelHandler) Handle(params tunnel.UntunnelParams, principal *res
} }
} }
if senv == nil { 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) logrus.Error(err)
return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
} }
} else { } 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())) 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 { 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) logrus.Error(err)
return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) return tunnel.NewUntunnelNotFound().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
} }
} else { } 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())) 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() }() defer func() { _ = tx.Rollback() }()
if a, err := str.FindAccountWithToken(token, tx); err == nil { if a, err := str.FindAccountWithToken(token, tx); err == nil {
principal := rest_model_zrok.Principal{ principal := rest_model_zrok.Principal{
ID: int64(a.Id), ID: int64(a.Id),
Token: a.Token, Token: a.Token,
Username: a.Username, Email: a.Email,
} }
return &principal, nil return &principal, nil
} else { } else {

View File

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

View File

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

View File

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

View File

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

View File

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