routes.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "miniflux.app/v2/internal/config"
  9. )
  10. // isStaticAssetRoute checks if the request path corresponds to a static
  11. // asset route that does not require a web session.
  12. func isStaticAssetRoute(r *http.Request) bool {
  13. path := r.URL.Path
  14. switch path {
  15. case "/favicon.ico", "/robots.txt":
  16. return true
  17. }
  18. return strings.HasPrefix(path, "/stylesheets/") ||
  19. strings.HasPrefix(path, "/js/") ||
  20. strings.HasPrefix(path, "/icon/") ||
  21. strings.HasPrefix(path, "/feed-icon/") ||
  22. strings.HasPrefix(path, "/proxy/")
  23. }
  24. // isPublicRoute checks if the request path corresponds to a route that
  25. // does not require authentication. The path is expected to have the base
  26. // path already stripped.
  27. func isPublicRoute(r *http.Request) bool {
  28. if isStaticAssetRoute(r) {
  29. return true
  30. }
  31. path := r.URL.Path
  32. switch path {
  33. case "/", "/login", "/manifest.json", "/offline",
  34. "/webauthn/login/begin", "/webauthn/login/finish":
  35. return true
  36. }
  37. return strings.HasPrefix(path, "/oauth2/") && (strings.HasSuffix(path, "/redirect") || strings.HasSuffix(path, "/callback")) ||
  38. strings.HasPrefix(path, "/share/")
  39. }
  40. // isSessionStartRoute checks if the request starts state that a later request
  41. // must find (login CSRF token, WebAuthn challenge, OAuth2 state), so a visitor
  42. // without a session needs one persisted.
  43. func isSessionStartRoute(r *http.Request) bool {
  44. if r.Method != http.MethodGet && r.Method != http.MethodHead {
  45. return false
  46. }
  47. path := r.URL.Path
  48. switch {
  49. case path == "/":
  50. return true
  51. case path == "/webauthn/login/begin":
  52. return config.Opts.WebAuthn()
  53. case strings.HasPrefix(path, "/oauth2/") && strings.HasSuffix(path, "/redirect"):
  54. return config.Opts.OAuth2Provider() != ""
  55. }
  56. return false
  57. }
  58. // loginRedirectURL builds the login page URL with the given request URI
  59. // stored in the redirect_url query parameter.
  60. func loginRedirectURL(basePath, requestURI string) string {
  61. loginURL, _ := url.Parse(basePath + "/")
  62. values := loginURL.Query()
  63. values.Set("redirect_url", requestURI)
  64. loginURL.RawQuery = values.Encode()
  65. return loginURL.String()
  66. }