fever.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2018 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 middleware
  5. import (
  6. "context"
  7. "net/http"
  8. "github.com/miniflux/miniflux/logger"
  9. )
  10. // FeverAuth handles Fever API authentication.
  11. func (m *Middleware) FeverAuth(next http.Handler) http.Handler {
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. logger.Debug("[Middleware:Fever]")
  14. apiKey := r.FormValue("api_key")
  15. user, err := m.store.UserByFeverToken(apiKey)
  16. if err != nil {
  17. logger.Error("[Fever] %v", err)
  18. w.Header().Set("Content-Type", "application/json")
  19. w.Write([]byte(`{"api_version": 3, "auth": 0}`))
  20. return
  21. }
  22. if user == nil {
  23. logger.Info("[Middleware:Fever] Fever authentication failure")
  24. w.Header().Set("Content-Type", "application/json")
  25. w.Write([]byte(`{"api_version": 3, "auth": 0}`))
  26. return
  27. }
  28. logger.Info("[Middleware:Fever] User #%d is authenticated", user.ID)
  29. m.store.SetLastLogin(user.ID)
  30. ctx := r.Context()
  31. ctx = context.WithValue(ctx, UserIDContextKey, user.ID)
  32. ctx = context.WithValue(ctx, UserTimezoneContextKey, user.Timezone)
  33. ctx = context.WithValue(ctx, IsAdminUserContextKey, user.IsAdmin)
  34. ctx = context.WithValue(ctx, IsAuthenticatedContextKey, true)
  35. next.ServeHTTP(w, r.WithContext(ctx))
  36. })
  37. }