2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package api // import "miniflux.app/v2/internal/api"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
2017-11-20 00:10:04 -05:00
|
|
|
"net/http"
|
2017-11-28 00:30:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/http/request"
|
|
|
|
"miniflux.app/v2/internal/http/response/json"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2018-11-11 13:22:47 -05:00
|
|
|
type middleware struct {
|
|
|
|
store *storage.Storage
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMiddleware(s *storage.Storage) *middleware {
|
|
|
|
return &middleware{s}
|
|
|
|
}
|
2020-06-16 13:05:07 -04:00
|
|
|
func (m *middleware) handleCORS(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
2020-06-22 00:04:34 -04:00
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
2022-11-12 23:48:58 -05:00
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "X-Auth-Token, Authorization, Content-Type, Accept")
|
2020-06-22 00:04:34 -04:00
|
|
|
if r.Method == http.MethodOptions {
|
2022-05-07 06:01:59 -04:00
|
|
|
w.Header().Set("Access-Control-Max-Age", "3600")
|
2020-06-16 13:05:07 -04:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2018-11-11 13:22:47 -05:00
|
|
|
|
2020-03-01 20:38:29 -05:00
|
|
|
func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
|
2017-11-20 00:10:04 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-03-01 20:38:29 -05:00
|
|
|
clientIP := request.ClientIP(r)
|
|
|
|
token := r.Header.Get("X-Auth-Token")
|
|
|
|
|
|
|
|
if token == "" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("[API] Skipped API token authentication because no API Key has been provided",
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2020-03-01 20:38:29 -05:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := m.store.UserByAPIKey(token)
|
|
|
|
if err != nil {
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[API] No user found with the provided API key",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2020-03-01 20:38:29 -05:00
|
|
|
json.Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Info("[API] User authenticated successfully with the API Token Authentication",
|
|
|
|
slog.Bool("authentication_successful", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.String("username", user.Username),
|
|
|
|
)
|
|
|
|
|
2020-03-01 20:38:29 -05:00
|
|
|
m.store.SetLastLogin(user.ID)
|
|
|
|
m.store.SetAPIKeyUsedTimestamp(user.ID, token)
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID)
|
|
|
|
ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone)
|
|
|
|
ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin)
|
|
|
|
ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) basicAuth(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if request.IsAuthenticated(r) {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
|
|
|
|
2018-09-09 18:15:14 -04:00
|
|
|
clientIP := request.ClientIP(r)
|
2017-11-20 00:10:04 -05:00
|
|
|
username, password, authOK := r.BasicAuth()
|
|
|
|
if !authOK {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[API] No Basic HTTP Authentication header sent with the request",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2018-10-07 21:42:43 -04:00
|
|
|
json.Unauthorized(w, r)
|
2020-09-27 16:18:51 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if username == "" || password == "" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[API] Empty username or password provided during Basic HTTP Authentication",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2020-09-27 16:18:51 -04:00
|
|
|
json.Unauthorized(w, r)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:38:46 -04:00
|
|
|
if err := m.store.CheckPassword(username, password); err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[API] Invalid username or password provided during Basic HTTP Authentication",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.String("username", username),
|
|
|
|
)
|
2018-10-07 21:42:43 -04:00
|
|
|
json.Unauthorized(w, r)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:38:46 -04:00
|
|
|
user, err := m.store.UserByUsername(username)
|
2017-12-29 16:53:02 -05:00
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
json.ServerError(w, r, err)
|
2017-12-29 16:53:02 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[API] User not found while using Basic HTTP Authentication",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.String("username", username),
|
|
|
|
)
|
2018-10-07 21:42:43 -04:00
|
|
|
json.Unauthorized(w, r)
|
2017-11-20 00:10:04 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Info("[API] User authenticated successfully with the Basic HTTP Authentication",
|
|
|
|
slog.Bool("authentication_successful", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.String("username", username),
|
|
|
|
)
|
|
|
|
|
2018-04-27 23:38:46 -04:00
|
|
|
m.store.SetLastLogin(user.ID)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
ctx := r.Context()
|
2018-09-03 17:26:40 -04:00
|
|
|
ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID)
|
|
|
|
ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone)
|
|
|
|
ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin)
|
|
|
|
ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|