2021-02-09 11:53:33 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-02-08 16:47:42 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/volatiletech/sqlboiler/v4/boil"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
2021-03-10 15:44:46 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite/models"
|
2021-02-08 16:47:42 +00:00
|
|
|
"github.com/volatiletech/sqlboiler/v4/queries/qm"
|
|
|
|
|
2021-03-10 15:44:46 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
|
2021-02-08 16:47:42 +00:00
|
|
|
)
|
|
|
|
|
2021-03-02 16:14:02 +00:00
|
|
|
// compiler assertion to ensure the struct fullfills the interface
|
2021-03-10 15:44:46 +00:00
|
|
|
var _ roomdb.AuthFallbackService = (*AuthFallback)(nil)
|
2021-02-08 16:47:42 +00:00
|
|
|
|
|
|
|
type AuthFallback struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2021-03-09 12:39:43 +00:00
|
|
|
// Check receives the username and password (in clear) and checks them accordingly.
|
|
|
|
// If it's a valid combination it returns the user ID, or an error if they are not.
|
2021-03-11 07:52:53 +00:00
|
|
|
func (af AuthFallback) Check(name, password string) (interface{}, error) {
|
2021-02-08 16:47:42 +00:00
|
|
|
ctx := context.Background()
|
2021-03-11 07:52:53 +00:00
|
|
|
found, err := models.AuthFallbacks(qm.Where("name = ?", name)).One(ctx, af.db)
|
2021-02-08 16:47:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword(found.PasswordHash, []byte(password))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("auth/fallback: password missmatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
return found.ID, nil
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:52:53 +00:00
|
|
|
func (af AuthFallback) Create(ctx context.Context, name string, password []byte) (int64, error) {
|
2021-02-08 16:47:42 +00:00
|
|
|
var u models.AuthFallback
|
|
|
|
u.Name = name
|
|
|
|
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
2021-03-02 16:14:02 +00:00
|
|
|
return -1, fmt.Errorf("auth/fallback: failed to hash password for new user")
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u.PasswordHash = hashed
|
|
|
|
|
2021-03-11 07:52:53 +00:00
|
|
|
err = u.Insert(ctx, af.db, boil.Infer())
|
2021-02-08 16:47:42 +00:00
|
|
|
if err != nil {
|
2021-03-02 16:14:02 +00:00
|
|
|
return -1, fmt.Errorf("auth/fallback: failed to insert new user: %w", err)
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 16:14:02 +00:00
|
|
|
return u.ID, nil
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
2021-02-09 11:39:57 +00:00
|
|
|
|
2021-03-11 07:52:53 +00:00
|
|
|
func (af AuthFallback) GetByID(ctx context.Context, uid int64) (*roomdb.User, error) {
|
|
|
|
modelU, err := models.FindAuthFallback(ctx, af.db, uid)
|
2021-02-09 11:39:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-10 15:44:46 +00:00
|
|
|
return &roomdb.User{
|
2021-02-09 11:39:57 +00:00
|
|
|
ID: modelU.ID,
|
|
|
|
Name: modelU.Name,
|
|
|
|
}, nil
|
|
|
|
}
|