go-ssb-room/roomdb/sqlite/auth_fallback.go

63 lines
1.7 KiB
Go
Raw Normal View History

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"
"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"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite/models"
2021-02-08 16:47:42 +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
}
// 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.
func (af AuthFallback) Check(login, password string) (interface{}, error) {
2021-02-08 16:47:42 +00:00
ctx := context.Background()
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")
}
return found.R.Member.ID, nil
2021-02-08 16:47:42 +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 {
return fmt.Errorf("auth/fallback: failed to hash password for new user")
2021-02-08 16:47:42 +00:00
}
newPasswordEntry.PasswordHash = hashed
2021-02-08 16:47:42 +00:00
err = newPasswordEntry.Insert(ctx, af.db, boil.Infer())
2021-02-08 16:47:42 +00:00
if err != nil {
return fmt.Errorf("auth/fallback: failed to insert new user: %w", err)
2021-02-08 16:47:42 +00:00
}
return nil
2021-02-08 16:47:42 +00:00
}