2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-04-29 19:35:04 -04:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package ui // import "miniflux.app/v2/internal/ui"
|
2018-04-29 19:35:04 -04:00
|
|
|
|
|
|
|
import (
|
2023-09-03 00:35:10 -04:00
|
|
|
"crypto/subtle"
|
2020-12-22 00:14:10 -05:00
|
|
|
"errors"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
2018-04-29 19:35:04 -04:00
|
|
|
"net/http"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/http/cookie"
|
|
|
|
"miniflux.app/v2/internal/http/request"
|
|
|
|
"miniflux.app/v2/internal/http/response/html"
|
|
|
|
"miniflux.app/v2/internal/http/route"
|
|
|
|
"miniflux.app/v2/internal/locale"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/ui/session"
|
2018-04-29 19:35:04 -04:00
|
|
|
)
|
|
|
|
|
2018-11-11 14:28:29 -05:00
|
|
|
func (h *handler) oauth2Callback(w http.ResponseWriter, r *http.Request) {
|
2018-10-22 23:49:10 -04:00
|
|
|
clientIP := request.ClientIP(r)
|
2018-09-22 18:04:55 -04:00
|
|
|
printer := locale.NewPrinter(request.UserLanguage(r))
|
2018-11-11 14:28:29 -05:00
|
|
|
sess := session.New(h.store, request.SessionID(r))
|
2018-04-29 19:35:04 -04:00
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
provider := request.RouteStringParam(r, "provider")
|
2018-04-29 19:35:04 -04:00
|
|
|
if provider == "" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("Invalid or missing OAuth2 provider")
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
code := request.QueryStringParam(r, "code", "")
|
2018-04-29 19:35:04 -04:00
|
|
|
if code == "" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("No code received on OAuth2 callback")
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-24 00:02:26 -04:00
|
|
|
state := request.QueryStringParam(r, "state", "")
|
2023-09-03 00:35:10 -04:00
|
|
|
if subtle.ConstantTimeCompare([]byte(state), []byte(request.OAuth2State(r))) == 0 {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("Invalid OAuth2 state value received",
|
|
|
|
slog.String("expected", request.OAuth2State(r)),
|
|
|
|
slog.String("received", state),
|
|
|
|
)
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:14:10 -05:00
|
|
|
authProvider, err := getOAuth2Manager(r.Context()).FindProvider(provider)
|
2018-04-29 19:35:04 -04:00
|
|
|
if err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("Unable to initialize OAuth2 provider",
|
|
|
|
slog.String("provider", provider),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:35:10 -04:00
|
|
|
profile, err := authProvider.GetProfile(r.Context(), code, request.OAuth2CodeVerifier(r))
|
2018-04-29 19:35:04 -04:00
|
|
|
if err != nil {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Warn("Unable to get OAuth2 profile from provider",
|
|
|
|
slog.String("provider", provider),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 17:26:40 -04:00
|
|
|
if request.IsAuthenticated(r) {
|
2020-12-22 00:14:10 -05:00
|
|
|
loggedUser, err := h.store.UserByID(request.UserID(r))
|
2018-04-29 19:35:04 -04:00
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:14:10 -05:00
|
|
|
if h.store.AnotherUserWithFieldExists(loggedUser.ID, profile.Key, profile.ID) {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Error("Oauth2 user cannot be associated because it is already associated with another user",
|
|
|
|
slog.Int64("user_id", loggedUser.ID),
|
|
|
|
slog.String("oauth2_provider", provider),
|
|
|
|
slog.String("oauth2_profile_id", profile.ID),
|
|
|
|
)
|
2024-02-27 11:19:38 -05:00
|
|
|
sess.NewFlashErrorMessage(printer.Print("error.duplicate_linked_account"))
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "settings"))
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:14:10 -05:00
|
|
|
authProvider.PopulateUserWithProfileID(loggedUser, profile)
|
|
|
|
if err := h.store.UpdateUser(loggedUser); err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-27 11:19:38 -05:00
|
|
|
sess.NewFlashMessage(printer.Print("alert.account_linked"))
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "settings"))
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:14:10 -05:00
|
|
|
user, err := h.store.UserByField(profile.Key, profile.ID)
|
2018-04-29 19:35:04 -04:00
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2019-06-01 21:18:09 -04:00
|
|
|
if !config.Opts.IsOAuth2UserCreationAllowed() {
|
2018-10-07 21:42:43 -04:00
|
|
|
html.Forbidden(w, r)
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 00:14:10 -05:00
|
|
|
if h.store.UserExists(profile.Username) {
|
2024-02-27 11:19:38 -05:00
|
|
|
html.BadRequest(w, r, errors.New(printer.Print("error.user_already_exists")))
|
2020-12-22 00:14:10 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:20:21 -05:00
|
|
|
userCreationRequest := &model.UserCreationRequest{Username: profile.Username}
|
|
|
|
authProvider.PopulateUserCreationWithProfileID(userCreationRequest, profile)
|
2018-04-29 19:35:04 -04:00
|
|
|
|
2021-01-04 00:20:21 -05:00
|
|
|
user, err = h.store.CreateUser(userCreationRequest)
|
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 23:28:34 -05:00
|
|
|
sessionToken, _, err := h.store.CreateUserSessionFromUsername(user.Username, r.UserAgent(), clientIP)
|
2018-04-29 19:35:04 -04:00
|
|
|
if err != nil {
|
2018-10-07 21:42:43 -04:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-29 19:35:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Info("User authenticated successfully using OAuth2",
|
|
|
|
slog.Bool("authentication_successful", true),
|
|
|
|
slog.String("client_ip", clientIP),
|
|
|
|
slog.String("user_agent", r.UserAgent()),
|
|
|
|
slog.Int64("user_id", user.ID),
|
|
|
|
slog.String("username", user.Username),
|
|
|
|
)
|
2018-10-22 23:49:10 -04:00
|
|
|
|
2018-11-11 14:28:29 -05:00
|
|
|
h.store.SetLastLogin(user.ID)
|
2018-04-29 19:35:04 -04:00
|
|
|
sess.SetLanguage(user.Language)
|
2018-07-19 01:30:05 -04:00
|
|
|
sess.SetTheme(user.Theme)
|
2018-04-29 19:35:04 -04:00
|
|
|
|
|
|
|
http.SetCookie(w, cookie.New(
|
|
|
|
cookie.CookieUserSessionID,
|
|
|
|
sessionToken,
|
2019-06-01 21:18:09 -04:00
|
|
|
config.Opts.HTTPS,
|
|
|
|
config.Opts.BasePath(),
|
2018-04-29 19:35:04 -04:00
|
|
|
))
|
|
|
|
|
2023-11-07 14:29:19 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, user.DefaultHomePage))
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|