Mostly finish docs
Actually start to parse things
This commit is contained in:
@@ -3,11 +3,17 @@ package darkstorm
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPasswordLength = errors.New("password length must be 12-128")
|
||||
)
|
||||
|
||||
func generateSalt() (string, error) {
|
||||
out := make([]byte, 16)
|
||||
_, err := rand.Read(out)
|
||||
@@ -24,7 +30,16 @@ type User struct {
|
||||
PasswordChange uint64
|
||||
}
|
||||
|
||||
type ReqUser struct {
|
||||
Perm map[string]string
|
||||
ID string
|
||||
Username string
|
||||
}
|
||||
|
||||
func NewUser(username, password, email string) (*User, error) {
|
||||
if len(password) < 12 || len(password) > 128 {
|
||||
return nil, ErrPasswordLength
|
||||
}
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -44,7 +59,19 @@ func NewUser(username, password, email string) (*User, error) {
|
||||
return out, err
|
||||
}
|
||||
|
||||
func (u *User) HashPassword(password string) (string, error) {
|
||||
func (u User) GetID() string {
|
||||
return u.ID
|
||||
}
|
||||
|
||||
func (u User) toReqUser() ReqUser {
|
||||
return ReqUser{
|
||||
Perm: u.Perm,
|
||||
ID: u.ID,
|
||||
Username: u.Username,
|
||||
}
|
||||
}
|
||||
|
||||
func (u User) HashPassword(password string) (string, error) {
|
||||
salt, err := base64.RawStdEncoding.DecodeString(u.Salt)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -53,10 +80,39 @@ func (u *User) HashPassword(password string) (string, error) {
|
||||
return base64.RawStdEncoding.EncodeToString(res), nil
|
||||
}
|
||||
|
||||
func (u *User) ValidatePassword(password string) (bool, error) {
|
||||
func (u User) ValidatePassword(password string) (bool, error) {
|
||||
hsh, err := u.HashPassword(password)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return hsh == u.Password, nil
|
||||
}
|
||||
|
||||
type createUserRequest struct {
|
||||
Username string
|
||||
Password string
|
||||
Email string
|
||||
}
|
||||
|
||||
type createUserReturn struct {
|
||||
Username string
|
||||
Token string
|
||||
}
|
||||
|
||||
func (b *Backend) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
type loginRequest struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
type loginReturn struct {
|
||||
Token string
|
||||
Timeout int
|
||||
}
|
||||
|
||||
func (b *Backend) Login(w http.ResponseWriter, r *http.Request) {
|
||||
//TODO
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user