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"
|
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/logger"
|
|
|
|
"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 == "" {
|
|
|
|
logger.Error("[OAuth2] Invalid or missing 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 == "" {
|
|
|
|
logger.Error("[OAuth2] No code received on 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 {
|
2018-09-03 17:26:40 -04:00
|
|
|
logger.Error(`[OAuth2] Invalid state value: got "%s" instead of "%s"`, state, request.OAuth2State(r))
|
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 {
|
|
|
|
logger.Error("[OAuth2] %v", 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 {
|
|
|
|
logger.Error("[OAuth2] %v", 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-10-22 23:49:10 -04:00
|
|
|
logger.Info("[OAuth2] [ClientIP=%s] Successful auth for %s", clientIP, profile)
|
|
|
|
|
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) {
|
|
|
|
logger.Error("[OAuth2] User #%d cannot be associated because it is already associated with another user", loggedUser.ID)
|
2018-09-22 18:04:55 -04:00
|
|
|
sess.NewFlashErrorMessage(printer.Printf("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
|
|
|
|
}
|
|
|
|
|
2018-09-22 18:04:55 -04:00
|
|
|
sess.NewFlashMessage(printer.Printf("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) {
|
|
|
|
html.BadRequest(w, r, errors.New(printer.Printf("error.user_already_exists")))
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-10-22 23:49:10 -04:00
|
|
|
logger.Info("[OAuth2] [ClientIP=%s] username=%s (%s) just logged in", clientIP, user.Username, profile)
|
|
|
|
|
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
|
|
|
))
|
|
|
|
|
2018-11-11 14:28:29 -05:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "unread"))
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|