Phase 2: Subsonic REST API framework (auth, response formatting, error handling, routing) plus user permission model
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.noroutes.net/NoRoutes/vibetunes/internal/database"
|
||||
)
|
||||
|
||||
// The legacy Subsonic protocol's token authentication scheme
|
||||
// (t = md5(password + salt)) can only be verified by a server that
|
||||
// knows the plaintext password. VibeSonic's native login hashes
|
||||
// passwords with Argon2id, which is one-way by design and cannot answer
|
||||
// that check. Every Subsonic-compatible server that supports token auth
|
||||
// solves this the same way: keep a second, reversible copy of the
|
||||
// password around specifically for this purpose, encrypted at rest
|
||||
// (never plaintext) with a server-side key.
|
||||
//
|
||||
// EncryptionKey resolves that key: the operator-configured
|
||||
// VIBESONIC_SESSION_SECRET if set (sha256'd to the right size), or
|
||||
// otherwise a random 32-byte key generated once and persisted in the
|
||||
// settings table.
|
||||
|
||||
const settingsKeyAuthKey = "subsonic_auth_encryption_key"
|
||||
|
||||
func EncryptionKey(db *sql.DB, configuredSecret string) ([]byte, error) {
|
||||
if configuredSecret != "" {
|
||||
sum := sha256.Sum256([]byte(configuredSecret))
|
||||
return sum[:], nil
|
||||
}
|
||||
if existing := database.GetSetting(db, settingsKeyAuthKey, ""); existing != "" {
|
||||
key, err := base64.StdEncoding.DecodeString(existing)
|
||||
if err == nil && len(key) == 32 {
|
||||
return key, nil
|
||||
}
|
||||
}
|
||||
key := make([]byte, 32)
|
||||
if _, err := rand.Read(key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := database.SetSetting(db, settingsKeyAuthKey, base64.StdEncoding.EncodeToString(key)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// EncryptPassword returns an AES-256-GCM encrypted, base64-encoded copy
|
||||
// of plaintext for storage in users.subsonic_auth_secret.
|
||||
func EncryptPassword(key []byte, plaintext string) (string, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
var ErrCannotDecrypt = errors.New("cannot decrypt stored password")
|
||||
|
||||
// DecryptPassword reverses EncryptPassword.
|
||||
func DecryptPassword(key []byte, encoded string) (string, error) {
|
||||
raw, err := base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil {
|
||||
return "", ErrCannotDecrypt
|
||||
}
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(raw) < gcm.NonceSize() {
|
||||
return "", ErrCannotDecrypt
|
||||
}
|
||||
nonce, ciphertext := raw[:gcm.NonceSize()], raw[gcm.NonceSize():]
|
||||
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: %v", ErrCannotDecrypt, err)
|
||||
}
|
||||
return string(plaintext), nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"git.noroutes.net/NoRoutes/vibetunes/internal/database"
|
||||
)
|
||||
|
||||
// SetUserPassword hashes plaintext for native login and also stores the
|
||||
// encrypted copy legacy Subsonic token authentication needs (see
|
||||
// subsonic_secret.go), keeping both in sync. This is the only path that
|
||||
// should ever change a user's password.
|
||||
func SetUserPassword(db *sql.DB, encryptionKey []byte, userID, plaintext string) error {
|
||||
hash, err := HashPassword(plaintext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := database.SetUserPassword(db, userID, hash); err != nil {
|
||||
return err
|
||||
}
|
||||
encrypted, err := EncryptPassword(encryptionKey, plaintext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return database.SetUserSubsonicAuthSecret(db, userID, encrypted)
|
||||
}
|
||||
Reference in New Issue
Block a user