context.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "miniflux.app/v2/internal/model"
  7. )
  8. // ContextKey represents a context key.
  9. type ContextKey int
  10. // List of context keys.
  11. const (
  12. UserIDContextKey ContextKey = iota
  13. UserNameContextKey
  14. UserTimezoneContextKey
  15. IsAdminUserContextKey
  16. IsAuthenticatedContextKey
  17. WebSessionContextKey
  18. ClientIPContextKey
  19. GoogleReaderTokenKey
  20. )
  21. // WebSession returns the current web session from the request context, if present.
  22. func WebSession(r *http.Request) *model.WebSession {
  23. if v := r.Context().Value(WebSessionContextKey); v != nil {
  24. if value, valid := v.(*model.WebSession); valid {
  25. return value
  26. }
  27. }
  28. return nil
  29. }
  30. // GoogleReaderToken returns the Google Reader token from the request context, if present.
  31. func GoogleReaderToken(r *http.Request) string {
  32. return getContextStringValue(r, GoogleReaderTokenKey)
  33. }
  34. // IsAdminUser reports whether the logged-in user is an administrator.
  35. func IsAdminUser(r *http.Request) bool {
  36. return getContextBoolValue(r, IsAdminUserContextKey)
  37. }
  38. // IsAuthenticated reports whether the user is authenticated.
  39. func IsAuthenticated(r *http.Request) bool {
  40. if getContextBoolValue(r, IsAuthenticatedContextKey) {
  41. return true
  42. }
  43. if session := WebSession(r); session != nil {
  44. return session.IsAuthenticated()
  45. }
  46. return false
  47. }
  48. // UserID returns the logged-in user's ID from the request context.
  49. func UserID(r *http.Request) int64 {
  50. if userID := getContextInt64Value(r, UserIDContextKey); userID != 0 {
  51. return userID
  52. }
  53. if session := WebSession(r); session != nil {
  54. if id, ok := session.UserID(); ok {
  55. return id
  56. }
  57. }
  58. return 0
  59. }
  60. // UserName returns the logged-in user's username, or "unknown" when unset.
  61. func UserName(r *http.Request) string {
  62. value := getContextStringValue(r, UserNameContextKey)
  63. if value == "" {
  64. value = "unknown"
  65. }
  66. return value
  67. }
  68. // UserTimezone returns the user's timezone, defaulting to "UTC" when unset.
  69. func UserTimezone(r *http.Request) string {
  70. value := getContextStringValue(r, UserTimezoneContextKey)
  71. if value == "" {
  72. value = "UTC"
  73. }
  74. return value
  75. }
  76. // ClientIP returns the client IP address stored in the request context.
  77. func ClientIP(r *http.Request) string {
  78. return getContextStringValue(r, ClientIPContextKey)
  79. }
  80. func getContextStringValue(r *http.Request, key ContextKey) string {
  81. if v := r.Context().Value(key); v != nil {
  82. if value, valid := v.(string); valid {
  83. return value
  84. }
  85. }
  86. return ""
  87. }
  88. func getContextBoolValue(r *http.Request, key ContextKey) bool {
  89. if v := r.Context().Value(key); v != nil {
  90. if value, valid := v.(bool); valid {
  91. return value
  92. }
  93. }
  94. return false
  95. }
  96. func getContextInt64Value(r *http.Request, key ContextKey) int64 {
  97. if v := r.Context().Value(key); v != nil {
  98. if value, valid := v.(int64); valid {
  99. return value
  100. }
  101. }
  102. return 0
  103. }