mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-20 07:05:24 +00:00
feat(api-key): show last used IP
This commit is contained in:
1 parent
6f43986c37
commit
e0d149d511
12 files changed
+245
-6
No files matched your search
@@ -14,9 +14,11 @@ import (
|
||||
"github.com/Wei-Shaw/sub2api/ent/schema/mixins"
|
||||
"github.com/Wei-Shaw/sub2api/ent/user"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/lib/pq"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
)
|
||||
|
||||
@@ -431,10 +433,96 @@ func (r *apiKeyRepository) ListByUserID(ctx context.Context, userID int64, param
|
||||
for i := range keys {
|
||||
outKeys = append(outKeys, *apiKeyEntityToService(keys[i]))
|
||||
}
|
||||
if err := r.attachLastUsedIPs(ctx, outKeys); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return outKeys, paginationResultFromTotal(int64(total), params), nil
|
||||
}
|
||||
|
||||
func (r *apiKeyRepository) attachLastUsedIPs(ctx context.Context, keys []service.APIKey) error {
|
||||
if len(keys) == 0 || r.sql == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
apiKeyIDs := make([]int64, 0, len(keys))
|
||||
for i := range keys {
|
||||
apiKeyIDs = append(apiKeyIDs, keys[i].ID)
|
||||
}
|
||||
|
||||
lastUsedIPs, err := r.latestUsageLogIPs(ctx, apiKeyIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range keys {
|
||||
if ip, ok := lastUsedIPs[keys[i].ID]; ok {
|
||||
keys[i].LastUsedIP = &ip
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *apiKeyRepository) latestUsageLogIPs(ctx context.Context, apiKeyIDs []int64) (map[int64]string, error) {
|
||||
if len(apiKeyIDs) == 0 || r.sql == nil {
|
||||
return map[int64]string{}, nil
|
||||
}
|
||||
|
||||
query, args := latestUsageLogIPsQuery(apiKeyIDs, r.client.Driver().Dialect())
|
||||
rows, err := r.sql.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make(map[int64]string, len(apiKeyIDs))
|
||||
for rows.Next() {
|
||||
var apiKeyID int64
|
||||
var ipAddress string
|
||||
if err := rows.Scan(&apiKeyID, &ipAddress); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[apiKeyID] = ipAddress
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func latestUsageLogIPsQuery(apiKeyIDs []int64, dialectName string) (string, []any) {
|
||||
if dialectName == dialect.Postgres {
|
||||
return `
|
||||
SELECT api_key_id, ip_address
|
||||
FROM (
|
||||
SELECT api_key_id, ip_address,
|
||||
ROW_NUMBER() OVER (PARTITION BY api_key_id ORDER BY created_at DESC, id DESC) AS rn
|
||||
FROM usage_logs
|
||||
WHERE api_key_id = ANY($1::bigint[])
|
||||
AND ip_address IS NOT NULL
|
||||
AND ip_address <> ''
|
||||
) ranked
|
||||
WHERE rn = 1`, []any{pq.Array(apiKeyIDs)}
|
||||
}
|
||||
|
||||
placeholders := make([]string, len(apiKeyIDs))
|
||||
args := make([]any, len(apiKeyIDs))
|
||||
for i, id := range apiKeyIDs {
|
||||
placeholders[i] = "?"
|
||||
args[i] = id
|
||||
}
|
||||
return fmt.Sprintf(`
|
||||
SELECT api_key_id, ip_address
|
||||
FROM (
|
||||
SELECT api_key_id, ip_address,
|
||||
ROW_NUMBER() OVER (PARTITION BY api_key_id ORDER BY created_at DESC, id DESC) AS rn
|
||||
FROM usage_logs
|
||||
WHERE api_key_id IN (%s)
|
||||
AND ip_address IS NOT NULL
|
||||
AND ip_address <> ''
|
||||
) ranked
|
||||
WHERE rn = 1`, strings.Join(placeholders, ", ")), args
|
||||
}
|
||||
|
||||
func (r *apiKeyRepository) VerifyOwnership(ctx context.Context, userID int64, apiKeyIDs []int64) ([]int64, error) {
|
||||
if len(apiKeyIDs) == 0 {
|
||||
return []int64{}, nil
|
||||
|
||||
Reference in new issue
Block a user