Merge mediaauth into shared/backend/auth as the unified auth-proxy engine
mediaauth was its own package only because "auth" was already taken by this package's lower-level Store/User/Role layer. Since mediaauth is no longer media-family-specific (see the earlier "unified auth proxy" generalization), it now lives directly in this package instead, renamed where it collided with existing exports: mediaauth.User -> ProxyUser, mediaauth.Store -> ProxyStore (this package's own local-account Store is unrelated and keeps its name). Service/New/NewWithStore/UserFromContext and the rest keep their names. mediacore.go updated to match. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c9737d1f9f
commit
5364588ba0
4 files changed
+258
-242
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,28 @@
|
||||
package auth
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeProxyCredentials(t *testing.T) {
|
||||
email, username, password, err := normalizeProxyCredentials(" User@Example.com ", "user", "correct horse battery staple")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if email != "user@example.com" || username != "user" || password == "" {
|
||||
t.Fatalf("normalized credentials = %q, %q, %q", email, username, password)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeProxyCredentialsAllowsAdminEmailOnly(t *testing.T) {
|
||||
if _, _, _, err := normalizeProxyCredentials("admin@example.com", "", "correct horse battery staple"); err != nil {
|
||||
t.Fatalf("empty optional username rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePlatformRole(t *testing.T) {
|
||||
if normalizePlatformRole("MODERATOR") != "moderator" {
|
||||
t.Fatal("moderator role was not normalized")
|
||||
}
|
||||
if normalizePlatformRole("unknown") != "user" {
|
||||
t.Fatal("unknown role was not reduced to user")
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package mediaauth
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeCredentials(t *testing.T) {
|
||||
email, username, password, err := normalizeCredentials(" User@Example.com ", "user", "correct horse battery staple")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if email != "user@example.com" || username != "user" || password == "" {
|
||||
t.Fatalf("normalized credentials = %q, %q, %q", email, username, password)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeCredentialsAllowsAdminEmailOnly(t *testing.T) {
|
||||
if _, _, _, err := normalizeCredentials("admin@example.com", "", "correct horse battery staple"); err != nil {
|
||||
t.Fatalf("empty optional username rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRole(t *testing.T) {
|
||||
if normalizeRole("MODERATOR") != "moderator" {
|
||||
t.Fatal("moderator role was not normalized")
|
||||
}
|
||||
if normalizeRole("unknown") != "user" {
|
||||
t.Fatal("unknown role was not reduced to user")
|
||||
}
|
||||
}
|
||||
@@ -45,15 +45,15 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
sharedauth "repos.gfranz.ch/Gabriel20xx/shared/backend/auth"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/carriernodeclient"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/mediaauth"
|
||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/syncsocket"
|
||||
)
|
||||
|
||||
// Config wires a Core to the rest of one service's dependencies.
|
||||
type Config struct {
|
||||
DB *pgxpool.Pool
|
||||
Auth *mediaauth.Service
|
||||
Auth *sharedauth.Service
|
||||
Sync *syncsocket.Hub
|
||||
CarrierNode *carriernodeclient.Client
|
||||
// ServiceName is recorded on mutations that track which app produced
|
||||
@@ -65,7 +65,7 @@ type Config struct {
|
||||
// what they need to run.
|
||||
type Core struct {
|
||||
db *pgxpool.Pool
|
||||
auth *mediaauth.Service
|
||||
auth *sharedauth.Service
|
||||
sync *syncsocket.Hub
|
||||
carrierNode *carriernodeclient.Client
|
||||
serviceName string
|
||||
@@ -214,7 +214,7 @@ func (c *Core) mediaState(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "missing mediaKey")
|
||||
return
|
||||
}
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var likes, saves, views int64
|
||||
if err := c.db.QueryRow(r.Context(), `SELECT COUNT(*) FROM media_stats WHERE media_key = $1 AND stat_type = 'like'`, key).Scan(&likes); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "media state unavailable")
|
||||
@@ -240,7 +240,7 @@ func (c *Core) mediaSave(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) mediaStatMutation(w http.ResponseWriter, r *http.Request, statType string) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeError(w, http.StatusUnauthorized, "not authenticated")
|
||||
return
|
||||
@@ -283,7 +283,7 @@ func (c *Core) mediaStatMutation(w http.ResponseWriter, r *http.Request, statTyp
|
||||
}
|
||||
|
||||
func (c *Core) mediaView(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
MediaKey string `json:"mediaKey"`
|
||||
}
|
||||
@@ -307,7 +307,7 @@ func (c *Core) mediaView(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) mediaViewSession(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
MediaKey string `json:"mediaKey"`
|
||||
DurationMs int64 `json:"durationMs"`
|
||||
@@ -338,7 +338,7 @@ func (c *Core) mediaViewSession(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) mediaLiked(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
rows, err := c.db.Query(r.Context(), `SELECT media_key, created_at FROM media_stats WHERE user_id = $1 AND stat_type = 'like' ORDER BY created_at DESC`, user.ID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "liked media unavailable")
|
||||
@@ -379,7 +379,7 @@ func (c *Core) mediaTags(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) addMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
@@ -402,7 +402,7 @@ func (c *Core) addMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) voteMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
Vote int `json:"vote"`
|
||||
}
|
||||
@@ -468,7 +468,7 @@ func (c *Core) tagMedia(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlists(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
rows, err := c.db.Query(r.Context(), `SELECT id, name, created_at FROM playlists WHERE user_id=$1 ORDER BY created_at DESC`, user.ID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "playlists unavailable")
|
||||
@@ -488,7 +488,7 @@ func (c *Core) playlists(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlistSummary(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeJSON(w, http.StatusOK, success(map[string]any{"playlists": []any{}}, "No playlists"))
|
||||
return
|
||||
@@ -512,7 +512,7 @@ func (c *Core) playlistSummary(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil || user == nil {
|
||||
writeError(w, http.StatusNotFound, "playlist not found")
|
||||
@@ -528,7 +528,7 @@ func (c *Core) playlist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) createPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
var input struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
@@ -552,7 +552,7 @@ func (c *Core) createPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) deletePlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -567,7 +567,7 @@ func (c *Core) deletePlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) playlistItems(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil || user == nil {
|
||||
writeError(w, http.StatusNotFound, "playlist not found")
|
||||
@@ -598,7 +598,7 @@ func (c *Core) playlistItems(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) addPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -631,7 +631,7 @@ func (c *Core) addPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) deletePlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -655,7 +655,7 @@ func (c *Core) deletePlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) randomPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeError(w, http.StatusNotFound, "playlist not found")
|
||||
return
|
||||
@@ -674,7 +674,7 @@ func (c *Core) randomPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) reorderPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
||||
@@ -699,7 +699,7 @@ func (c *Core) reorderPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (c *Core) profile(w http.ResponseWriter, r *http.Request) {
|
||||
user := mediaauth.UserFromContext(r.Context())
|
||||
user := sharedauth.UserFromContext(r.Context())
|
||||
if user == nil {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"user": nil})
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user