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