oauth2_callback.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "errors"
  6. "net/http"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/http/cookie"
  9. "miniflux.app/v2/internal/http/request"
  10. "miniflux.app/v2/internal/http/response/html"
  11. "miniflux.app/v2/internal/http/route"
  12. "miniflux.app/v2/internal/locale"
  13. "miniflux.app/v2/internal/logger"
  14. "miniflux.app/v2/internal/model"
  15. "miniflux.app/v2/internal/ui/session"
  16. )
  17. func (h *handler) oauth2Callback(w http.ResponseWriter, r *http.Request) {
  18. clientIP := request.ClientIP(r)
  19. printer := locale.NewPrinter(request.UserLanguage(r))
  20. sess := session.New(h.store, request.SessionID(r))
  21. provider := request.RouteStringParam(r, "provider")
  22. if provider == "" {
  23. logger.Error("[OAuth2] Invalid or missing provider")
  24. html.Redirect(w, r, route.Path(h.router, "login"))
  25. return
  26. }
  27. code := request.QueryStringParam(r, "code", "")
  28. if code == "" {
  29. logger.Error("[OAuth2] No code received on callback")
  30. html.Redirect(w, r, route.Path(h.router, "login"))
  31. return
  32. }
  33. state := request.QueryStringParam(r, "state", "")
  34. if state == "" || state != request.OAuth2State(r) {
  35. logger.Error(`[OAuth2] Invalid state value: got "%s" instead of "%s"`, state, request.OAuth2State(r))
  36. html.Redirect(w, r, route.Path(h.router, "login"))
  37. return
  38. }
  39. authProvider, err := getOAuth2Manager(r.Context()).FindProvider(provider)
  40. if err != nil {
  41. logger.Error("[OAuth2] %v", err)
  42. html.Redirect(w, r, route.Path(h.router, "login"))
  43. return
  44. }
  45. profile, err := authProvider.GetProfile(r.Context(), code)
  46. if err != nil {
  47. logger.Error("[OAuth2] %v", err)
  48. html.Redirect(w, r, route.Path(h.router, "login"))
  49. return
  50. }
  51. logger.Info("[OAuth2] [ClientIP=%s] Successful auth for %s", clientIP, profile)
  52. if request.IsAuthenticated(r) {
  53. loggedUser, err := h.store.UserByID(request.UserID(r))
  54. if err != nil {
  55. html.ServerError(w, r, err)
  56. return
  57. }
  58. if h.store.AnotherUserWithFieldExists(loggedUser.ID, profile.Key, profile.ID) {
  59. logger.Error("[OAuth2] User #%d cannot be associated because it is already associated with another user", loggedUser.ID)
  60. sess.NewFlashErrorMessage(printer.Printf("error.duplicate_linked_account"))
  61. html.Redirect(w, r, route.Path(h.router, "settings"))
  62. return
  63. }
  64. authProvider.PopulateUserWithProfileID(loggedUser, profile)
  65. if err := h.store.UpdateUser(loggedUser); err != nil {
  66. html.ServerError(w, r, err)
  67. return
  68. }
  69. sess.NewFlashMessage(printer.Printf("alert.account_linked"))
  70. html.Redirect(w, r, route.Path(h.router, "settings"))
  71. return
  72. }
  73. user, err := h.store.UserByField(profile.Key, profile.ID)
  74. if err != nil {
  75. html.ServerError(w, r, err)
  76. return
  77. }
  78. if user == nil {
  79. if !config.Opts.IsOAuth2UserCreationAllowed() {
  80. html.Forbidden(w, r)
  81. return
  82. }
  83. if h.store.UserExists(profile.Username) {
  84. html.BadRequest(w, r, errors.New(printer.Printf("error.user_already_exists")))
  85. return
  86. }
  87. userCreationRequest := &model.UserCreationRequest{Username: profile.Username}
  88. authProvider.PopulateUserCreationWithProfileID(userCreationRequest, profile)
  89. user, err = h.store.CreateUser(userCreationRequest)
  90. if err != nil {
  91. html.ServerError(w, r, err)
  92. return
  93. }
  94. }
  95. sessionToken, _, err := h.store.CreateUserSessionFromUsername(user.Username, r.UserAgent(), clientIP)
  96. if err != nil {
  97. html.ServerError(w, r, err)
  98. return
  99. }
  100. logger.Info("[OAuth2] [ClientIP=%s] username=%s (%s) just logged in", clientIP, user.Username, profile)
  101. h.store.SetLastLogin(user.ID)
  102. sess.SetLanguage(user.Language)
  103. sess.SetTheme(user.Theme)
  104. http.SetCookie(w, cookie.New(
  105. cookie.CookieUserSessionID,
  106. sessionToken,
  107. config.Opts.HTTPS,
  108. config.Opts.BasePath(),
  109. ))
  110. html.Redirect(w, r, route.Path(h.router, "unread"))
  111. }