4
0

api_key.go 2.5 KB

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