2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-09-03 23:26:40 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package request // import "miniflux.app/v2/internal/http/request"
|
2018-09-03 23:26:40 +02:00
|
|
|
|
2023-10-21 00:12:02 +02:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-11-05 18:57:35 +01:00
|
|
|
|
|
|
|
"miniflux.app/v2/internal/model"
|
2023-10-21 00:12:02 +02:00
|
|
|
)
|
2018-09-03 23:26:40 +02:00
|
|
|
|
|
|
|
// ContextKey represents a context key.
|
|
|
|
type ContextKey int
|
|
|
|
|
|
|
|
// List of context keys.
|
|
|
|
const (
|
|
|
|
UserIDContextKey ContextKey = iota
|
|
|
|
UserTimezoneContextKey
|
|
|
|
IsAdminUserContextKey
|
|
|
|
IsAuthenticatedContextKey
|
|
|
|
UserSessionTokenContextKey
|
|
|
|
UserLanguageContextKey
|
|
|
|
UserThemeContextKey
|
|
|
|
SessionIDContextKey
|
|
|
|
CSRFContextKey
|
|
|
|
OAuth2StateContextKey
|
2023-09-03 06:35:10 +02:00
|
|
|
OAuth2CodeVerifierContextKey
|
2018-09-03 23:26:40 +02:00
|
|
|
FlashMessageContextKey
|
|
|
|
FlashErrorMessageContextKey
|
|
|
|
PocketRequestTokenContextKey
|
2023-10-21 00:12:02 +02:00
|
|
|
LastForceRefreshContextKey
|
2018-09-10 00:15:14 +02:00
|
|
|
ClientIPContextKey
|
2022-01-03 04:45:12 +01:00
|
|
|
GoogleReaderToken
|
2023-11-05 18:57:35 +01:00
|
|
|
WebAuthnDataContextKey
|
2018-09-03 23:26:40 +02:00
|
|
|
)
|
|
|
|
|
2023-11-05 18:57:35 +01:00
|
|
|
func WebAuthnSessionData(r *http.Request) *model.WebAuthnSession {
|
|
|
|
if v := r.Context().Value(WebAuthnDataContextKey); v != nil {
|
|
|
|
value, valid := v.(model.WebAuthnSession)
|
|
|
|
if !valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &value
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 04:45:12 +01:00
|
|
|
// GoolgeReaderToken returns the google reader token if it exists.
|
|
|
|
func GoolgeReaderToken(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, GoogleReaderToken)
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
// IsAdminUser checks if the logged user is administrator.
|
|
|
|
func IsAdminUser(r *http.Request) bool {
|
|
|
|
return getContextBoolValue(r, IsAdminUserContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsAuthenticated returns a boolean if the user is authenticated.
|
|
|
|
func IsAuthenticated(r *http.Request) bool {
|
|
|
|
return getContextBoolValue(r, IsAuthenticatedContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserID returns the UserID of the logged user.
|
|
|
|
func UserID(r *http.Request) int64 {
|
|
|
|
return getContextInt64Value(r, UserIDContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserTimezone returns the timezone used by the logged user.
|
|
|
|
func UserTimezone(r *http.Request) string {
|
|
|
|
value := getContextStringValue(r, UserTimezoneContextKey)
|
|
|
|
if value == "" {
|
|
|
|
value = "UTC"
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserLanguage get the locale used by the current logged user.
|
|
|
|
func UserLanguage(r *http.Request) string {
|
|
|
|
language := getContextStringValue(r, UserLanguageContextKey)
|
|
|
|
if language == "" {
|
|
|
|
language = "en_US"
|
|
|
|
}
|
|
|
|
return language
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserTheme get the theme used by the current logged user.
|
|
|
|
func UserTheme(r *http.Request) string {
|
|
|
|
theme := getContextStringValue(r, UserThemeContextKey)
|
|
|
|
if theme == "" {
|
2020-03-20 04:45:05 +01:00
|
|
|
theme = "system_serif"
|
2018-09-03 23:26:40 +02:00
|
|
|
}
|
|
|
|
return theme
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSRF returns the current CSRF token.
|
|
|
|
func CSRF(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, CSRFContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionID returns the current session ID.
|
|
|
|
func SessionID(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, SessionIDContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserSessionToken returns the current user session token.
|
|
|
|
func UserSessionToken(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, UserSessionTokenContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OAuth2State returns the current OAuth2 state.
|
|
|
|
func OAuth2State(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, OAuth2StateContextKey)
|
|
|
|
}
|
|
|
|
|
2023-09-03 06:35:10 +02:00
|
|
|
func OAuth2CodeVerifier(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, OAuth2CodeVerifierContextKey)
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
// FlashMessage returns the message message if any.
|
|
|
|
func FlashMessage(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, FlashMessageContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlashErrorMessage returns the message error message if any.
|
|
|
|
func FlashErrorMessage(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, FlashErrorMessageContextKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PocketRequestToken returns the Pocket Request Token if any.
|
|
|
|
func PocketRequestToken(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, PocketRequestTokenContextKey)
|
|
|
|
}
|
|
|
|
|
2023-10-21 00:12:02 +02:00
|
|
|
// LastForceRefresh returns the last force refresh timestamp.
|
|
|
|
func LastForceRefresh(r *http.Request) int64 {
|
|
|
|
jsonStringValue := getContextStringValue(r, LastForceRefreshContextKey)
|
|
|
|
timestamp, err := strconv.ParseInt(jsonStringValue, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return timestamp
|
|
|
|
}
|
|
|
|
|
2018-09-10 00:15:14 +02:00
|
|
|
// ClientIP returns the client IP address stored in the context.
|
|
|
|
func ClientIP(r *http.Request) string {
|
|
|
|
return getContextStringValue(r, ClientIPContextKey)
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
func getContextStringValue(r *http.Request, key ContextKey) string {
|
|
|
|
if v := r.Context().Value(key); v != nil {
|
2018-09-24 06:02:26 +02:00
|
|
|
value, valid := v.(string)
|
|
|
|
if !valid {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2018-09-03 23:26:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContextBoolValue(r *http.Request, key ContextKey) bool {
|
|
|
|
if v := r.Context().Value(key); v != nil {
|
2018-09-24 06:02:26 +02:00
|
|
|
value, valid := v.(bool)
|
|
|
|
if !valid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2018-09-03 23:26:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContextInt64Value(r *http.Request, key ContextKey) int64 {
|
|
|
|
if v := r.Context().Value(key); v != nil {
|
2018-09-24 06:02:26 +02:00
|
|
|
value, valid := v.(int64)
|
|
|
|
if !valid {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
2018-09-03 23:26:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|