| 3460 | } |
| 3461 | |
| 3462 | func (s *Store) ListAPIKeys(ctx context.Context, userID string) ([]APIKey, error) { |
| 3463 | rows, err := s.pool.Query(ctx, |
| 3464 | `SELECT id, user_id, name, key_prefix, COALESCE(scope, 'account'), agent_id, created_at, last_used_at, expires_at |
| 3465 | FROM api_keys WHERE user_id = $1 AND revoked_at IS NULL ORDER BY created_at DESC`, |
| 3466 | userID, |
| 3467 | ) |
| 3468 | if err != nil { |
| 3469 | return nil, err |
| 3470 | } |
| 3471 | defer rows.Close() |
| 3472 | var keys []APIKey |
| 3473 | for rows.Next() { |
| 3474 | var k APIKey |
| 3475 | if err := rows.Scan(&k.ID, &k.UserID, &k.Name, &k.KeyPrefix, &k.Scope, &k.AgentID, &k.CreatedAt, &k.LastUsedAt, &k.ExpiresAt); err != nil { |
| 3476 | return nil, err |
| 3477 | } |
| 3478 | keys = append(keys, k) |
| 3479 | } |
| 3480 | return keys, rows.Err() |
| 3481 | } |
| 3482 | |
| 3483 | // ErrAPIKeyNotFound is returned by DeleteAPIKey when no live key matched the |
| 3484 | // (id, user) — i.e. it doesn't exist, isn't owned by the caller, or was |