lose a temp variable

This commit is contained in:
Your Name 2020-07-18 08:03:31 -04:00
parent 6d43965c1f
commit b76896e63a
1 changed files with 29 additions and 29 deletions

58
main.go
View File

@ -56,10 +56,10 @@ func UsernameAvailable(username string) bool {
return false return false
} }
var tmp string var tmp string
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("select username from %s_auth where username = $1", c.TablePrefix) q := fmt.Sprintf("select username from %s_auth where username = $1", c.TablePrefix)
err := c.db.Get(&tmp, q, u) err := c.db.Get(&tmp, q, username)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return true return true
} }
@ -76,7 +76,7 @@ func Register(username, email, ip string) bool {
if !UsernameAvailable(username) { if !UsernameAvailable(username) {
return false return false
} }
u := strings.ToLower(username) username = strings.ToLower(username)
pass := randBytes(16) pass := randBytes(16)
crypt, err := bcrypt.GenerateFromPassword(pass, 10) crypt, err := bcrypt.GenerateFromPassword(pass, 10)
@ -85,7 +85,7 @@ func Register(username, email, ip string) bool {
return false return false
} }
q := fmt.Sprintf("insert into %s_auth (username, , displayname, email, password, registration_date, registration_ip) values ($1, $2, $3, CURRENT_TIMESTAMP, $4)", c.TablePrefix) q := fmt.Sprintf("insert into %s_auth (username, , displayname, email, password, registration_date, registration_ip) values ($1, $2, $3, CURRENT_TIMESTAMP, $4)", c.TablePrefix)
_, err = c.db.Exec(q, u, username, email, crypt, ip) _, err = c.db.Exec(q, username, username, email, crypt, ip)
if err != nil { if err != nil {
log.Printf("scsusers.Register: insert failed: %s\n", err.Error()) log.Printf("scsusers.Register: insert failed: %s\n", err.Error())
return false return false
@ -96,7 +96,7 @@ func Register(username, email, ip string) bool {
} }
log.Printf("scsusers.Register: Failed to send registration email, deleting user %s\n", username) log.Printf("scsusers.Register: Failed to send registration email, deleting user %s\n", username)
q = fmt.Sprintf("delete from %s_auth where username = $1 AND password=$2", c.TablePrefix) q = fmt.Sprintf("delete from %s_auth where username = $1 AND password=$2", c.TablePrefix)
_, err = c.db.Exec(q, u, string(crypt)) _, err = c.db.Exec(q, username, string(crypt))
if err != nil { if err != nil {
log.Printf("scsusers.Register: Failed to delete new user %s: %s\n", username, err.Error()) log.Printf("scsusers.Register: Failed to delete new user %s: %s\n", username, err.Error())
} }
@ -104,11 +104,11 @@ func Register(username, email, ip string) bool {
} }
func Login(username, password string) bool { func Login(username, password string) bool {
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("select password from %s_auth where username = $1 AND status='active'", c.TablePrefix) q := fmt.Sprintf("select password from %s_auth where username = $1 AND status='active'", c.TablePrefix)
var crypt string var crypt string
err := c.db.Get(&crypt, q, u) err := c.db.Get(&crypt, q, username)
if err != nil { if err != nil {
log.Printf("scsusers.Login: Failed login attempt for unknown username: %s\n", username) log.Printf("scsusers.Login: Failed login attempt for unknown username: %s\n", username)
return false return false
@ -122,11 +122,11 @@ func Login(username, password string) bool {
} }
func ChangePassword(username, oldpass, newpass string) bool { func ChangePassword(username, oldpass, newpass string) bool {
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("select password from %s_auth where username = $1 AND status='active'", c.TablePrefix) q := fmt.Sprintf("select password from %s_auth where username = $1 AND status='active'", c.TablePrefix)
var crypt string var crypt string
err := c.db.Get(&crypt, q, u) err := c.db.Get(&crypt, q, username)
if err != nil { if err != nil {
log.Println("scsusers.ChangePassword: Failed change attempt for unknown username: " + username) log.Println("scsusers.ChangePassword: Failed change attempt for unknown username: " + username)
return false return false
@ -137,7 +137,7 @@ func ChangePassword(username, oldpass, newpass string) bool {
} }
newcrypt, err := bcrypt.GenerateFromPassword([]byte(newpass), 10) newcrypt, err := bcrypt.GenerateFromPassword([]byte(newpass), 10)
q = fmt.Sprintf("update %s_auth set password=$2 where username = $1", c.TablePrefix) q = fmt.Sprintf("update %s_auth set password=$2 where username = $1", c.TablePrefix)
_, err = c.db.Exec(q, u, newcrypt) _, err = c.db.Exec(q, username, newcrypt)
if err != nil { if err != nil {
log.Printf("scsusers.ChangePassword: update failed for %s: %s\n", username, err.Error()) log.Printf("scsusers.ChangePassword: update failed for %s: %s\n", username, err.Error())
return false return false
@ -148,10 +148,10 @@ func ChangePassword(username, oldpass, newpass string) bool {
func GetUserid(username string) int64 { func GetUserid(username string) int64 {
var i int64 var i int64
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("select userid from %s_auth where username = $1", c.TablePrefix) q := fmt.Sprintf("select userid from %s_auth where username = $1", c.TablePrefix)
err := c.db.Get(&i, q, u) err := c.db.Get(&i, q, username)
if err != nil { if err != nil {
log.Printf("scsusers.getUserId: Error loading user: %s : %s\n", username, err.Error()) log.Printf("scsusers.getUserId: Error loading user: %s : %s\n", username, err.Error())
return 0 return 0
@ -160,11 +160,11 @@ func GetUserid(username string) int64 {
} }
func LoadUser(username string) (UserData, error) { func LoadUser(username string) (UserData, error) {
var tmp UserData var tmp UserData
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("select data from %s_userdata where username = $1", c.TablePrefix) q := fmt.Sprintf("select data from %s_userdata where username = $1", c.TablePrefix)
var d string var d string
err := c.db.Get(d, q, u) err := c.db.Get(d, q, username)
if err != nil { if err != nil {
log.Printf("scsusers.LoadUser: Error loading user: %s : %s\n", username, err.Error()) log.Printf("scsusers.LoadUser: Error loading user: %s : %s\n", username, err.Error())
return tmp, err return tmp, err
@ -177,14 +177,14 @@ func LoadUser(username string) (UserData, error) {
} }
func SaveUser(username string, d UserData) bool { func SaveUser(username string, d UserData) bool {
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("update %s_userdata set data=$1 where username = $2", c.TablePrefix) q := fmt.Sprintf("update %s_userdata set data=$1 where username = $2", c.TablePrefix)
j, err := json.Marshal(d) j, err := json.Marshal(d)
if err != nil { if err != nil {
log.Printf("scsusers.SaveUser: json.Marshal failed for username %s : %s\n", username, err.Error()) log.Printf("scsusers.SaveUser: json.Marshal failed for username %s : %s\n", username, err.Error())
return false return false
} }
_, err = c.db.Exec(q, u, j) _, err = c.db.Exec(q, username, j)
if err != nil { if err != nil {
log.Printf("scsusers.SaveUser: db.Exec failed for username %s : %s\n", username, err.Error()) log.Printf("scsusers.SaveUser: db.Exec failed for username %s : %s\n", username, err.Error())
return false return false
@ -194,9 +194,9 @@ func SaveUser(username string, d UserData) bool {
} }
func Bump(username string, ip string) { func Bump(username string, ip string) {
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("update %s_auth set lastseen=CURRENT_TIMESTAMP, lastseenip=$2 where username = $1", c.TablePrefix) q := fmt.Sprintf("update %s_auth set lastseen=CURRENT_TIMESTAMP, lastseenip=$2 where username = $1", c.TablePrefix)
_, err := c.db.Exec(q, u, ip) _, err := c.db.Exec(q, username, ip)
if err != nil { if err != nil {
log.Printf("scsusers.Bump: Error on user bump: %s : %s\n", username, err.Error()) log.Printf("scsusers.Bump: Error on user bump: %s : %s\n", username, err.Error())
} }
@ -209,13 +209,13 @@ type Metadata struct {
func GetAllMeta(username string) map[string]string { func GetAllMeta(username string) map[string]string {
meta := make(map[string]string) meta := make(map[string]string)
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf(`select meta_key, meta_value q := fmt.Sprintf(`select meta_key, meta_value
from %s_user_metadata where from %s_user_metadata where
user_id=(select userid from %s_auth where username = $1)`, user_id=(select userid from %s_auth where username = $1)`,
c.TablePrefix, c.TablePrefix) c.TablePrefix, c.TablePrefix)
rows, err := c.db.Queryx(q, u) rows, err := c.db.Queryx(q, username)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
log.Printf("scsusers.GetAllMeta: %s: %s\n", username, err.Error()) log.Printf("scsusers.GetAllMeta: %s: %s\n", username, err.Error())
return meta return meta
@ -234,11 +234,11 @@ func GetAllMeta(username string) map[string]string {
func GetMeta(username string, metakey string) string { func GetMeta(username string, metakey string) string {
var v string var v string
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf(`select meta_value from %s_user_metadata where q := fmt.Sprintf(`select meta_value from %s_user_metadata where
user_id=(select userid from %s_auth where username = $1) AND meta_key=$2`, c.TablePrefix, c.TablePrefix) user_id=(select userid from %s_auth where username = $1) AND meta_key=$2`, c.TablePrefix, c.TablePrefix)
err := c.db.Get(&v, q, u, metakey) err := c.db.Get(&v, q, username, metakey)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
log.Printf("scsusers.GetMeta: %s - %s - %s\n", username, metakey, err.Error()) log.Printf("scsusers.GetMeta: %s - %s - %s\n", username, metakey, err.Error())
} }
@ -255,16 +255,16 @@ func GetMeta(username string, metakey string) string {
func SetMeta(username string, metakey string, metavalue string) { func SetMeta(username string, metakey string, metavalue string) {
var err error var err error
u := strings.ToLower(username) username = strings.ToLower(username)
if metavalue == "" { if metavalue == "" {
q := fmt.Sprintf(`delete from %s_user_metadata where user_id=(select userid from %s_auth where username = $1) AND meta_key=$2`, q := fmt.Sprintf(`delete from %s_user_metadata where user_id=(select userid from %s_auth where username = $1) AND meta_key=$2`,
c.TablePrefix, c.TablePrefix) c.TablePrefix, c.TablePrefix)
_, err = c.db.Exec(q, u, metakey) _, err = c.db.Exec(q, username, metakey)
} else { } else {
q := fmt.Sprintf(`insert into %s_user_metadata (user_id, meta_key, meta_value) VALUES q := fmt.Sprintf(`insert into %s_user_metadata (user_id, meta_key, meta_value) VALUES
((select userid from %s_auth where username = $1), $2, $3)`, c.TablePrefix, c.TablePrefix) ((select userid from %s_auth where username = $1), $2, $3)`, c.TablePrefix, c.TablePrefix)
_, err = c.db.Exec(q, u, metakey, metavalue) _, err = c.db.Exec(q, username, metakey, metavalue)
} }
if err != nil { if err != nil {
log.Printf("scsusers.SetMeta: %s %s %s %s\n", username, metakey, metavalue, err.Error()) log.Printf("scsusers.SetMeta: %s %s %s %s\n", username, metakey, metavalue, err.Error())
@ -273,14 +273,14 @@ func SetMeta(username string, metakey string, metavalue string) {
func RecoverByUsername(username string) { func RecoverByUsername(username string) {
var email string var email string
u := strings.ToLower(username) username = strings.ToLower(username)
q := fmt.Sprintf("select email from %s_auth where username = $1", c.TablePrefix) q := fmt.Sprintf("select email from %s_auth where username = $1", c.TablePrefix)
err := c.db.Get(&email, q, u) err := c.db.Get(&email, q, username)
if err != sql.ErrNoRows { if err != sql.ErrNoRows {
recoverycode := randBytes(16) recoverycode := randBytes(16)
qq := fmt.Sprintf("update %s_auth set recoverycode=$1, recoverytime=NOW() where username = $2", c.TablePrefix) qq := fmt.Sprintf("update %s_auth set recoverycode=$1, recoverytime=NOW() where username = $2", c.TablePrefix)
_, err := c.db.Exec(qq, recoverycode, u) _, err := c.db.Exec(qq, recoverycode, username)
if err == nil { if err == nil {
sendRecoveryEmail(email, username, string(recoverycode)) sendRecoveryEmail(email, username, string(recoverycode))
} }
@ -436,7 +436,7 @@ func SetRecoveryTemplate(t string) bool {
return true return true
} }
} }
df := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html></head><body><p>Hello {{.UserName}}! Someone (hopefully you) has attempted an account recovery agt {{.SiteName}}. If this was you, enter the following code to regain access: {{.RecoveryCode}}<br> If this was not you, you can ignore this email.</p></body></html>` df := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html></head><body><p>Hello {{.UserName}}! Someone (hopefully you) has attempted an account recovery agt {{.SiteName}}. If this was yousername, enter the following code to regain access: {{.RecoveryCode}}<br> If this was not yousername, you can ignore this email.</p></body></html>`
r, err := template.New("recovery").Parse(df) r, err := template.New("recovery").Parse(df)
if err != nil { if err != nil {
log.Fatal("scsusers.SetRecoveryTemplate: Default recovery template MUST compile. Error: " + err.Error()) log.Fatal("scsusers.SetRecoveryTemplate: Default recovery template MUST compile. Error: " + err.Error())