context.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. GoogleReaderToken
  25. )
  26. // GoolgeReaderToken returns the google reader token if it exists.
  27. func GoolgeReaderToken(r *http.Request) string {
  28. return getContextStringValue(r, GoogleReaderToken)
  29. }
  30. // IsAdminUser checks if the logged user is administrator.
  31. func IsAdminUser(r *http.Request) bool {
  32. return getContextBoolValue(r, IsAdminUserContextKey)
  33. }
  34. // IsAuthenticated returns a boolean if the user is authenticated.
  35. func IsAuthenticated(r *http.Request) bool {
  36. return getContextBoolValue(r, IsAuthenticatedContextKey)
  37. }
  38. // UserID returns the UserID of the logged user.
  39. func UserID(r *http.Request) int64 {
  40. return getContextInt64Value(r, UserIDContextKey)
  41. }
  42. // UserTimezone returns the timezone used by the logged user.
  43. func UserTimezone(r *http.Request) string {
  44. value := getContextStringValue(r, UserTimezoneContextKey)
  45. if value == "" {
  46. value = "UTC"
  47. }
  48. return value
  49. }
  50. // UserLanguage get the locale used by the current logged user.
  51. func UserLanguage(r *http.Request) string {
  52. language := getContextStringValue(r, UserLanguageContextKey)
  53. if language == "" {
  54. language = "en_US"
  55. }
  56. return language
  57. }
  58. // UserTheme get the theme used by the current logged user.
  59. func UserTheme(r *http.Request) string {
  60. theme := getContextStringValue(r, UserThemeContextKey)
  61. if theme == "" {
  62. theme = "system_serif"
  63. }
  64. return theme
  65. }
  66. // CSRF returns the current CSRF token.
  67. func CSRF(r *http.Request) string {
  68. return getContextStringValue(r, CSRFContextKey)
  69. }
  70. // SessionID returns the current session ID.
  71. func SessionID(r *http.Request) string {
  72. return getContextStringValue(r, SessionIDContextKey)
  73. }
  74. // UserSessionToken returns the current user session token.
  75. func UserSessionToken(r *http.Request) string {
  76. return getContextStringValue(r, UserSessionTokenContextKey)
  77. }
  78. // OAuth2State returns the current OAuth2 state.
  79. func OAuth2State(r *http.Request) string {
  80. return getContextStringValue(r, OAuth2StateContextKey)
  81. }
  82. // FlashMessage returns the message message if any.
  83. func FlashMessage(r *http.Request) string {
  84. return getContextStringValue(r, FlashMessageContextKey)
  85. }
  86. // FlashErrorMessage returns the message error message if any.
  87. func FlashErrorMessage(r *http.Request) string {
  88. return getContextStringValue(r, FlashErrorMessageContextKey)
  89. }
  90. // PocketRequestToken returns the Pocket Request Token if any.
  91. func PocketRequestToken(r *http.Request) string {
  92. return getContextStringValue(r, PocketRequestTokenContextKey)
  93. }
  94. // ClientIP returns the client IP address stored in the context.
  95. func ClientIP(r *http.Request) string {
  96. return getContextStringValue(r, ClientIPContextKey)
  97. }
  98. func getContextStringValue(r *http.Request, key ContextKey) string {
  99. if v := r.Context().Value(key); v != nil {
  100. value, valid := v.(string)
  101. if !valid {
  102. return ""
  103. }
  104. return value
  105. }
  106. return ""
  107. }
  108. func getContextBoolValue(r *http.Request, key ContextKey) bool {
  109. if v := r.Context().Value(key); v != nil {
  110. value, valid := v.(bool)
  111. if !valid {
  112. return false
  113. }
  114. return value
  115. }
  116. return false
  117. }
  118. func getContextInt64Value(r *http.Request, key ContextKey) int64 {
  119. if v := r.Context().Value(key); v != nil {
  120. value, valid := v.(int64)
  121. if !valid {
  122. return 0
  123. }
  124. return value
  125. }
  126. return 0
  127. }