routes.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. )
  9. // isStaticAssetRoute checks if the request path corresponds to a static
  10. // asset route that does not require a web session.
  11. func isStaticAssetRoute(r *http.Request) bool {
  12. path := r.URL.Path
  13. switch path {
  14. case "/favicon.ico", "/robots.txt":
  15. return true
  16. }
  17. return strings.HasPrefix(path, "/stylesheets/") ||
  18. strings.HasPrefix(path, "/js/") ||
  19. strings.HasPrefix(path, "/icon/") ||
  20. strings.HasPrefix(path, "/feed-icon/")
  21. }
  22. // isPublicRoute checks if the request path corresponds to a route that
  23. // does not require authentication. The path is expected to have the base
  24. // path already stripped.
  25. func isPublicRoute(r *http.Request) bool {
  26. if isStaticAssetRoute(r) {
  27. return true
  28. }
  29. path := r.URL.Path
  30. switch path {
  31. case "/", "/login", "/manifest.json",
  32. "/healthcheck", "/offline",
  33. "/webauthn/login/begin", "/webauthn/login/finish":
  34. return true
  35. }
  36. return strings.HasPrefix(path, "/oauth2/") && (strings.HasSuffix(path, "/redirect") || strings.HasSuffix(path, "/callback")) ||
  37. strings.HasPrefix(path, "/share/") ||
  38. strings.HasPrefix(path, "/proxy/")
  39. }
  40. // loginRedirectURL builds the login page URL with the given request URI
  41. // stored in the redirect_url query parameter.
  42. func loginRedirectURL(basePath, requestURI string) string {
  43. loginURL, _ := url.Parse(basePath + "/")
  44. values := loginURL.Query()
  45. values.Set("redirect_url", requestURI)
  46. loginURL.RawQuery = values.Encode()
  47. return loginURL.String()
  48. }