context.go 3.9 KB

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