2018-04-29 19:35:04 -04:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-10-07 21:42:43 -04:00
|
|
|
package ui // import "miniflux.app/ui"
|
2018-04-29 19:35:04 -04:00
|
|
|
|
|
|
|
import (
|
2020-12-22 00:14:10 -05:00
|
|
|
"errors"
|
2018-04-29 19:35:04 -04:00
|
|
|
"net/http"
|
|
|
|
|
2019-06-01 21:18:09 -04:00
|
|
|
"miniflux.app/config"
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/http/cookie"
|
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/html"
|
|
|
|
"miniflux.app/http/route"
|
2018-09-22 18:04:55 -04:00
|
|
|
"miniflux.app/locale"
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/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", "")
|
2018-09-03 17:26:40 -04:00
|
|
|
if state == "" || state != request.OAuth2State(r) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-07 21:45:19 -05:00
|
|
|
profile, err := authProvider.GetProfile(r.Context(), code)
|
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
|
|
|
}
|