Fixes to password provider

This commit is contained in:
Melon 2025-03-13 23:05:05 +00:00
parent 4300165ae3
commit ba76dc5371
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
3 changed files with 12 additions and 13 deletions

View File

@ -25,14 +25,13 @@ type PasswordLogin struct {
DB passwordLoginDB
}
func (b *PasswordLogin) AccessState() process.State { return process.StateBase }
func (p *PasswordLogin) AccessState() process.State { return process.StateBase }
func (b *PasswordLogin) Name() string { return "password" }
func (p *PasswordLogin) Name() string { return "password" }
func (b *PasswordLogin) RenderTemplate(ctx authContext.TemplateContext) error {
func (p *PasswordLogin) RenderTemplate(ctx authContext.TemplateContext) error {
// TODO(melon): rewrite this
req := ctx.Request()
un := req.FormValue("login")
redirect := req.FormValue("redirect")
if redirect == "" {
redirect = "/"
@ -41,24 +40,24 @@ func (b *PasswordLogin) RenderTemplate(ctx authContext.TemplateContext) error {
UserEmail string
Redirect string
}{
UserEmail: un,
UserEmail: ctx.LoginProcessData().Email,
Redirect: redirect,
})
return nil
}
func (b *PasswordLogin) AttemptLogin(ctx authContext.FormContext) error {
func (p *PasswordLogin) AttemptLogin(ctx authContext.FormContext) error {
req := ctx.Request()
un := req.FormValue("username")
un := req.FormValue("email")
pw := req.FormValue("password")
if len(pw) < 8 {
return auth.BasicUserSafeError(http.StatusBadRequest, "Password too short")
}
login, err := b.DB.CheckLogin(ctx.Context(), un, pw)
login, err := p.DB.CheckLogin(ctx.Context(), un, pw)
switch {
case err == nil:
user, err := b.DB.GetUser(ctx.Context(), login.Subject)
user, err := p.DB.GetUser(ctx.Context(), login.Subject)
if err != nil {
return err
}

View File

@ -9,7 +9,7 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
-- name: checkLogin :one
SELECT subject, password, need_factor, email, email_verified
FROM users
WHERE users.subject = ?
WHERE users.email = ?
LIMIT 1;
-- name: GetUser :one

View File

@ -219,7 +219,7 @@ func (q *Queries) changeUserPassword(ctx context.Context, arg changeUserPassword
const checkLogin = `-- name: checkLogin :one
SELECT subject, password, need_factor, email, email_verified
FROM users
WHERE users.subject = ?
WHERE users.email = ?
LIMIT 1
`
@ -231,8 +231,8 @@ type checkLoginRow struct {
EmailVerified bool `json:"email_verified"`
}
func (q *Queries) checkLogin(ctx context.Context, subject string) (checkLoginRow, error) {
row := q.db.QueryRowContext(ctx, checkLogin, subject)
func (q *Queries) checkLogin(ctx context.Context, email string) (checkLoginRow, error) {
row := q.db.QueryRowContext(ctx, checkLogin, email)
var i checkLoginRow
err := row.Scan(
&i.Subject,