1// Copyright 2018 Frédéric Guillot. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5package request // import "miniflux.app/http/request"
6
7import "net/http"
8
9// ContextKey represents a context key.
10type ContextKey int
11
12// List of context keys.
13const (
14	UserIDContextKey ContextKey = iota
15	UserTimezoneContextKey
16	IsAdminUserContextKey
17	IsAuthenticatedContextKey
18	UserSessionTokenContextKey
19	UserLanguageContextKey
20	UserThemeContextKey
21	SessionIDContextKey
22	CSRFContextKey
23	OAuth2StateContextKey
24	FlashMessageContextKey
25	FlashErrorMessageContextKey
26	PocketRequestTokenContextKey
27	ClientIPContextKey
28	GoogleReaderToken
29)
30
31// GoolgeReaderToken returns the google reader token if it exists.
32func GoolgeReaderToken(r *http.Request) string {
33	return getContextStringValue(r, GoogleReaderToken)
34}
35
36// IsAdminUser checks if the logged user is administrator.
37func IsAdminUser(r *http.Request) bool {
38	return getContextBoolValue(r, IsAdminUserContextKey)
39}
40
41// IsAuthenticated returns a boolean if the user is authenticated.
42func IsAuthenticated(r *http.Request) bool {
43	return getContextBoolValue(r, IsAuthenticatedContextKey)
44}
45
46// UserID returns the UserID of the logged user.
47func UserID(r *http.Request) int64 {
48	return getContextInt64Value(r, UserIDContextKey)
49}
50
51// UserTimezone returns the timezone used by the logged user.
52func UserTimezone(r *http.Request) string {
53	value := getContextStringValue(r, UserTimezoneContextKey)
54	if value == "" {
55		value = "UTC"
56	}
57	return value
58}
59
60// UserLanguage get the locale used by the current logged user.
61func UserLanguage(r *http.Request) string {
62	language := getContextStringValue(r, UserLanguageContextKey)
63	if language == "" {
64		language = "en_US"
65	}
66	return language
67}
68
69// UserTheme get the theme used by the current logged user.
70func UserTheme(r *http.Request) string {
71	theme := getContextStringValue(r, UserThemeContextKey)
72	if theme == "" {
73		theme = "system_serif"
74	}
75	return theme
76}
77
78// CSRF returns the current CSRF token.
79func CSRF(r *http.Request) string {
80	return getContextStringValue(r, CSRFContextKey)
81}
82
83// SessionID returns the current session ID.
84func SessionID(r *http.Request) string {
85	return getContextStringValue(r, SessionIDContextKey)
86}
87
88// UserSessionToken returns the current user session token.
89func UserSessionToken(r *http.Request) string {
90	return getContextStringValue(r, UserSessionTokenContextKey)
91}
92
93// OAuth2State returns the current OAuth2 state.
94func OAuth2State(r *http.Request) string {
95	return getContextStringValue(r, OAuth2StateContextKey)
96}
97
98// FlashMessage returns the message message if any.
99func FlashMessage(r *http.Request) string {
100	return getContextStringValue(r, FlashMessageContextKey)
101}
102
103// FlashErrorMessage returns the message error message if any.
104func FlashErrorMessage(r *http.Request) string {
105	return getContextStringValue(r, FlashErrorMessageContextKey)
106}
107
108// PocketRequestToken returns the Pocket Request Token if any.
109func PocketRequestToken(r *http.Request) string {
110	return getContextStringValue(r, PocketRequestTokenContextKey)
111}
112
113// ClientIP returns the client IP address stored in the context.
114func ClientIP(r *http.Request) string {
115	return getContextStringValue(r, ClientIPContextKey)
116}
117
118func getContextStringValue(r *http.Request, key ContextKey) string {
119	if v := r.Context().Value(key); v != nil {
120		value, valid := v.(string)
121		if !valid {
122			return ""
123		}
124
125		return value
126	}
127
128	return ""
129}
130
131func getContextBoolValue(r *http.Request, key ContextKey) bool {
132	if v := r.Context().Value(key); v != nil {
133		value, valid := v.(bool)
134		if !valid {
135			return false
136		}
137
138		return value
139	}
140
141	return false
142}
143
144func getContextInt64Value(r *http.Request, key ContextKey) int64 {
145	if v := r.Context().Value(key); v != nil {
146		value, valid := v.(int64)
147		if !valid {
148			return 0
149		}
150
151		return value
152	}
153
154	return 0
155}
156