List enumerates all auth records stored in PostgreSQL.
(ctx context.Context)
| 270 | |
| 271 | // List enumerates all auth records stored in PostgreSQL. |
| 272 | func (s *PostgresStore) List(ctx context.Context) ([]*cliproxyauth.Auth, error) { |
| 273 | query := fmt.Sprintf("SELECT id, content, created_at, updated_at FROM %s ORDER BY id", s.fullTableName(s.cfg.AuthTable)) |
| 274 | rows, err := s.db.QueryContext(ctx, query) |
| 275 | if err != nil { |
| 276 | return nil, fmt.Errorf("postgres store: list auth: %w", err) |
| 277 | } |
| 278 | defer rows.Close() |
| 279 | |
| 280 | auths := make([]*cliproxyauth.Auth, 0, 32) |
| 281 | for rows.Next() { |
| 282 | var ( |
| 283 | id string |
| 284 | payload string |
| 285 | createdAt time.Time |
| 286 | updatedAt time.Time |
| 287 | ) |
| 288 | if err = rows.Scan(&id, &payload, &createdAt, &updatedAt); err != nil { |
| 289 | return nil, fmt.Errorf("postgres store: scan auth row: %w", err) |
| 290 | } |
| 291 | path, errPath := s.absoluteAuthPath(id) |
| 292 | if errPath != nil { |
| 293 | log.WithError(errPath).Warnf("postgres store: skipping auth %s outside spool", id) |
| 294 | continue |
| 295 | } |
| 296 | metadata := make(map[string]any) |
| 297 | if err = json.Unmarshal([]byte(payload), &metadata); err != nil { |
| 298 | log.WithError(err).Warnf("postgres store: skipping auth %s with invalid json", id) |
| 299 | continue |
| 300 | } |
| 301 | provider := strings.TrimSpace(valueAsString(metadata["type"])) |
| 302 | if provider == "" { |
| 303 | provider = "unknown" |
| 304 | } |
| 305 | attr := map[string]string{ |
| 306 | cliproxyauth.AttributePath: path, |
| 307 | cliproxyauth.AttributeSourceBackend: cliproxyauth.AuthSourcePostgres, |
| 308 | } |
| 309 | if email := strings.TrimSpace(valueAsString(metadata["email"])); email != "" { |
| 310 | attr["email"] = email |
| 311 | } |
| 312 | auth := &cliproxyauth.Auth{ |
| 313 | ID: normalizeAuthID(id), |
| 314 | Provider: provider, |
| 315 | FileName: normalizeAuthID(id), |
| 316 | Label: labelFor(metadata), |
| 317 | Status: cliproxyauth.StatusActive, |
| 318 | Attributes: attr, |
| 319 | Metadata: metadata, |
| 320 | CreatedAt: createdAt, |
| 321 | UpdatedAt: updatedAt, |
| 322 | LastRefreshedAt: time.Time{}, |
| 323 | NextRefreshAfter: time.Time{}, |
| 324 | } |
| 325 | cliproxyauth.ApplyCustomHeadersFromMetadata(auth) |
| 326 | if disabled, ok := metadata["disabled"].(bool); ok && disabled { |
| 327 | auth.Disabled = true |
| 328 | auth.Status = cliproxyauth.StatusDisabled |
| 329 | } |
nothing calls this directly
no test coverage detected