Add token unwrapping, admin concept and user creation endpoint

This commit is contained in:
Miguel de la Cruz 2021-09-13 14:52:00 +02:00
parent 7ebb14e431
commit 3c7c32423e
12 changed files with 141 additions and 16 deletions

View file

@ -6,13 +6,13 @@ import (
)
type App struct {
config *model.Config
store *store.Store
Config *model.Config
Store *store.Store
}
func NewApp(config *model.Config, store *store.Store) *App {
return &App{
config: config,
store: store,
Config: config,
Store: store,
}
}

View file

@ -8,7 +8,7 @@ import (
)
func (a *App) Login(username, password string) (string, error) {
user, err := a.store.User().GetByUsername(username)
user, err := a.Store.User().GetByUsername(username)
if err == sql.ErrNoRows {
return "", nil
} else if err != nil {
@ -19,7 +19,7 @@ func (a *App) Login(username, password string) (string, error) {
return "", nil
}
token, err := utils.GenerateToken(user.ID, *a.config.Secret)
token, err := utils.GenerateToken(user.ID, *a.Config.Secret)
if err != nil {
return "", fmt.Errorf("cannot generate token: %w", err)
}

View file

@ -7,7 +7,7 @@ import (
"git.ctrlz.es/mgdelacroix/craban/utils"
)
func (a *App) CreateUser(username, password, name, mail string) (*model.User, error) {
func (a *App) CreateUser(username, password, name, mail string, admin bool) (*model.User, error) {
hashedPassword, err := utils.Encrypt(password)
if err != nil {
return nil, fmt.Errorf("cannot create user: %w", err)
@ -18,19 +18,24 @@ func (a *App) CreateUser(username, password, name, mail string) (*model.User, er
Password: hashedPassword,
Name: name,
Mail: mail,
Admin: admin,
}
if err := newUser.IsValid(); err != nil {
return nil, fmt.Errorf("invalid user for creation: %w", err)
}
return a.store.User().Create(newUser)
return a.Store.User().Create(newUser)
}
func (a *App) ListUsers() ([]*model.User, error) {
return a.store.User().List()
return a.Store.User().List()
}
func (a *App) DeleteUserByUsername(username string) error {
return a.store.User().DeleteByUsername(username)
return a.Store.User().DeleteByUsername(username)
}
func (a *App) GetUserByID(userID int) (*model.User, error) {
return a.Store.User().GetByID(userID)
}