context.go 4.7 KB

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