navidrome/model/api_key.go
Kartik Ohri 362a8826b9 Implement OpenSubsonic's API Key authentication
Ref: https://opensubsonic.netlify.app/docs/extensions/apikeyauth/.

Add functionality for users to create, manage, and use API keys for authentication.

*   **Database:**
    *   Added a new `api_key`(`20250415111500_add_api_key_table.go`) table to store API keys associated with users.
*   **Backend:**
    *   Defined the `APIKey` model.
    *   Implemented `APIKeyRepository` for database operations.
    *   Added `FindByAPIKey` method to `UserRepository` to allow user lookup via API key.
    *   Updated Subsonic API error responses to include API key-related errors and messaging.
    *   Integrated API key authentication into the subsonic authentication middleware.
*   **UI:**
    *   Added new React components for API Key CRUD operations: `ApiKeyList`, `ApiKeyCreate`, and `ApiKeyEdit`.
    *   The API Key management section can be accessed from the settings menu.

# Conflicts:
#	model/user.go
#	server/subsonic/middlewares.go
#	server/subsonic/middlewares_test.go
#	tests/mock_data_store.go
#	tests/mock_user_repo.go
2025-08-14 02:18:11 +05:30

27 lines
627 B
Go

package model
import (
"github.com/deluan/rest"
"time"
)
type APIKey struct {
ID string `structs:"id" json:"id"`
UserID string `structs:"user_id" json:"userId"`
Name string `structs:"name" json:"name"`
Key string `structs:"key" json:"key"`
CreatedAt time.Time `structs:"created_at" json:"createdAt"`
}
type APIKeys []APIKey
type APIKeyRepository interface {
ResourceRepository
rest.Persistable
CountAll(...QueryOptions) (int64, error)
Get(id string) (*APIKey, error)
GetAll(options ...QueryOptions) (APIKeys, error)
Put(*APIKey) error
FindByKey(key string) (*APIKey, error)
}