fever.go 1.3 KB

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