context.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "time"
  8. "miniflux.app/v2/internal/model"
  9. )
  10. // ContextKey represents a context key.
  11. type ContextKey int
  12. // List of context keys.
  13. const (
  14. UserIDContextKey ContextKey = iota
  15. UserNameContextKey
  16. UserTimezoneContextKey
  17. IsAdminUserContextKey
  18. IsAuthenticatedContextKey
  19. UserSessionTokenContextKey
  20. UserLanguageContextKey
  21. UserThemeContextKey
  22. SessionIDContextKey
  23. CSRFContextKey
  24. OAuth2StateContextKey
  25. OAuth2CodeVerifierContextKey
  26. FlashMessageContextKey
  27. FlashErrorMessageContextKey
  28. LastForceRefreshContextKey
  29. ClientIPContextKey
  30. GoogleReaderTokenKey
  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. // GoogleReaderToken returns the google reader token if it exists.
  42. func GoogleReaderToken(r *http.Request) string {
  43. return getContextStringValue(r, GoogleReaderTokenKey)
  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. // LastForceRefresh returns the last force refresh timestamp.
  117. func LastForceRefresh(r *http.Request) time.Time {
  118. jsonStringValue := getContextStringValue(r, LastForceRefreshContextKey)
  119. timestamp, err := strconv.ParseInt(jsonStringValue, 10, 64)
  120. if err != nil {
  121. return time.Time{}
  122. }
  123. return time.Unix(timestamp, 0)
  124. }
  125. // ClientIP returns the client IP address stored in the context.
  126. func ClientIP(r *http.Request) string {
  127. return getContextStringValue(r, ClientIPContextKey)
  128. }
  129. func getContextStringValue(r *http.Request, key ContextKey) string {
  130. if v := r.Context().Value(key); v != nil {
  131. if value, valid := v.(string); valid {
  132. return value
  133. }
  134. }
  135. return ""
  136. }
  137. func getContextBoolValue(r *http.Request, key ContextKey) bool {
  138. if v := r.Context().Value(key); v != nil {
  139. if value, valid := v.(bool); valid {
  140. return value
  141. }
  142. }
  143. return false
  144. }
  145. func getContextInt64Value(r *http.Request, key ContextKey) int64 {
  146. if v := r.Context().Value(key); v != nil {
  147. if value, valid := v.(int64); valid {
  148. return value
  149. }
  150. }
  151. return 0
  152. }