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 (
|
|
|
|
"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-03-07 21:45:19 -05:00
|
|
|
authProvider, err := getOAuth2Manager(r.Context()).Provider(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) {
|
2018-11-11 14:28:29 -05:00
|
|
|
user, err := h.store.UserByExtraField(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 {
|
2018-09-03 17:26:40 -04:00
|
|
|
logger.Error("[OAuth2] User #%d cannot be associated because %s is already associated", request.UserID(r), user.Username)
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-11 14:28:29 -05:00
|
|
|
if err := h.store.UpdateExtraField(request.UserID(r), profile.Key, profile.ID); 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
|
|
|
|
}
|
|
|
|
|
2018-11-11 14:28:29 -05:00
|
|
|
user, err := h.store.UserByExtraField(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
|
|
|
|
}
|
|
|
|
|
|
|
|
user = model.NewUser()
|
|
|
|
user.Username = profile.Username
|
|
|
|
user.IsAdmin = false
|
|
|
|
user.Extra[profile.Key] = profile.ID
|
|
|
|
|
2018-11-11 14:28:29 -05:00
|
|
|
if err := h.store.CreateUser(user); 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-11-11 14:28:29 -05:00
|
|
|
sessionToken, _, err := h.store.CreateUserSession(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
|
|
|
}
|