e871b05b38
CI / Test (linux/amd64) (pull_request) Successful in 34s
CI / Lint (pull_request) Failing after 16s
CI / Build (windows/amd64) (pull_request) Successful in 22s
CI / Build (linux/amd64) (pull_request) Successful in 20s
CI / Build (linux/arm64) (pull_request) Successful in 21s
Cleanup pass over the repo so CI can enforce lint going forward
without the only-new-issues escape hatch:
* gofumpt -w across the tree (31 hits, all formatting)
* misspell --fix (25 hits, US-locale spelling) — but reverted on
api.JobCancelled = "cancelled" since that literal is the wire +
DB CHECK constraint value, plus matched the case in store/fleet.go
back to "cancelled" and added //nolint:misspell on both for the
next time someone reaches for the auto-fix
* Wrap every `defer rows.Close()` / `defer stmt.Close()` /
`defer res.Body.Close()` in `defer func() { _ = .Close() }()`
to satisfy errcheck without losing the close itself
* websocket.Dial callers (1 prod, 4 tests) now capture + close the
upgrade response Body — coder/websocket can return res with a nil
Body on success, so the test deferred-closes guard against that
* Annotate the two genuine-by-design nilerr cases with //nolint
comments explaining why nil-on-error is the contract (cookie
missing = no session; ctx cancelled mid-backoff = clean shutdown)
* Add brief godoc on the 10 exported const groups + types that
revive flagged (api.HostOS/HostArch/JobKind/JobStatus/LogStream/
ErrorCode, restic.EventKind, store.Role, web.FS)
* Drop the unused (*Server).userByID method
* Inline the unparam baseView(active) — every UI page is under
the dashboard primary nav today
Result: `golangci-lint run ./...` reports 0 issues. CI lint job
no longer needs only-new-issues: true; X-06 follow-up entry in
tasks.md removed.
91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
// Package auth handles password hashing (argon2id), session
|
|
// management, CSRF tokens, and bearer-token verification for agents.
|
|
package auth
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/argon2"
|
|
)
|
|
|
|
// argon2id parameters following RFC 9106 §4 "second
|
|
// recommended option" (memory-constrained):
|
|
// - 64 MiB memory, 3 iterations, 4 lanes, 32-byte tag.
|
|
//
|
|
// These are tunable per-deployment if a beefy controller wants to
|
|
// crank them; we ship a defensible default.
|
|
const (
|
|
defaultMemoryKiB = 64 * 1024
|
|
defaultIterations = 3
|
|
defaultParallel = 4
|
|
defaultSaltLen = 16
|
|
defaultKeyLen = 32
|
|
)
|
|
|
|
// HashPassword returns an argon2id-encoded string of the form
|
|
//
|
|
// $argon2id$v=19$m=...,t=...,p=...$<salt>$<hash>
|
|
//
|
|
// safe to store in a TEXT column. The salt is freshly random per call.
|
|
func HashPassword(password string) (string, error) {
|
|
salt := make([]byte, defaultSaltLen)
|
|
if _, err := rand.Read(salt); err != nil {
|
|
return "", fmt.Errorf("auth: read salt: %w", err)
|
|
}
|
|
hash := argon2.IDKey([]byte(password), salt,
|
|
defaultIterations, defaultMemoryKiB, defaultParallel, defaultKeyLen)
|
|
|
|
return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
|
argon2.Version,
|
|
defaultMemoryKiB, defaultIterations, defaultParallel,
|
|
base64.RawStdEncoding.EncodeToString(salt),
|
|
base64.RawStdEncoding.EncodeToString(hash),
|
|
), nil
|
|
}
|
|
|
|
// VerifyPassword returns nil if password matches the encoded hash.
|
|
// On any decode error or mismatch the error is non-nil — callers
|
|
// should treat all non-nil returns as "invalid credentials" and not
|
|
// leak which case it was.
|
|
func VerifyPassword(encoded, password string) error {
|
|
parts := strings.Split(encoded, "$")
|
|
// "$argon2id$v=...$m=...,t=...,p=...$<salt>$<hash>" → 6 parts (leading empty)
|
|
if len(parts) != 6 || parts[1] != "argon2id" {
|
|
return errors.New("auth: unrecognized hash format")
|
|
}
|
|
var version int
|
|
if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil {
|
|
return fmt.Errorf("auth: parse version: %w", err)
|
|
}
|
|
if version != argon2.Version {
|
|
return fmt.Errorf("auth: unsupported argon2 version %d", version)
|
|
}
|
|
var memory, iterations uint32
|
|
var parallel uint8
|
|
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d",
|
|
&memory, &iterations, ¶llel); err != nil {
|
|
return fmt.Errorf("auth: parse params: %w", err)
|
|
}
|
|
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
|
if err != nil {
|
|
return fmt.Errorf("auth: decode salt: %w", err)
|
|
}
|
|
want, err := base64.RawStdEncoding.DecodeString(parts[5])
|
|
if err != nil {
|
|
return fmt.Errorf("auth: decode hash: %w", err)
|
|
}
|
|
|
|
got := argon2.IDKey([]byte(password), salt,
|
|
iterations, memory, parallel, uint32(len(want)))
|
|
|
|
if subtle.ConstantTimeCompare(got, want) != 1 {
|
|
return errors.New("auth: invalid password")
|
|
}
|
|
return nil
|
|
}
|