2018-04-28 05:38:46 +02:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
2017-11-20 06:10:04 +01:00
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-11-11 19:22:47 +01:00
|
|
|
package api // import "miniflux.app/api"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2017-11-28 06:30:04 +01:00
|
|
|
|
2018-08-25 22:59:17 +02:00
|
|
|
"miniflux.app/http/request"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/response/json"
|
|
|
|
"miniflux.app/logger"
|
2018-11-11 19:22:47 +01:00
|
|
|
"miniflux.app/storage"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2018-11-11 19:22:47 +01:00
|
|
|
type middleware struct {
|
|
|
|
store *storage.Storage
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMiddleware(s *storage.Storage) *middleware {
|
|
|
|
return &middleware{s}
|
|
|
|
}
|
2020-06-16 19:05:07 +02: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 06:04:34 +02:00
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
2020-06-16 19:05:07 +02:00
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "X-Auth-Token")
|
2020-06-22 06:04:34 +02:00
|
|
|
if r.Method == http.MethodOptions {
|
2022-05-07 12:01:59 +02:00
|
|
|
w.Header().Set("Access-Control-Max-Age", "3600")
|
2020-06-16 19:05:07 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2018-11-11 19:22:47 +01:00
|
|
|
|
2020-03-02 02:38:29 +01:00
|
|
|
func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
|
2017-11-20 06:10:04 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-03-02 02:38:29 +01:00
|
|
|
clientIP := request.ClientIP(r)
|
|
|
|
token := r.Header.Get("X-Auth-Token")
|
|
|
|
|
|
|
|
if token == "" {
|
|
|
|
logger.Debug("[API][TokenAuth] [ClientIP=%s] No API Key provided, go to the next middleware", clientIP)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := m.store.UserByAPIKey(token)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("[API][TokenAuth] %v", err)
|
|
|
|
json.ServerError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
logger.Error("[API][TokenAuth] [ClientIP=%s] No user found with the given API key", clientIP)
|
|
|
|
json.Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("[API][TokenAuth] [ClientIP=%s] User authenticated: %s", clientIP, user.Username)
|
|
|
|
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 06:10:04 +01:00
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
|
|
|
|
2018-09-10 00:15:14 +02:00
|
|
|
clientIP := request.ClientIP(r)
|
2017-11-20 06:10:04 +01:00
|
|
|
username, password, authOK := r.BasicAuth()
|
|
|
|
if !authOK {
|
2020-03-02 02:38:29 +01:00
|
|
|
logger.Debug("[API][BasicAuth] [ClientIP=%s] No authentication headers sent", clientIP)
|
2018-10-08 03:42:43 +02:00
|
|
|
json.Unauthorized(w, r)
|
2020-09-27 22:18:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if username == "" || password == "" {
|
|
|
|
logger.Error("[API][BasicAuth] [ClientIP=%s] Empty username or password", clientIP)
|
|
|
|
json.Unauthorized(w, r)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
if err := m.store.CheckPassword(username, password); err != nil {
|
2020-03-02 02:38:29 +01:00
|
|
|
logger.Error("[API][BasicAuth] [ClientIP=%s] Invalid username or password: %s", clientIP, username)
|
2018-10-08 03:42:43 +02:00
|
|
|
json.Unauthorized(w, r)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
user, err := m.store.UserByUsername(username)
|
2017-12-29 22:53:02 +01:00
|
|
|
if err != nil {
|
2020-03-02 02:38:29 +01:00
|
|
|
logger.Error("[API][BasicAuth] %v", err)
|
2018-10-08 03:42:43 +02:00
|
|
|
json.ServerError(w, r, err)
|
2017-12-29 22:53:02 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2020-03-02 02:38:29 +01:00
|
|
|
logger.Error("[API][BasicAuth] [ClientIP=%s] User not found: %s", clientIP, username)
|
2018-10-08 03:42:43 +02:00
|
|
|
json.Unauthorized(w, r)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-02 02:38:29 +01:00
|
|
|
logger.Info("[API][BasicAuth] [ClientIP=%s] User authenticated: %s", clientIP, username)
|
2018-04-28 05:38:46 +02:00
|
|
|
m.store.SetLastLogin(user.ID)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
ctx := r.Context()
|
2018-09-03 23:26:40 +02: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 06:10:04 +01:00
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|