web_bot_auth.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "miniflux.app/v2/internal/botauth"
  6. "miniflux.app/v2/internal/crypto"
  7. )
  8. func (s *Storage) CreateWebAuthBothKeys() error {
  9. privateKey, publicKey, err := crypto.GenerateEd25519Keys()
  10. if err != nil {
  11. return err
  12. }
  13. query := `INSERT INTO web_bot_auth (private_key, public_key) VALUES ($1, $2)`
  14. if _, err := s.db.Exec(query, privateKey, publicKey); err != nil {
  15. return err
  16. }
  17. return nil
  18. }
  19. func (s *Storage) WebAuthBothKeys() (keyPairs botauth.KeyPairs, err error) {
  20. query := `SELECT private_key, public_key FROM web_bot_auth ORDER BY created_at DESC`
  21. rows, err := s.db.Query(query)
  22. if err != nil {
  23. return nil, err
  24. }
  25. defer rows.Close()
  26. for rows.Next() {
  27. var privateKey, publicKey []byte
  28. if err := rows.Scan(&privateKey, &publicKey); err != nil {
  29. return nil, err
  30. }
  31. keyPair, err := botauth.NewKeyPair(privateKey, publicKey)
  32. if err != nil {
  33. return nil, err
  34. }
  35. keyPairs = append(keyPairs, keyPair)
  36. }
  37. if err := rows.Err(); err != nil {
  38. return nil, err
  39. }
  40. return keyPairs, nil
  41. }
  42. func (s *Storage) HasWebAuthBothKeys() (bool, error) {
  43. var count int
  44. if err := s.db.QueryRow(`SELECT COUNT(*) FROM web_bot_auth`).Scan(&count); err != nil {
  45. return false, err
  46. }
  47. return count > 0, nil
  48. }