context.go 3.3 KB

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