auth.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package middleware
  2. import (
  3. "crypto/subtle"
  4. "net/http"
  5. "strings"
  6. "github.com/owncast/owncast/core/data"
  7. "github.com/owncast/owncast/core/user"
  8. "github.com/owncast/owncast/utils"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. // ExternalAccessTokenHandlerFunc is a function that is called after validing access.
  12. type ExternalAccessTokenHandlerFunc func(user.ExternalAPIUser, http.ResponseWriter, *http.Request)
  13. // UserAccessTokenHandlerFunc is a function that is called after validing user access.
  14. type UserAccessTokenHandlerFunc func(user.User, http.ResponseWriter, *http.Request)
  15. // RequireAdminAuth wraps a handler requiring HTTP basic auth for it using the given
  16. // the stream key as the password and and a hardcoded "admin" for username.
  17. func RequireAdminAuth(handler http.HandlerFunc) http.HandlerFunc {
  18. return func(w http.ResponseWriter, r *http.Request) {
  19. username := "admin"
  20. password := data.GetAdminPassword()
  21. realm := "Owncast Authenticated Request"
  22. // The following line is kind of a work around.
  23. // If you want HTTP Basic Auth + Cors it requires _explicit_ origins to be provided in the
  24. // Access-Control-Allow-Origin header. So we just pull out the origin header and specify it.
  25. // If we want to lock down admin APIs to not be CORS accessible for anywhere, this is where we would do that.
  26. w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
  27. w.Header().Set("Access-Control-Allow-Credentials", "true")
  28. w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
  29. // For request needing CORS, send a 204.
  30. if r.Method == "OPTIONS" {
  31. w.WriteHeader(http.StatusNoContent)
  32. return
  33. }
  34. user, pass, ok := r.BasicAuth()
  35. // Failed
  36. if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
  37. w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
  38. http.Error(w, "Unauthorized", http.StatusUnauthorized)
  39. log.Debugln("Failed admin authentication")
  40. return
  41. }
  42. handler(w, r)
  43. }
  44. }
  45. func accessDenied(w http.ResponseWriter) {
  46. w.WriteHeader(http.StatusUnauthorized) //nolint
  47. w.Write([]byte("unauthorized")) //nolint
  48. }
  49. // RequireExternalAPIAccessToken will validate a 3rd party access token.
  50. func RequireExternalAPIAccessToken(scope string, handler ExternalAccessTokenHandlerFunc) http.HandlerFunc {
  51. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  52. // We should accept 3rd party preflight OPTIONS requests.
  53. if r.Method == "OPTIONS" {
  54. // All OPTIONS requests should have a wildcard CORS header.
  55. w.Header().Set("Access-Control-Allow-Origin", "*")
  56. w.WriteHeader(http.StatusNoContent)
  57. return
  58. }
  59. authHeader := r.Header.Get("Authorization")
  60. token := ""
  61. if strings.HasPrefix(strings.ToLower(authHeader), "bearer ") {
  62. token = authHeader[len("bearer "):]
  63. }
  64. if token == "" {
  65. log.Warnln("invalid access token")
  66. accessDenied(w)
  67. return
  68. }
  69. integration, err := user.GetExternalAPIUserForAccessTokenAndScope(token, scope)
  70. if integration == nil || err != nil {
  71. accessDenied(w)
  72. return
  73. }
  74. // All auth'ed 3rd party requests should have a wildcard CORS header.
  75. w.Header().Set("Access-Control-Allow-Origin", "*")
  76. handler(*integration, w, r)
  77. if err := user.SetExternalAPIUserAccessTokenAsUsed(token); err != nil {
  78. log.Debugln("token not found when updating last_used timestamp")
  79. }
  80. })
  81. }
  82. // RequireUserAccessToken will validate a provided user's access token and make sure the associated user is enabled.
  83. // Not to be used for validating 3rd party access.
  84. func RequireUserAccessToken(handler UserAccessTokenHandlerFunc) http.HandlerFunc {
  85. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  86. accessToken := r.URL.Query().Get("accessToken")
  87. if accessToken == "" {
  88. accessDenied(w)
  89. return
  90. }
  91. ipAddress := utils.GetIPAddressFromRequest(r)
  92. // Check if this client's IP address is banned.
  93. if blocked, err := data.IsIPAddressBanned(ipAddress); blocked {
  94. log.Debugln("Client ip address has been blocked. Rejecting.")
  95. accessDenied(w)
  96. return
  97. } else if err != nil {
  98. log.Errorln("error determining if IP address is blocked: ", err)
  99. }
  100. // A user is required to use the websocket
  101. user := user.GetUserByToken(accessToken)
  102. if user == nil || !user.IsEnabled() {
  103. accessDenied(w)
  104. return
  105. }
  106. handler(*user, w, r)
  107. })
  108. }
  109. // RequireUserModerationScopeAccesstoken will validate a provided user's access token and make sure the associated user is enabled
  110. // and has "MODERATOR" scope assigned to the user.
  111. func RequireUserModerationScopeAccesstoken(handler http.HandlerFunc) http.HandlerFunc {
  112. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  113. accessToken := r.URL.Query().Get("accessToken")
  114. if accessToken == "" {
  115. accessDenied(w)
  116. return
  117. }
  118. // A user is required to use the websocket
  119. user := user.GetUserByToken(accessToken)
  120. if user == nil || !user.IsEnabled() || !user.IsModerator() {
  121. accessDenied(w)
  122. return
  123. }
  124. handler(w, r)
  125. })
  126. }