middleware.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 api // import "miniflux.app/api"
  5. import (
  6. "context"
  7. "net/http"
  8. "miniflux.app/http/request"
  9. "miniflux.app/http/response/json"
  10. "miniflux.app/logger"
  11. "miniflux.app/storage"
  12. )
  13. type middleware struct {
  14. store *storage.Storage
  15. }
  16. func newMiddleware(s *storage.Storage) *middleware {
  17. return &middleware{s}
  18. }
  19. func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
  20. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  21. clientIP := request.ClientIP(r)
  22. token := r.Header.Get("X-Auth-Token")
  23. if token == "" {
  24. logger.Debug("[API][TokenAuth] [ClientIP=%s] No API Key provided, go to the next middleware", clientIP)
  25. next.ServeHTTP(w, r)
  26. return
  27. }
  28. user, err := m.store.UserByAPIKey(token)
  29. if err != nil {
  30. logger.Error("[API][TokenAuth] %v", err)
  31. json.ServerError(w, r, err)
  32. return
  33. }
  34. if user == nil {
  35. logger.Error("[API][TokenAuth] [ClientIP=%s] No user found with the given API key", clientIP)
  36. json.Unauthorized(w, r)
  37. return
  38. }
  39. logger.Info("[API][TokenAuth] [ClientIP=%s] User authenticated: %s", clientIP, user.Username)
  40. m.store.SetLastLogin(user.ID)
  41. m.store.SetAPIKeyUsedTimestamp(user.ID, token)
  42. ctx := r.Context()
  43. ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID)
  44. ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone)
  45. ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin)
  46. ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)
  47. next.ServeHTTP(w, r.WithContext(ctx))
  48. })
  49. }
  50. func (m *middleware) basicAuth(next http.Handler) http.Handler {
  51. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  52. if request.IsAuthenticated(r) {
  53. next.ServeHTTP(w, r)
  54. return
  55. }
  56. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  57. clientIP := request.ClientIP(r)
  58. username, password, authOK := r.BasicAuth()
  59. if !authOK {
  60. logger.Debug("[API][BasicAuth] [ClientIP=%s] No authentication headers sent", clientIP)
  61. json.Unauthorized(w, r)
  62. return
  63. }
  64. if err := m.store.CheckPassword(username, password); err != nil {
  65. logger.Error("[API][BasicAuth] [ClientIP=%s] Invalid username or password: %s", clientIP, username)
  66. json.Unauthorized(w, r)
  67. return
  68. }
  69. user, err := m.store.UserByUsername(username)
  70. if err != nil {
  71. logger.Error("[API][BasicAuth] %v", err)
  72. json.ServerError(w, r, err)
  73. return
  74. }
  75. if user == nil {
  76. logger.Error("[API][BasicAuth] [ClientIP=%s] User not found: %s", clientIP, username)
  77. json.Unauthorized(w, r)
  78. return
  79. }
  80. logger.Info("[API][BasicAuth] [ClientIP=%s] User authenticated: %s", clientIP, username)
  81. m.store.SetLastLogin(user.ID)
  82. ctx := r.Context()
  83. ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID)
  84. ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone)
  85. ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin)
  86. ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)
  87. next.ServeHTTP(w, r.WithContext(ctx))
  88. })
  89. }