Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
109 lines (88 sloc) 2.1 KB
package model
import (
"crypto/sha256"
"encoding/base64"
"errors"
"strconv"
"strings"
)
type Password string
func (self *Password) Get() string {
return string(*self)
}
func (self *Password) Set(value string) {
*self = Password(value)
}
// todo salt
func (self *Password) SetHashed(value string) {
self.Set(PasswordHash(value))
}
func (self *Password) EqualsHashed(value string) bool {
return self.Get() == PasswordHash(value)
}
func (self *Password) IsEmpty() bool {
return len(*self) == 0
}
func (self *Password) String() string {
return self.Get()
}
func (self *Password) SetString(str string) error {
self.Set(str)
return nil
}
func (self *Password) FixValue(metaData *MetaData) {
}
func (self *Password) Required(metaData *MetaData) bool {
if minlen, ok, _ := self.Minlen(metaData); ok {
if minlen > 0 {
return true
}
}
return metaData.BoolAttrib(StructTagKey, "required")
}
func (self *Password) Validate(metaData *MetaData) error {
value := string(*self)
pos := strings.IndexAny(value, "\n\r")
if pos != -1 {
return errors.New("Line breaks not allowed")
}
if self.Required(metaData) && self.IsEmpty() {
return NewRequiredError(metaData)
}
minlen, ok, err := self.Minlen(metaData)
if ok && len(value) < minlen {
err = &StringTooShort{value, minlen}
}
if err != nil {
return err
}
maxlen, ok, err := self.Maxlen(metaData)
if ok && len(value) > maxlen {
err = &StringTooLong{value, maxlen}
}
if err != nil {
return err
}
return nil
}
func (self *Password) Minlen(metaData *MetaData) (minlen int, ok bool, err error) {
var str string
if str, ok = metaData.Attrib(StructTagKey, "minlen"); ok {
minlen, err = strconv.Atoi(str)
ok = err == nil
}
return minlen, ok, err
}
func (self *Password) Maxlen(metaData *MetaData) (maxlen int, ok bool, err error) {
var str string
if str, ok = metaData.Attrib(StructTagKey, "maxlen"); ok {
maxlen, err = strconv.Atoi(str)
ok = err == nil
}
return maxlen, ok, err
}
func PasswordHash(password string) (hash string) {
sha := sha256.New()
sha.Write([]byte(password))
return base64.StdEncoding.EncodeToString(sha.Sum(nil))
}
Something went wrong with that request. Please try again.