context.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. 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. // UserName returns the username of the logged user.
  57. func UserName(r *http.Request) string {
  58. value := getContextStringValue(r, UserNameContextKey)
  59. if value == "" {
  60. value = "unknown"
  61. }
  62. return value
  63. }
  64. // UserTimezone returns the timezone used by the logged user.
  65. func UserTimezone(r *http.Request) string {
  66. value := getContextStringValue(r, UserTimezoneContextKey)
  67. if value == "" {
  68. value = "UTC"
  69. }
  70. return value
  71. }
  72. // UserLanguage get the locale used by the current logged user.
  73. func UserLanguage(r *http.Request) string {
  74. language := getContextStringValue(r, UserLanguageContextKey)
  75. if language == "" {
  76. language = "en_US"
  77. }
  78. return language
  79. }
  80. // UserTheme get the theme used by the current logged user.
  81. func UserTheme(r *http.Request) string {
  82. theme := getContextStringValue(r, UserThemeContextKey)
  83. if theme == "" {
  84. theme = "system_serif"
  85. }
  86. return theme
  87. }
  88. // CSRF returns the current CSRF token.
  89. func CSRF(r *http.Request) string {
  90. return getContextStringValue(r, CSRFContextKey)
  91. }
  92. // SessionID returns the current session ID.
  93. func SessionID(r *http.Request) string {
  94. return getContextStringValue(r, SessionIDContextKey)
  95. }
  96. // UserSessionToken returns the current user session token.
  97. func UserSessionToken(r *http.Request) string {
  98. return getContextStringValue(r, UserSessionTokenContextKey)
  99. }
  100. // OAuth2State returns the current OAuth2 state.
  101. func OAuth2State(r *http.Request) string {
  102. return getContextStringValue(r, OAuth2StateContextKey)
  103. }
  104. func OAuth2CodeVerifier(r *http.Request) string {
  105. return getContextStringValue(r, OAuth2CodeVerifierContextKey)
  106. }
  107. // FlashMessage returns the message message if any.
  108. func FlashMessage(r *http.Request) string {
  109. return getContextStringValue(r, FlashMessageContextKey)
  110. }
  111. // FlashErrorMessage returns the message error message if any.
  112. func FlashErrorMessage(r *http.Request) string {
  113. return getContextStringValue(r, FlashErrorMessageContextKey)
  114. }
  115. // LastForceRefresh returns the last force refresh timestamp.
  116. func LastForceRefresh(r *http.Request) int64 {
  117. jsonStringValue := getContextStringValue(r, LastForceRefreshContextKey)
  118. timestamp, err := strconv.ParseInt(jsonStringValue, 10, 64)
  119. if err != nil {
  120. return 0
  121. }
  122. return timestamp
  123. }
  124. // ClientIP returns the client IP address stored in the context.
  125. func ClientIP(r *http.Request) string {
  126. return getContextStringValue(r, ClientIPContextKey)
  127. }
  128. func getContextStringValue(r *http.Request, key ContextKey) string {
  129. if v := r.Context().Value(key); v != nil {
  130. if value, valid := v.(string); valid {
  131. return value
  132. }
  133. }
  134. return ""
  135. }
  136. func getContextBoolValue(r *http.Request, key ContextKey) bool {
  137. if v := r.Context().Value(key); v != nil {
  138. if value, valid := v.(bool); valid {
  139. return value
  140. }
  141. }
  142. return false
  143. }
  144. func getContextInt64Value(r *http.Request, key ContextKey) int64 {
  145. if v := r.Context().Value(key); v != nil {
  146. if value, valid := v.(int64); valid {
  147. return value
  148. }
  149. }
  150. return 0
  151. }