2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-01-02 22:45:12 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package googlereader // import "miniflux.app/v2/internal/googlereader"
|
2022-01-02 22:45:12 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
2022-01-02 22:45:12 -05:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/http/request"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/storage"
|
2022-01-02 22:45:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type middleware struct {
|
|
|
|
store *storage.Storage
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMiddleware(s *storage.Storage) *middleware {
|
|
|
|
return &middleware{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
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", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Authorization")
|
|
|
|
if r.Method == http.MethodOptions {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clientIP := request.ClientIP(r)
|
|
|
|
|
|
|
|
var token string
|
|
|
|
if r.Method == http.MethodPost {
|
2023-09-24 19:32:09 -04:00
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
slog.Warn("[GoogleReader] Could not parse request form data",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
2023-09-24 19:32:09 -04:00
|
|
|
|
2022-01-02 22:45:12 -05:00
|
|
|
token = r.Form.Get("T")
|
|
|
|
if token == "" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] Post-Form T field is empty",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
authorization := r.Header.Get("Authorization")
|
|
|
|
|
|
|
|
if authorization == "" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] No token provided",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fields := strings.Fields(authorization)
|
|
|
|
if len(fields) != 2 {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] Authorization header does not have the expected GoogleLogin format auth=xxxxxx",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fields[0] != "GoogleLogin" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] Authorization header does not begin with GoogleLogin",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
auths := strings.Split(fields[1], "=")
|
|
|
|
if len(auths) != 2 {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] Authorization header does not have the expected GoogleLogin format auth=xxxxxx",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if auths[0] != "auth" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] Authorization header does not have the expected GoogleLogin format auth=xxxxxx",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
token = auths[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(token, "/")
|
|
|
|
if len(parts) != 2 {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] Auth token does not have the expected structure username/hash",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.String("token", token),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var integration *model.Integration
|
|
|
|
var user *model.User
|
|
|
|
var err error
|
|
|
|
if integration, err = m.store.GoogleReaderUserGetIntegration(parts[0]); err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] No user found with the given Google Reader username",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
expectedToken := getAuthToken(integration.GoogleReaderUsername, integration.GoogleReaderPassword)
|
|
|
|
if expectedToken != token {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("[GoogleReader] Token does not match",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if user, err = m.store.UserByID(integration.UserID); err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("[GoogleReader] Unable to fetch user from database",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2022-01-02 22:45:12 -05:00
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
if user == nil {
|
|
|
|
slog.Warn("[GoogleReader] No user found with the given Google Reader credentials",
|
|
|
|
slog.Bool("authentication_failed", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
)
|
|
|
|
Unauthorized(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Info("[GoogleReader] User authenticated successfully",
|
|
|
|
slog.Bool("authentication_successful", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.String("username", user.Username),
|
|
|
|
)
|
|
|
|
|
2022-01-02 22:45:12 -05:00
|
|
|
m.store.SetLastLogin(integration.UserID)
|
|
|
|
|
|
|
|
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)
|
|
|
|
ctx = context.WithValue(ctx, request.GoogleReaderToken, token)
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAuthToken(username, password string) string {
|
|
|
|
token := hex.EncodeToString(hmac.New(sha1.New, []byte(username+password)).Sum(nil))
|
|
|
|
token = username + "/" + token
|
|
|
|
return token
|
|
|
|
}
|