This commit is contained in:
1 parent
899223270a
commit
da15c3a7b7
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/go-chi/chi/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"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/carriernodeclient"
|
||||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/mediaauth"
|
|
||||||
"repos.gfranz.ch/Gabriel20xx/shared/backend/syncsocket"
|
"repos.gfranz.ch/Gabriel20xx/shared/backend/syncsocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config wires a Core to the rest of one service's dependencies.
|
// Config wires a Core to the rest of one service's dependencies.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DB *pgxpool.Pool
|
DB *pgxpool.Pool
|
||||||
Auth *mediaauth.Service
|
Auth *sharedauth.Service
|
||||||
Sync *syncsocket.Hub
|
Sync *syncsocket.Hub
|
||||||
CarrierNode *carriernodeclient.Client
|
CarrierNode *carriernodeclient.Client
|
||||||
// ServiceName is recorded on mutations that track which app produced
|
// ServiceName is recorded on mutations that track which app produced
|
||||||
@@ -65,7 +65,7 @@ type Config struct {
|
|||||||
// what they need to run.
|
// what they need to run.
|
||||||
type Core struct {
|
type Core struct {
|
||||||
db *pgxpool.Pool
|
db *pgxpool.Pool
|
||||||
auth *mediaauth.Service
|
auth *sharedauth.Service
|
||||||
sync *syncsocket.Hub
|
sync *syncsocket.Hub
|
||||||
carrierNode *carriernodeclient.Client
|
carrierNode *carriernodeclient.Client
|
||||||
serviceName string
|
serviceName string
|
||||||
@@ -214,7 +214,7 @@ func (c *Core) mediaState(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeError(w, http.StatusBadRequest, "missing mediaKey")
|
writeError(w, http.StatusBadRequest, "missing mediaKey")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
var likes, saves, views int64
|
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 {
|
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")
|
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) {
|
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 {
|
if user == nil {
|
||||||
writeError(w, http.StatusUnauthorized, "not authenticated")
|
writeError(w, http.StatusUnauthorized, "not authenticated")
|
||||||
return
|
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) {
|
func (c *Core) mediaView(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
var input struct {
|
var input struct {
|
||||||
MediaKey string `json:"mediaKey"`
|
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) {
|
func (c *Core) mediaViewSession(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
var input struct {
|
var input struct {
|
||||||
MediaKey string `json:"mediaKey"`
|
MediaKey string `json:"mediaKey"`
|
||||||
DurationMs int64 `json:"durationMs"`
|
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) {
|
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)
|
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 {
|
if err != nil {
|
||||||
writeError(w, http.StatusInternalServerError, "liked media unavailable")
|
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) {
|
func (c *Core) addMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
var input struct {
|
var input struct {
|
||||||
Tag string `json:"tag"`
|
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) {
|
func (c *Core) voteMediaTag(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
var input struct {
|
var input struct {
|
||||||
Vote int `json:"vote"`
|
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) {
|
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)
|
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 {
|
if err != nil {
|
||||||
writeError(w, http.StatusInternalServerError, "playlists unavailable")
|
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) {
|
func (c *Core) playlistSummary(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
writeJSON(w, http.StatusOK, success(map[string]any{"playlists": []any{}}, "No playlists"))
|
writeJSON(w, http.StatusOK, success(map[string]any{"playlists": []any{}}, "No playlists"))
|
||||||
return
|
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) {
|
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)
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||||
if err != nil || user == nil {
|
if err != nil || user == nil {
|
||||||
writeError(w, http.StatusNotFound, "playlist not found")
|
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) {
|
func (c *Core) createPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
var input struct {
|
var input struct {
|
||||||
Name string `json:"name"`
|
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) {
|
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)
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
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) {
|
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)
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||||
if err != nil || user == nil {
|
if err != nil || user == nil {
|
||||||
writeError(w, http.StatusNotFound, "playlist not found")
|
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) {
|
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)
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
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) {
|
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)
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
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) {
|
func (c *Core) randomPlaylistItem(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
writeError(w, http.StatusNotFound, "playlist not found")
|
writeError(w, http.StatusNotFound, "playlist not found")
|
||||||
return
|
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) {
|
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)
|
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeError(w, http.StatusBadRequest, "invalid playlist id")
|
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) {
|
func (c *Core) profile(w http.ResponseWriter, r *http.Request) {
|
||||||
user := mediaauth.UserFromContext(r.Context())
|
user := sharedauth.UserFromContext(r.Context())
|
||||||
if user == nil {
|
if user == nil {
|
||||||
writeJSON(w, http.StatusOK, map[string]any{"user": nil})
|
writeJSON(w, http.StatusOK, map[string]any{"user": nil})
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in new issue
Block a user