1
0
Fork 0
miniflux/middleware/fever.go

47 lines
1.3 KiB
Go
Raw Normal View History

2018-04-27 23:38:46 -04:00
// Copyright 2018 Frédéric Guillot. All rights reserved.
2017-12-03 20:44:27 -05: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-15 21:55:57 -05:00
"github.com/miniflux/miniflux/logger"
2017-12-03 20:44:27 -05:00
)
2018-04-27 23:38:46 -04:00
// FeverAuth handles Fever API authentication.
func (m *Middleware) FeverAuth(next http.Handler) http.Handler {
2017-12-03 20:44:27 -05:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-12-15 21:55:57 -05:00
logger.Debug("[Middleware:Fever]")
2017-12-03 20:44:27 -05:00
apiKey := r.FormValue("api_key")
2018-04-27 23:38:46 -04:00
user, err := m.store.UserByFeverToken(apiKey)
2017-12-03 20:44:27 -05:00
if err != nil {
2017-12-15 21:55:57 -05:00
logger.Error("[Fever] %v", err)
2017-12-03 20:44:27 -05:00
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"api_version": 3, "auth": 0}`))
return
}
if user == nil {
2017-12-15 21:55:57 -05:00
logger.Info("[Middleware:Fever] Fever authentication failure")
2017-12-03 20:44:27 -05:00
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"api_version": 3, "auth": 0}`))
return
}
2017-12-15 21:55:57 -05:00
logger.Info("[Middleware:Fever] User #%d is authenticated", user.ID)
2018-04-27 23:38:46 -04:00
m.store.SetLastLogin(user.ID)
2017-12-03 20:44:27 -05: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))
})
}