feat: add subsonic token auth and starred endpoints
This commit is contained in:
@@ -2,11 +2,17 @@ package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -32,15 +38,16 @@ type Session struct {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
db *sql.DB
|
||||
encryptionKey string
|
||||
}
|
||||
|
||||
func NewService(db *sql.DB) *Service {
|
||||
return &Service{db: db}
|
||||
func NewService(db *sql.DB, encryptionKey string) *Service {
|
||||
return &Service{db: db, encryptionKey: encryptionKey}
|
||||
}
|
||||
|
||||
func (s *Service) Login(ctx context.Context, username, password string) (Session, error) {
|
||||
user, passwordHash, err := s.findUserByUsername(ctx, username)
|
||||
user, passwordHash, _, err := s.findUserByUsername(ctx, username)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Session{}, ErrInvalidCredentials
|
||||
@@ -52,6 +59,10 @@ func (s *Service) Login(ctx context.Context, username, password string) (Session
|
||||
return Session{}, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
if err := s.storeSubsonicSecret(ctx, user.ID, password); err != nil {
|
||||
return Session{}, fmt.Errorf("store subsonic secret: %w", err)
|
||||
}
|
||||
|
||||
token, err := newToken()
|
||||
if err != nil {
|
||||
return Session{}, fmt.Errorf("generate token: %w", err)
|
||||
@@ -112,7 +123,7 @@ func (s *Service) CurrentUserBySubsonicAuth(ctx context.Context, username, passw
|
||||
return User{}, ErrUnauthorized
|
||||
}
|
||||
|
||||
user, passwordHash, err := s.findUserByUsername(ctx, username)
|
||||
user, passwordHash, encryptedSecret, err := s.findUserByUsername(ctx, username)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return User{}, ErrUnauthorized
|
||||
@@ -121,9 +132,17 @@ func (s *Service) CurrentUserBySubsonicAuth(ctx context.Context, username, passw
|
||||
}
|
||||
|
||||
if token != "" || salt != "" {
|
||||
// We only support legacy `p` auth right now because password hashes are stored using bcrypt
|
||||
// and cannot be converted back into the plain password needed for Subsonic token auth.
|
||||
return User{}, ErrUnauthorized
|
||||
if token == "" || salt == "" || encryptedSecret == "" {
|
||||
return User{}, ErrUnauthorized
|
||||
}
|
||||
secret, err := decryptSecret(encryptedSecret, s.encryptionKey)
|
||||
if err != nil {
|
||||
return User{}, ErrUnauthorized
|
||||
}
|
||||
if hexMD5(secret+salt) != strings.ToLower(token) {
|
||||
return User{}, ErrUnauthorized
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
if strings.HasPrefix(password, "enc:") {
|
||||
@@ -138,19 +157,24 @@ func (s *Service) CurrentUserBySubsonicAuth(ctx context.Context, username, passw
|
||||
return User{}, ErrUnauthorized
|
||||
}
|
||||
|
||||
if err := s.storeSubsonicSecret(ctx, user.ID, password); err != nil {
|
||||
return User{}, fmt.Errorf("store subsonic secret: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *Service) findUserByUsername(ctx context.Context, username string) (User, string, error) {
|
||||
func (s *Service) findUserByUsername(ctx context.Context, username string) (User, string, string, error) {
|
||||
var user User
|
||||
var passwordHash string
|
||||
var subsonicSecret sql.NullString
|
||||
var createdAt string
|
||||
var lastLoginAt sql.NullString
|
||||
var isAdmin int
|
||||
|
||||
err := s.db.QueryRowContext(
|
||||
ctx,
|
||||
`SELECT id, username, password_hash, is_admin, created_at, last_login_at
|
||||
`SELECT id, username, password_hash, COALESCE(subsonic_auth_secret, ''), is_admin, created_at, last_login_at
|
||||
FROM users
|
||||
WHERE username = ?`,
|
||||
username,
|
||||
@@ -158,12 +182,13 @@ func (s *Service) findUserByUsername(ctx context.Context, username string) (User
|
||||
&user.ID,
|
||||
&user.Username,
|
||||
&passwordHash,
|
||||
&subsonicSecret,
|
||||
&isAdmin,
|
||||
&createdAt,
|
||||
&lastLoginAt,
|
||||
)
|
||||
if err != nil {
|
||||
return User{}, "", err
|
||||
return User{}, "", "", err
|
||||
}
|
||||
|
||||
user.IsAdmin = isAdmin == 1
|
||||
@@ -172,7 +197,7 @@ func (s *Service) findUserByUsername(ctx context.Context, username string) (User
|
||||
user.LastLoginAt = parseTime(lastLoginAt.String)
|
||||
}
|
||||
|
||||
return user, passwordHash, nil
|
||||
return user, passwordHash, subsonicSecret.String, nil
|
||||
}
|
||||
|
||||
func (s *Service) findUserByToken(ctx context.Context, token string) (User, error) {
|
||||
@@ -224,3 +249,71 @@ func newToken() (string, error) {
|
||||
}
|
||||
return hex.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func (s *Service) storeSubsonicSecret(ctx context.Context, userID, password string) error {
|
||||
if userID == "" || password == "" {
|
||||
return nil
|
||||
}
|
||||
encrypted, err := encryptSecret(password, s.encryptionKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, `UPDATE users SET subsonic_auth_secret = ? WHERE id = ?`, encrypted, userID)
|
||||
return err
|
||||
}
|
||||
|
||||
func EncryptSubsonicSecret(value, key string) (string, error) {
|
||||
return encryptSecret(value, key)
|
||||
}
|
||||
|
||||
func encryptSecret(value, key string) (string, error) {
|
||||
block, err := aes.NewCipher(deriveKey(key))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
ciphertext := gcm.Seal(nonce, nonce, []byte(value), nil)
|
||||
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
||||
}
|
||||
|
||||
func decryptSecret(value, key string) (string, error) {
|
||||
block, err := aes.NewCipher(deriveKey(key))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(raw) < gcm.NonceSize() {
|
||||
return "", ErrUnauthorized
|
||||
}
|
||||
nonce := raw[:gcm.NonceSize()]
|
||||
ciphertext := raw[gcm.NonceSize():]
|
||||
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(plaintext), nil
|
||||
}
|
||||
|
||||
func deriveKey(key string) []byte {
|
||||
sum := sha256.Sum256([]byte(key))
|
||||
return sum[:]
|
||||
}
|
||||
|
||||
func hexMD5(value string) string {
|
||||
sum := md5.Sum([]byte(value))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user