context.go 3.8 KB

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