mirror of
https://github.com/D-Modem/login.git
synced 2026-11-29 12:58:06 -08:00
A signup made the account in the users file only. Now it makes the account in each backend of [general] auth that is set up to make accounts: the users file, SQL ([sql] insert_query), LDAP ([ldap] add_dn, added as add_bind_dn, the password set by Password Modify or unicodePwd for AD) and system accounts ([system] create_command, with a useradd sample script). [signup] backends limits the list. Before the account is made, each backend checks that the name is free. If one backend fails, the accounts already made are taken back (delete_query, entry delete, remove_command). With pending = yes, each backend must lock the account, or dmodem-login does not start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRRfuh3h6uNuHtaugux
166 lines
4.9 KiB
Go
166 lines
4.9 KiB
Go
// Copyright (C) 2026 D-Modem Authors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/D-Modem/login/internal/config"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
_ "github.com/lib/pq"
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
// SQL: query, with the user (and the password, for args = user
|
|
// password) as its arguments, returns the stored password of the user
|
|
// (checked as hash says), or with hash = none any row for a valid login
|
|
type SQL struct {
|
|
driver string
|
|
dsn string
|
|
query string
|
|
args []string
|
|
scheme string
|
|
timeout time.Duration
|
|
db *sql.DB
|
|
// changing passwords (mode = passwd items): update_query with
|
|
// update_args, the password as update_hash says
|
|
updateQuery string
|
|
updateArgs []string
|
|
updateHash string
|
|
// new accounts (signups): insert_query with insert_args, the password
|
|
// as insert_hash says; exists_query and delete_query with the user
|
|
insertQuery string
|
|
insertArgs []string
|
|
insertHash string
|
|
existsQuery string
|
|
deleteQuery string
|
|
}
|
|
|
|
var sqlDrivers = map[string]string{
|
|
"mysql": "mysql",
|
|
"mariadb": "mysql",
|
|
"postgres": "postgres",
|
|
"postgresql": "postgres",
|
|
"sqlite": "sqlite",
|
|
"sqlite3": "sqlite",
|
|
}
|
|
|
|
// NewSQL: the [sql] backend, its database opened (not yet
|
|
// connected); args names the query's parameters (user, password)
|
|
func NewSQL(s *config.Section) (Authenticator, error) {
|
|
var err error
|
|
a := &SQL{
|
|
dsn: s.Get("dsn", ""),
|
|
query: s.Get("query", ""),
|
|
args: config.Fields(strings.ToLower(s.Get("args", "user"))),
|
|
scheme: strings.ToLower(s.Get("hash", "auto")),
|
|
}
|
|
drv := strings.ToLower(s.Get("driver", ""))
|
|
if a.driver = sqlDrivers[drv]; a.driver == "" {
|
|
return nil, fmt.Errorf("[sql] driver: %q (mysql, postgres or sqlite)", drv)
|
|
}
|
|
if a.dsn == "" || a.query == "" {
|
|
return nil, fmt.Errorf("[sql] needs dsn and query")
|
|
}
|
|
for _, arg := range a.args {
|
|
if arg != "user" && arg != "password" {
|
|
return nil, fmt.Errorf("[sql] args: %q (user, password)", arg)
|
|
}
|
|
}
|
|
if a.timeout, err = s.Duration("timeout", 10*time.Second); err != nil {
|
|
return nil, err
|
|
}
|
|
a.updateQuery = s.Get("update_query", "")
|
|
a.updateArgs = config.Fields(strings.ToLower(s.Get("update_args", "password user")))
|
|
a.updateHash = strings.ToLower(s.Get("update_hash", "bcrypt"))
|
|
for _, arg := range a.updateArgs {
|
|
if arg != "user" && arg != "password" && arg != "old_password" {
|
|
return nil, fmt.Errorf("[sql] update_args: %q (user, password, old_password)", arg)
|
|
}
|
|
}
|
|
if a.updateHash != "bcrypt" && a.updateHash != "none" {
|
|
return nil, fmt.Errorf("[sql] update_hash: %q (bcrypt, or none: the password as typed, for a query that hashes it)", a.updateHash)
|
|
}
|
|
a.insertQuery = s.Get("insert_query", "")
|
|
a.insertArgs = config.Fields(strings.ToLower(s.Get("insert_args", "user password")))
|
|
a.insertHash = strings.ToLower(s.Get("insert_hash", a.updateHash))
|
|
a.existsQuery = s.Get("exists_query", "")
|
|
a.deleteQuery = s.Get("delete_query", "")
|
|
for _, arg := range a.insertArgs {
|
|
if arg != "user" && arg != "password" && arg != "pending" && arg != "note" {
|
|
return nil, fmt.Errorf("[sql] insert_args: %q (user, password, pending, note)", arg)
|
|
}
|
|
}
|
|
if a.insertQuery != "" && !contains(a.insertArgs, "user") {
|
|
return nil, fmt.Errorf("[sql] insert_args: no user")
|
|
}
|
|
if a.insertHash != "bcrypt" && a.insertHash != "none" {
|
|
return nil, fmt.Errorf("[sql] insert_hash: %q (bcrypt, or none: the password as typed, for a query that hashes it)", a.insertHash)
|
|
}
|
|
// opening does not connect: a database that is down only fails the
|
|
// logins that reach it
|
|
if a.db, err = sql.Open(a.driver, a.dsn); err != nil {
|
|
return nil, fmt.Errorf("[sql] %v", err)
|
|
}
|
|
a.db.SetMaxOpenConns(1)
|
|
return a, nil
|
|
}
|
|
|
|
// Name: the backend's Name, for {auth} and the log
|
|
func (a *SQL) Name() string { return "sql" }
|
|
|
|
// Authenticate: the query's row for the user, its first column the
|
|
// stored hash checked against the password (hash = none: a row is enough)
|
|
func (a *SQL) Authenticate(ctx context.Context, user, password string) error {
|
|
ctx, cancel := context.WithTimeout(ctx, a.timeout)
|
|
defer cancel()
|
|
args := make([]any, len(a.args))
|
|
for i, arg := range a.args {
|
|
if arg == "user" {
|
|
args[i] = user
|
|
} else {
|
|
args[i] = password
|
|
}
|
|
}
|
|
var stored sql.NullString
|
|
row := a.db.QueryRowContext(ctx, a.query, args...)
|
|
var err error
|
|
if a.scheme == "none" {
|
|
err = row.Scan(&discard{})
|
|
} else {
|
|
err = row.Scan(&stored)
|
|
}
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNoUser
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if a.scheme == "none" {
|
|
return nil
|
|
}
|
|
if !stored.Valid {
|
|
return ErrBadPassword
|
|
}
|
|
ok, err := CheckPassword(stored.String, password, a.scheme)
|
|
if err != nil {
|
|
return fmt.Errorf("user %s: %v", user, err)
|
|
}
|
|
if !ok {
|
|
return ErrBadPassword
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// discard scans and drops a column of any type
|
|
type discard struct{}
|
|
|
|
// Scan: drops the column (sql.Scanner)
|
|
func (*discard) Scan(any) error { return nil }
|