ovpn-certman/models/models.go

77 lines
1.8 KiB
Go
Raw Normal View History

2018-01-26 08:43:53 +01:00
package models
import (
"errors"
2018-01-29 09:18:19 +01:00
"time"
2018-01-26 08:43:53 +01:00
2018-01-29 09:18:19 +01:00
"golang.org/x/crypto/bcrypt"
2018-01-26 08:43:53 +01:00
)
var (
// ErrNotImplemented gets thrown if some action was not attempted,
// because it is not implemented in the code yet.
ErrNotImplemented = errors.New("Not implemented")
)
2018-01-29 09:18:19 +01:00
// Model is a base model definition, including helpful fields for dealing with
// models in a database
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
2018-01-26 08:43:53 +01:00
// User represents a User of the system which is able to log in
type User struct {
2018-01-29 09:18:19 +01:00
Model
Email string
EmailValid bool
DisplayName string
2018-01-26 08:43:53 +01:00
HashedPassword []byte
IsAdmin bool
}
// SetPassword sets the password of an user struct, but does not save it yet
func (u *User) SetPassword(password string) error {
2018-01-29 09:18:19 +01:00
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
u.HashedPassword = bytes
return nil
2018-01-26 08:43:53 +01:00
}
// CheckPassword compares a supplied plain text password with the internally
// stored password hash, returns error=nil on success.
func (u *User) CheckPassword(password string) error {
2018-01-29 09:18:19 +01:00
return bcrypt.CompareHashAndPassword(u.HashedPassword, []byte(password))
}
type UserProvider interface {
CountUsers() (uint, error)
CreateUser(*User) (*User, error)
ListUsers(count, offset int) ([]*User, error)
GetUserByID(id uint) (*User, error)
GetUserByEmail(email string) (*User, error)
DeleteUser(id uint) error
2018-01-26 08:43:53 +01:00
}
2018-01-29 09:18:19 +01:00
// Client represent the OpenVPN client configuration
type Client struct {
Model
2018-01-26 08:43:53 +01:00
Name string
User User
2018-01-29 09:18:19 +01:00
UserID uint
2018-01-26 08:43:53 +01:00
Cert []byte
PrivateKey []byte
}
2018-01-29 09:18:19 +01:00
type ClientProvider interface {
CountClients() (uint, error)
CreateClient(*User) (*User, error)
ListClients(count, offset int) ([]*User, error)
GetClientByID(id uint) (*User, error)
DeleteClient(id uint) error
}