context.go 4.2 KB

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