Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/auth_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (app *ApiServer) isAuthorizedRequest(ctx context.Context, userId int32, aut
return true
}

if authedWallet == "" {
return false
}

cacheKey := fmt.Sprintf("%d:%s", userId, authedWallet)
if hit, ok := app.resolveGrantCache.Get(cacheKey); ok {
return hit
Expand Down Expand Up @@ -131,6 +135,26 @@ func (app *ApiServer) authMiddleware(c *fiber.Ctx) error {
return c.Next()
}

// Middleware to require auth for the userId in the route params
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this the case for all requests currently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. If you pass a user_id query param, we check against that for contextual requests.
But for most routes, if you leave out the query param, we just leave out contextual info.
This is stricter in that we validate your wallet against the userId in the route itself.

// Returns a 403 if the authedWallet is not authorized to act on behalf of the userId
// Should be placed after authMiddleware
func (app *ApiServer) requireAuthForUserId(c *fiber.Ctx) error {
wallet := c.Locals("authedWallet").(string)
userId := app.getUserId(c)
if !app.isAuthorizedRequest(c.Context(), userId, wallet) {
return fiber.NewError(
fiber.StatusForbidden,
fmt.Sprintf(
"You are not authorized to make this request authedWallet=%s userId=%d",
wallet,
userId,
),
)
}

return c.Next()
}

// Middleware that asserts that there is an authed wallet
func (app *ApiServer) requireAuthMiddleware(c *fiber.Ctx) error {
authedWallet := app.getAuthedWallet(c)
Expand Down
14 changes: 9 additions & 5 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func NewApiServer(config config.Config) *ApiServer {
}),
commsRpcProcessor: commsRpcProcessor,
env: config.Env,
audiusAppUrl: config.AudiusAppUrl,
skipAuthCheck: skipAuthCheck,
pool: pool,
writePool: writePool,
Expand Down Expand Up @@ -358,11 +359,13 @@ func NewApiServer(config config.Config) *ApiServer {
g.Get("/users/:userId/transactions/usdc/count", app.v1UsersTransactionsUsdcCount)
g.Get("/users/:userId/history/tracks", app.v1UsersHistory)
g.Get("/users/:userId/listen_counts_monthly", app.v1UsersListenCountsMonthly)
g.Get("/users/:userId/purchases", app.v1UsersPurchases)
g.Get("/users/:userId/purchases/count", app.v1UsersPurchasesCount)
g.Get("/users/:userId/sales", app.v1UsersSales)
g.Get("/users/:userId/sales/count", app.v1UsersSalesCount)
g.Get("/users/:userId/sales/aggregate", app.v1UsersSalesAggregate)
g.Get("/users/:userId/purchases", app.requireAuthForUserId, app.v1UsersPurchases)
g.Get("/users/:userId/purchases/count", app.requireAuthForUserId, app.v1UsersPurchasesCount)
g.Get("/users/:userId/sales", app.requireAuthForUserId, app.v1UsersSales)
g.Get("/users/:userId/sales/download", app.requireAuthForUserId, app.v1UsersSalesDownloadCsv)
g.Get("/users/:userId/sales/download/json", app.requireAuthForUserId, app.v1UsersSalesDownloadJson)
g.Get("/users/:userId/sales/count", app.requireAuthForUserId, app.v1UsersSalesCount)
g.Get("/users/:userId/sales/aggregate", app.requireAuthForUserId, app.v1UsersSalesAggregate)
g.Get("/users/:userId/muted", app.v1UsersMuted)
g.Get("/users/:userId/subscribers", app.v1UsersSubscribers)
g.Get("/users/:userId/remixers", app.v1UsersRemixers)
Expand Down Expand Up @@ -607,6 +610,7 @@ type ApiServer struct {
validators []config.Node
env string
auds *sdk.AudiusdSDK
audiusAppUrl string
skipAuthCheck bool // set to true in a test if you don't care about auth middleware
metricsCollector *MetricsCollector
birdeyeClient BirdeyeClient
Expand Down
215 changes: 215 additions & 0 deletions api/v1_users_sales_download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package api

import (
"encoding/csv"
"fmt"
"strconv"
"strings"
"time"

"api.audius.co/trashid"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
)

type GetUsersSalesDownloadQueryParams struct {
GranteeUserID *trashid.HashId `query:"grantee_user_id"`
}

type UsdcPurchaseWithEmailResponse struct {
Sales []UsdcPurchaseWithEmail `json:"sales"`
}

type UsdcPurchaseWithEmail struct {
Title string `db:"title" json:"title"`
Link string `db:"link" json:"link"`
PurchasedBy string `db:"purchased_by" json:"purchased_by"`
CreatedAt time.Time `db:"created_at" json:"date"`
SalePrice float64 `db:"sale_price" json:"sale_price"`
NetworkFee float64 `db:"-" json:"network_fee"`
PayExtra float64 `db:"pay_extra" json:"pay_extra"`
Splits []Split `db:"splits" json:"-"`
Total float64 `db:"-" json:"total"`
Country string `db:"country" json:"country"`
EncryptedEmail *string `db:"encrypted_email" json:"encrypted_email"`
EncryptedKey *string `db:"encrypted_key" json:"encrypted_key"`
BuyerUserID *int `db:"buyer_user_id" json:"buyer_user_id"`
IsInitial *bool `db:"is_initial" json:"is_initial"`
PubkeyBase64 *string `db:"pubkey_base64" json:"pubkey_base64"`
}

func (app *ApiServer) userSalesForDownload(c *fiber.Ctx) ([]UsdcPurchaseWithEmail, error) {
userId := app.getUserId(c)
params := GetUsersSalesDownloadQueryParams{}
if err := app.ParseAndValidateQueryParams(c, &params); err != nil {
return nil, err
}

emailAccessJoin := `LEFT OUTER JOIN email_access ON
email_access.email_owner_user_id = purchases_with_content.buyer_user_id
AND email_access.receiving_user_id = @sellerUserId
AND email_access.grantor_user_id = purchases_with_content.buyer_user_id`
if params.GranteeUserID != nil {
emailAccessJoin = `LEFT OUTER JOIN email_access ON
email_access.email_owner_user_id = purchases_with_content.buyer_user_id
AND email_access.receiving_user_id = @granteeUserId
AND email_access.grantor_user_id = @sellerUserId`
}

var sellerHandle string
err := app.pool.QueryRow(c.Context(), "SELECT handle FROM users WHERE user_id = @sellerUserId", pgx.NamedArgs{
"sellerUserId": userId,
}).Scan(&sellerHandle)
if err != nil {
return nil, err
}

sql := `
WITH purchases AS (
SELECT * FROM usdc_purchases
WHERE seller_user_id = @sellerUserId
),
purchases_with_content AS (
-- Playlists
SELECT purchases.*,
playlists.playlist_name AS content_title,
playlists.playlist_owner_id AS owner_id,
playlist_routes.slug AS content_slug
FROM purchases
JOIN playlists ON playlists.playlist_id = purchases.content_id
JOIN playlist_routes ON playlist_routes.playlist_id = purchases.content_id
WHERE (content_type = 'playlist' OR content_type = 'album')
-- Tracks
UNION ALL (
SELECT purchases.*,
tracks.title AS content_title,
tracks.owner_id AS owner_id,
track_routes.slug AS content_slug
FROM purchases
JOIN tracks ON tracks.track_id = purchases.content_id
JOIN track_routes ON track_routes.track_id = purchases.content_id
WHERE content_type = 'track'
)
)
SELECT
purchases_with_content.content_title AS title,
COALESCE(@linkBasePath || purchases_with_content.content_slug, '') AS link,
purchases_with_content.created_at,
purchases_with_content.amount / 1000000 AS sale_price,
purchases_with_content.extra_amount / 1000000 AS pay_extra,
purchases_with_content.splits,
COALESCE(purchases_with_content.country, '') AS country,
COALESCE(users.name, '') AS purchased_by,
users.user_id AS buyer_user_id,
encrypted_emails.encrypted_email,
email_access.encrypted_key,
email_access.is_initial,
user_pubkeys.pubkey_base64
FROM purchases_with_content
JOIN users ON users.user_id = purchases_with_content.buyer_user_id
LEFT OUTER JOIN encrypted_emails ON encrypted_emails.email_owner_user_id = purchases_with_content.buyer_user_id
` + emailAccessJoin + `
LEFT OUTER JOIN user_pubkeys ON user_pubkeys.user_id = purchases_with_content.buyer_user_id
ORDER BY purchases_with_content.created_at DESC;`

rows, err := app.pool.Query(c.Context(), sql, pgx.NamedArgs{
"sellerUserId": userId,
"granteeUserId": params.GranteeUserID,
"linkBasePath": fmt.Sprintf("%s/%s/", app.audiusAppUrl, sellerHandle),
})
if err != nil {
return nil, err
}

sales, err := pgx.CollectRows(rows, pgx.RowToStructByName[UsdcPurchaseWithEmail])
if err != nil {
return nil, err
}

for i := range sales {
total := 0
networkFee := 0
for _, split := range sales[i].Splits {
if split.PayoutWallet == app.solanaConfig.StakingBridgeUsdcTokenAccount.String() {
networkFee -= int(split.Amount)
} else if split.UserID != nil && int(userId) == *split.UserID {
total += int(split.Amount)
}
}
sales[i].Total = float64(total) / 1000000
sales[i].NetworkFee = float64(networkFee) / 1000000
}

return sales, nil
}

func (app *ApiServer) v1UsersSalesDownloadJson(c *fiber.Ctx) error {
sales, err := app.userSalesForDownload(c)
if err != nil {
return err
}

return c.JSON(fiber.Map{
"data": UsdcPurchaseWithEmailResponse{
Sales: sales,
},
})
}

func (app *ApiServer) v1UsersSalesDownloadCsv(c *fiber.Ctx) error {
sales, err := app.userSalesForDownload(c)
if err != nil {
return err
}

// Set CSV content type header
c.Set("Content-Type", "text/csv")
c.Set("Content-Disposition", "attachment; filename=\"sales.csv\"")

// Create CSV writer
var csvBuilder strings.Builder
writer := csv.NewWriter(&csvBuilder)

// Note: csv headers use spaces instead of underscores
headers := []string{
"title",
"link",
"purchased by",
"date",
"sale price",
"network_fee",
"pay extra",
"total",
"country",
}

if err := writer.Write(headers); err != nil {
return err
}

// Write data rows
for _, sale := range sales {
record := []string{
sale.Title,
sale.Link,
sale.PurchasedBy,
sale.CreatedAt.Format(time.RFC3339),
strconv.FormatFloat(sale.SalePrice, 'f', 6, 64),
strconv.FormatFloat(sale.NetworkFee, 'f', 6, 64),
strconv.FormatFloat(sale.PayExtra, 'f', 6, 64),
strconv.FormatFloat(sale.Total, 'f', 6, 64),
sale.Country,
}

if err := writer.Write(record); err != nil {
return err
}
}

writer.Flush()
if err := writer.Error(); err != nil {
return err
}

return c.SendString(csvBuilder.String())
}
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Config struct {
CommsMessagePush bool
AudiusdChainID uint
AudiusdEntityManagerAddress string
AudiusAppUrl string
}

var Cfg = Config{
Expand Down Expand Up @@ -91,6 +92,7 @@ func init() {

Cfg.AudiusdChainID = core_config.DevAcdcChainID
Cfg.AudiusdEntityManagerAddress = core_config.DevAcdcAddress
Cfg.AudiusAppUrl = "http://localhost:3000"
case "stage":
fallthrough
case "staging":
Expand All @@ -106,6 +108,7 @@ func init() {

Cfg.AudiusdChainID = core_config.StageAcdcChainID
Cfg.AudiusdEntityManagerAddress = core_config.StageAcdcAddress
Cfg.AudiusAppUrl = "https://staging.audius.co"
case "prod":
fallthrough
case "production":
Expand All @@ -120,9 +123,9 @@ func init() {
Cfg.Rewards = core_config.MakeRewards(core_config.ProdClaimAuthorities, core_config.ProdRewardExtensions)
Cfg.AudiusdURL = "creatornode.audius.co"
Cfg.ChainId = "audius-mainnet-alpha-beta"

Cfg.AudiusdChainID = core_config.ProdAcdcChainID
Cfg.AudiusdEntityManagerAddress = core_config.ProdAcdcAddress
Cfg.AudiusAppUrl = "https://audius.co"
default:
log.Fatalf("Unknown environment: %s", env)
}
Expand Down
Loading