context.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package request // import "miniflux.app/http/request"
  5. import "net/http"
  6. // ContextKey represents a context key.
  7. type ContextKey int
  8. // List of context keys.
  9. const (
  10. UserIDContextKey ContextKey = iota
  11. UserTimezoneContextKey
  12. IsAdminUserContextKey
  13. IsAuthenticatedContextKey
  14. UserSessionTokenContextKey
  15. UserLanguageContextKey
  16. UserThemeContextKey
  17. SessionIDContextKey
  18. CSRFContextKey
  19. OAuth2StateContextKey
  20. FlashMessageContextKey
  21. FlashErrorMessageContextKey
  22. PocketRequestTokenContextKey
  23. ClientIPContextKey
  24. )
  25. // IsAdminUser checks if the logged user is administrator.
  26. func IsAdminUser(r *http.Request) bool {
  27. return getContextBoolValue(r, IsAdminUserContextKey)
  28. }
  29. // IsAuthenticated returns a boolean if the user is authenticated.
  30. func IsAuthenticated(r *http.Request) bool {
  31. return getContextBoolValue(r, IsAuthenticatedContextKey)
  32. }
  33. // UserID returns the UserID of the logged user.
  34. func UserID(r *http.Request) int64 {
  35. return getContextInt64Value(r, UserIDContextKey)
  36. }
  37. // UserTimezone returns the timezone used by the logged user.
  38. func UserTimezone(r *http.Request) string {
  39. value := getContextStringValue(r, UserTimezoneContextKey)
  40. if value == "" {
  41. value = "UTC"
  42. }
  43. return value
  44. }
  45. // UserLanguage get the locale used by the current logged user.
  46. func UserLanguage(r *http.Request) string {
  47. language := getContextStringValue(r, UserLanguageContextKey)
  48. if language == "" {
  49. language = "en_US"
  50. }
  51. return language
  52. }
  53. // UserTheme get the theme used by the current logged user.
  54. func UserTheme(r *http.Request) string {
  55. theme := getContextStringValue(r, UserThemeContextKey)
  56. if theme == "" {
  57. theme = "system_serif"
  58. }
  59. return theme
  60. }
  61. // CSRF returns the current CSRF token.
  62. func CSRF(r *http.Request) string {
  63. return getContextStringValue(r, CSRFContextKey)
  64. }
  65. // SessionID returns the current session ID.
  66. func SessionID(r *http.Request) string {
  67. return getContextStringValue(r, SessionIDContextKey)
  68. }
  69. // UserSessionToken returns the current user session token.
  70. func UserSessionToken(r *http.Request) string {
  71. return getContextStringValue(r, UserSessionTokenContextKey)
  72. }
  73. // OAuth2State returns the current OAuth2 state.
  74. func OAuth2State(r *http.Request) string {
  75. return getContextStringValue(r, OAuth2StateContextKey)
  76. }
  77. // FlashMessage returns the message message if any.
  78. func FlashMessage(r *http.Request) string {
  79. return getContextStringValue(r, FlashMessageContextKey)
  80. }
  81. // FlashErrorMessage returns the message error message if any.
  82. func FlashErrorMessage(r *http.Request) string {
  83. return getContextStringValue(r, FlashErrorMessageContextKey)
  84. }
  85. // PocketRequestToken returns the Pocket Request Token if any.
  86. func PocketRequestToken(r *http.Request) string {
  87. return getContextStringValue(r, PocketRequestTokenContextKey)
  88. }
  89. // ClientIP returns the client IP address stored in the context.
  90. func ClientIP(r *http.Request) string {
  91. return getContextStringValue(r, ClientIPContextKey)
  92. }
  93. func getContextStringValue(r *http.Request, key ContextKey) string {
  94. if v := r.Context().Value(key); v != nil {
  95. value, valid := v.(string)
  96. if !valid {
  97. return ""
  98. }
  99. return value
  100. }
  101. return ""
  102. }
  103. func getContextBoolValue(r *http.Request, key ContextKey) bool {
  104. if v := r.Context().Value(key); v != nil {
  105. value, valid := v.(bool)
  106. if !valid {
  107. return false
  108. }
  109. return value
  110. }
  111. return false
  112. }
  113. func getContextInt64Value(r *http.Request, key ContextKey) int64 {
  114. if v := r.Context().Value(key); v != nil {
  115. value, valid := v.(int64)
  116. if !valid {
  117. return 0
  118. }
  119. return value
  120. }
  121. return 0
  122. }