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-08-25 06:51:50 +02:00
|
|
|
package ui // import "miniflux.app/ui"
|
2018-04-30 01:35:04 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/cookie"
|
|
|
|
"miniflux.app/http/request"
|
|
|
|
"miniflux.app/http/response"
|
|
|
|
"miniflux.app/http/response/html"
|
|
|
|
"miniflux.app/http/route"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/ui/session"
|
2018-04-30 01:35:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// OAuth2Callback receives the authorization code and create a new session.
|
|
|
|
func (c *Controller) OAuth2Callback(w http.ResponseWriter, r *http.Request) {
|
2018-09-03 23:26:40 +02:00
|
|
|
sess := session.New(c.store, request.SessionID(r))
|
2018-04-30 01:35:04 +02:00
|
|
|
|
|
|
|
provider := request.Param(r, "provider", "")
|
|
|
|
if provider == "" {
|
|
|
|
logger.Error("[OAuth2] Invalid or missing provider")
|
|
|
|
response.Redirect(w, r, route.Path(c.router, "login"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
code := request.QueryParam(r, "code", "")
|
|
|
|
if code == "" {
|
|
|
|
logger.Error("[OAuth2] No code received on callback")
|
|
|
|
response.Redirect(w, r, route.Path(c.router, "login"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
state := request.QueryParam(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-04-30 01:35:04 +02:00
|
|
|
response.Redirect(w, r, route.Path(c.router, "login"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
authProvider, err := getOAuth2Manager(c.cfg).Provider(provider)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("[OAuth2] %v", err)
|
|
|
|
response.Redirect(w, r, route.Path(c.router, "login"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
profile, err := authProvider.GetProfile(code)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("[OAuth2] %v", err)
|
|
|
|
response.Redirect(w, r, route.Path(c.router, "login"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
if request.IsAuthenticated(r) {
|
2018-04-30 01:35:04 +02:00
|
|
|
user, err := c.store.UserByExtraField(profile.Key, profile.ID)
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user != nil {
|
2018-09-03 23:26:40 +02:00
|
|
|
logger.Error("[OAuth2] User #%d cannot be associated because %s is already associated", request.UserID(r), user.Username)
|
2018-09-22 03:53:29 +02:00
|
|
|
sess.NewFlashErrorMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("error.duplicate_linked_account"))
|
2018-04-30 01:35:04 +02:00
|
|
|
response.Redirect(w, r, route.Path(c.router, "settings"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-03 23:26:40 +02:00
|
|
|
if err := c.store.UpdateExtraField(request.UserID(r), profile.Key, profile.ID); err != nil {
|
2018-04-30 01:35:04 +02:00
|
|
|
html.ServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-22 03:53:29 +02:00
|
|
|
sess.NewFlashMessage(c.translator.GetLanguage(request.UserLanguage(r)).Get("alert.account_linked"))
|
2018-04-30 01:35:04 +02:00
|
|
|
response.Redirect(w, r, route.Path(c.router, "settings"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := c.store.UserByExtraField(profile.Key, profile.ID)
|
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
if !c.cfg.IsOAuth2UserCreationAllowed() {
|
|
|
|
html.Forbidden(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user = model.NewUser()
|
|
|
|
user.Username = profile.Username
|
|
|
|
user.IsAdmin = false
|
|
|
|
user.Extra[profile.Key] = profile.ID
|
|
|
|
|
|
|
|
if err := c.store.CreateUser(user); err != nil {
|
|
|
|
html.ServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 00:15:14 +02:00
|
|
|
sessionToken, _, err := c.store.CreateUserSession(user.Username, r.UserAgent(), request.ClientIP(r))
|
2018-04-30 01:35:04 +02:00
|
|
|
if err != nil {
|
|
|
|
html.ServerError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("[Controller:OAuth2Callback] username=%s just logged in", user.Username)
|
|
|
|
c.store.SetLastLogin(user.ID)
|
|
|
|
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,
|
|
|
|
c.cfg.IsHTTPS,
|
|
|
|
c.cfg.BasePath(),
|
|
|
|
))
|
|
|
|
|
|
|
|
response.Redirect(w, r, route.Path(c.router, "unread"))
|
|
|
|
}
|