2018-04-30 01:35:04 +02: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-08 03:42:43 +02:00
|
|
|
package ui // import "miniflux.app/ui"
|
2018-04-30 01:35:04 +02:00
|
|
|
|
|
|
|
import (
|
2020-12-22 06:14:10 +01:00
|
|
|
"errors"
|
2018-04-30 01:35:04 +02:00
|
|
|
"net/http"
|
|
|
|
|
2019-06-02 03:18:09 +02:00
|
|
|
"miniflux.app/config"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/cookie"
|
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response/html"
|
|
|
|
"miniflux.app/http/route"
|
2018-09-23 00:04:55 +02:00
|
|
|
"miniflux.app/locale"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/ui/session"
|
2018-04-30 01:35:04 +02:00
|
|
|
)
|
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
func (h *handler) oauth2Callback(w http.ResponseWriter, r *http.Request) {
|
2018-10-23 05:49:10 +02:00
|
|
|
clientIP := request.ClientIP(r)
|
2018-09-23 00:04:55 +02:00
|
|
|
printer := locale.NewPrinter(request.UserLanguage(r))
|
2018-11-11 20:28:29 +01:00
|
|
|
sess := session.New(h.store, request.SessionID(r))
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2018-09-24 06:02:26 +02:00
|
|
|
provider := request.RouteStringParam(r, "provider")
|
2018-04-30 01:35:04 +02:00
|
|
|
if provider == "" {
|
|
|
|
logger.Error("[OAuth2] Invalid or missing provider")
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-24 06:02:26 +02:00
|
|
|
code := request.QueryStringParam(r, "code", "")
|
2018-04-30 01:35:04 +02:00
|
|
|
if code == "" {
|
|
|
|
logger.Error("[OAuth2] No code received on callback")
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-24 06:02:26 +02:00
|
|
|
state := request.QueryStringParam(r, "state", "")
|
2018-09-03 23:26:40 +02: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 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01:00
|
|
|
authProvider, err := getOAuth2Manager(r.Context()).FindProvider(provider)
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("[OAuth2] %v", err)
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-08 03:45:19 +01:00
|
|
|
profile, err := authProvider.GetProfile(r.Context(), code)
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Error("[OAuth2] %v", err)
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "login"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-23 05:49:10 +02:00
|
|
|
logger.Info("[OAuth2] [ClientIP=%s] Successful auth for %s", clientIP, profile)
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
if request.IsAuthenticated(r) {
|
2020-12-22 06:14:10 +01:00
|
|
|
loggedUser, err := h.store.UserByID(request.UserID(r))
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01: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-23 00:04:55 +02:00
|
|
|
sess.NewFlashErrorMessage(printer.Printf("error.duplicate_linked_account"))
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "settings"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01:00
|
|
|
authProvider.PopulateUserWithProfileID(loggedUser, profile)
|
|
|
|
if err := h.store.UpdateUser(loggedUser); err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-23 00:04:55 +02:00
|
|
|
sess.NewFlashMessage(printer.Printf("alert.account_linked"))
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "settings"))
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01:00
|
|
|
user, err := h.store.UserByField(profile.Key, profile.ID)
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2019-06-02 03:18:09 +02:00
|
|
|
if !config.Opts.IsOAuth2UserCreationAllowed() {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.Forbidden(w, r)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:14:10 +01:00
|
|
|
if h.store.UserExists(profile.Username) {
|
|
|
|
html.BadRequest(w, r, errors.New(printer.Printf("error.user_already_exists")))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:20:21 +01:00
|
|
|
userCreationRequest := &model.UserCreationRequest{Username: profile.Username}
|
|
|
|
authProvider.PopulateUserCreationWithProfileID(userCreationRequest, profile)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
2021-01-04 06:20:21 +01:00
|
|
|
user, err = h.store.CreateUser(userCreationRequest)
|
|
|
|
if err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 05:28:34 +01:00
|
|
|
sessionToken, _, err := h.store.CreateUserSessionFromUsername(user.Username, r.UserAgent(), clientIP)
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
2018-10-08 03:42:43 +02:00
|
|
|
html.ServerError(w, r, err)
|
2018-04-30 01:35:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-23 05:49:10 +02:00
|
|
|
logger.Info("[OAuth2] [ClientIP=%s] username=%s (%s) just logged in", clientIP, user.Username, profile)
|
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
h.store.SetLastLogin(user.ID)
|
2018-04-30 01:35:04 +02:00
|
|
|
sess.SetLanguage(user.Language)
|
2018-07-19 07:30:05 +02:00
|
|
|
sess.SetTheme(user.Theme)
|
2018-04-30 01:35:04 +02:00
|
|
|
|
|
|
|
http.SetCookie(w, cookie.New(
|
|
|
|
cookie.CookieUserSessionID,
|
|
|
|
sessionToken,
|
2019-06-02 03:18:09 +02:00
|
|
|
config.Opts.HTTPS,
|
|
|
|
config.Opts.BasePath(),
|
2018-04-30 01:35:04 +02:00
|
|
|
))
|
|
|
|
|
2018-11-11 20:28:29 +01:00
|
|
|
html.Redirect(w, r, route.Path(h.router, "unread"))
|
2018-04-30 01:35:04 +02:00
|
|
|
}
|