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"
|
|
|
|
"github.com/volatiletech/sqlboiler/v4/queries/qm"
|
2021-03-18 16:49:52 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-02-08 16:47:42 +00:00
|
|
|
|
2021-03-10 15:44:46 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
|
2021-03-18 16:49:52 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite/models"
|
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-18 16:49:52 +00:00
|
|
|
func (af AuthFallback) Check(login, password string) (interface{}, error) {
|
2021-02-08 16:47:42 +00:00
|
|
|
ctx := context.Background()
|
2021-03-18 16:49:52 +00:00
|
|
|
found, err := models.FallbackPasswords(
|
|
|
|
qm.Load("Member"),
|
|
|
|
qm.Where("login = ?", login),
|
|
|
|
).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")
|
|
|
|
}
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
return found.R.Member.ID, nil
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
func (af AuthFallback) Create(ctx context.Context, memberID int64, login string, password []byte) error {
|
|
|
|
var newPasswordEntry models.FallbackPassword
|
|
|
|
newPasswordEntry.MemberID = memberID
|
|
|
|
newPasswordEntry.Login = login
|
2021-02-08 16:47:42 +00:00
|
|
|
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
2021-03-18 16:49:52 +00:00
|
|
|
return fmt.Errorf("auth/fallback: failed to hash password for new user")
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
2021-03-18 16:49:52 +00:00
|
|
|
newPasswordEntry.PasswordHash = hashed
|
2021-02-08 16:47:42 +00:00
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
err = newPasswordEntry.Insert(ctx, af.db, boil.Infer())
|
2021-02-08 16:47:42 +00:00
|
|
|
if err != nil {
|
2021-03-18 16:49:52 +00:00
|
|
|
return fmt.Errorf("auth/fallback: failed to insert new user: %w", err)
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 16:49:52 +00:00
|
|
|
return nil
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|