2018-04-28 05:38:46 +02:00
|
|
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
2017-12-04 02:44:27 +01:00
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2017-12-04 02:44:27 +01:00
|
|
|
)
|
|
|
|
|
2018-04-28 05:38:46 +02:00
|
|
|
// FeverAuth handles Fever API authentication.
|
|
|
|
func (m *Middleware) FeverAuth(next http.Handler) http.Handler {
|
2017-12-04 02:44:27 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Debug("[Middleware:Fever]")
|
2017-12-04 02:44:27 +01:00
|
|
|
|
|
|
|
apiKey := r.FormValue("api_key")
|
2018-04-28 05:38:46 +02:00
|
|
|
user, err := m.store.UserByFeverToken(apiKey)
|
2017-12-04 02:44:27 +01:00
|
|
|
if err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Error("[Fever] %v", err)
|
2017-12-04 02:44:27 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"api_version": 3, "auth": 0}`))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Info("[Middleware:Fever] Fever authentication failure")
|
2017-12-04 02:44:27 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(`{"api_version": 3, "auth": 0}`))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Info("[Middleware:Fever] User #%d is authenticated", user.ID)
|
2018-04-28 05:38:46 +02:00
|
|
|
m.store.SetLastLogin(user.ID)
|
2017-12-04 02:44:27 +01:00
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, UserIDContextKey, user.ID)
|
|
|
|
ctx = context.WithValue(ctx, UserTimezoneContextKey, user.Timezone)
|
|
|
|
ctx = context.WithValue(ctx, IsAdminUserContextKey, user.IsAdmin)
|
|
|
|
ctx = context.WithValue(ctx, IsAuthenticatedContextKey, true)
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|