4
0

api_key.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package storage // import "miniflux.app/v2/internal/storage"
  4. import (
  5. "fmt"
  6. "miniflux.app/v2/internal/model"
  7. )
  8. // APIKeyExists checks if an API Key with the same description exists.
  9. func (s *Storage) APIKeyExists(userID int64, description string) bool {
  10. var result bool
  11. query := `SELECT true FROM api_keys WHERE user_id=$1 AND lower(description)=lower($2) LIMIT 1`
  12. s.db.QueryRow(query, userID, description).Scan(&result)
  13. return result
  14. }
  15. // SetAPIKeyUsedTimestamp updates the last used date of an API Key.
  16. func (s *Storage) SetAPIKeyUsedTimestamp(userID int64, token string) error {
  17. query := `UPDATE api_keys SET last_used_at=now() WHERE user_id=$1 and token=$2`
  18. _, err := s.db.Exec(query, userID, token)
  19. if err != nil {
  20. return fmt.Errorf(`store: unable to update last used date for API key: %v`, err)
  21. }
  22. return nil
  23. }
  24. // APIKeys returns all API Keys that belongs to the given user.
  25. func (s *Storage) APIKeys(userID int64) (model.APIKeys, error) {
  26. query := `
  27. SELECT
  28. id, user_id, token, description, last_used_at, created_at
  29. FROM
  30. api_keys
  31. WHERE
  32. user_id=$1
  33. ORDER BY description ASC
  34. `
  35. rows, err := s.db.Query(query, userID)
  36. if err != nil {
  37. return nil, fmt.Errorf(`store: unable to fetch API Keys: %v`, err)
  38. }
  39. defer rows.Close()
  40. apiKeys := make(model.APIKeys, 0)
  41. for rows.Next() {
  42. var apiKey model.APIKey
  43. if err := rows.Scan(
  44. &apiKey.ID,
  45. &apiKey.UserID,
  46. &apiKey.Token,
  47. &apiKey.Description,
  48. &apiKey.LastUsedAt,
  49. &apiKey.CreatedAt,
  50. ); err != nil {
  51. return nil, fmt.Errorf(`store: unable to fetch API Key row: %v`, err)
  52. }
  53. apiKeys = append(apiKeys, &apiKey)
  54. }
  55. return apiKeys, nil
  56. }
  57. // CreateAPIKey inserts a new API key.
  58. func (s *Storage) CreateAPIKey(apiKey *model.APIKey) error {
  59. query := `
  60. INSERT INTO api_keys
  61. (user_id, token, description)
  62. VALUES
  63. ($1, $2, $3)
  64. RETURNING
  65. id, created_at
  66. `
  67. err := s.db.QueryRow(
  68. query,
  69. apiKey.UserID,
  70. apiKey.Token,
  71. apiKey.Description,
  72. ).Scan(
  73. &apiKey.ID,
  74. &apiKey.CreatedAt,
  75. )
  76. if err != nil {
  77. return fmt.Errorf(`store: unable to create category: %v`, err)
  78. }
  79. return nil
  80. }
  81. // RemoveAPIKey deletes an API Key.
  82. func (s *Storage) RemoveAPIKey(userID, keyID int64) error {
  83. query := `DELETE FROM api_keys WHERE id = $1 AND user_id = $2`
  84. _, err := s.db.Exec(query, keyID, userID)
  85. if err != nil {
  86. return fmt.Errorf(`store: unable to remove this API Key: %v`, err)
  87. }
  88. return nil
  89. }