miniflux/ui/oauth2.go

172 lines
4.7 KiB
Go
Raw Normal View History

2017-11-23 07:22:33 +01: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.
package ui
2017-11-23 07:22:33 +01:00
import (
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/config"
"github.com/miniflux/miniflux/http/cookie"
"github.com/miniflux/miniflux/http/handler"
2017-12-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/model"
"github.com/miniflux/miniflux/oauth2"
2017-11-23 07:22:33 +01:00
"github.com/tomasen/realip"
)
// OAuth2Redirect redirects the user to the consent page to ask for permission.
func (c *Controller) OAuth2Redirect(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-23 07:22:33 +01:00
provider := request.StringParam("provider", "")
if provider == "" {
2017-12-17 03:07:53 +01:00
logger.Error("[OAuth2] Invalid or missing provider: %s", provider)
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("login"))
return
}
authProvider, err := getOAuth2Manager(c.cfg).Provider(provider)
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[OAuth2] %v", err)
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("login"))
return
}
2017-12-17 03:07:53 +01:00
response.Redirect(authProvider.GetRedirectURL(ctx.GenerateOAuth2State()))
2017-11-23 07:22:33 +01:00
}
// OAuth2Callback receives the authorization code and create a new session.
func (c *Controller) OAuth2Callback(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-23 07:22:33 +01:00
provider := request.StringParam("provider", "")
if provider == "" {
2017-12-16 03:55:57 +01:00
logger.Error("[OAuth2] Invalid or missing provider")
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("login"))
return
}
code := request.QueryStringParam("code", "")
if code == "" {
2017-12-16 03:55:57 +01:00
logger.Error("[OAuth2] No code received on callback")
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("login"))
return
}
state := request.QueryStringParam("state", "")
2017-12-17 03:07:53 +01:00
if state == "" || state != ctx.OAuth2State() {
logger.Error(`[OAuth2] Invalid state value: got "%s" instead of "%s"`, state, ctx.OAuth2State())
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("login"))
return
}
authProvider, err := getOAuth2Manager(c.cfg).Provider(provider)
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[OAuth2] %v", err)
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("login"))
return
}
profile, err := authProvider.GetProfile(code)
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[OAuth2] %v", err)
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("login"))
return
}
2017-11-25 01:09:10 +01:00
if ctx.IsAuthenticated() {
user, err := c.store.UserByExtraField(profile.Key, profile.ID)
if err != nil {
response.HTML().ServerError(err)
return
}
if user != nil {
logger.Error("[OAuth2] User #%d cannot be associated because %s is already associated", ctx.UserID(), user.Username)
ctx.SetFlashErrorMessage(ctx.Translate("There is already someone associated with this provider!"))
response.Redirect(ctx.Route("settings"))
return
}
user = ctx.LoggedUser()
2017-11-25 01:09:10 +01:00
if err := c.store.UpdateExtraField(user.ID, profile.Key, profile.ID); err != nil {
response.HTML().ServerError(err)
return
}
2017-12-17 03:07:53 +01:00
ctx.SetFlashMessage(ctx.Translate("Your external account is now linked !"))
2017-11-25 01:09:10 +01:00
response.Redirect(ctx.Route("settings"))
return
}
2017-11-28 06:30:04 +01:00
user, err := c.store.UserByExtraField(profile.Key, profile.ID)
2017-11-23 07:22:33 +01:00
if err != nil {
response.HTML().ServerError(err)
return
}
if user == nil {
if !c.cfg.IsOAuth2UserCreationAllowed() {
2017-11-25 01:09:10 +01:00
response.HTML().Forbidden()
return
}
2017-11-23 07:22:33 +01:00
user = model.NewUser()
user.Username = profile.Username
user.IsAdmin = false
user.Extra[profile.Key] = profile.ID
if err := c.store.CreateUser(user); err != nil {
response.HTML().ServerError(err)
return
}
}
2017-12-16 21:15:33 +01:00
sessionToken, err := c.store.CreateUserSession(
2017-11-23 07:22:33 +01:00
user.Username,
request.Request().UserAgent(),
realip.RealIP(request.Request()),
)
if err != nil {
response.HTML().ServerError(err)
return
}
2017-12-16 03:55:57 +01:00
logger.Info("[Controller:OAuth2Callback] username=%s just logged in", user.Username)
2017-11-23 07:22:33 +01:00
response.SetCookie(cookie.New(cookie.CookieUserSessionID, sessionToken, c.cfg.IsHTTPS))
2017-11-23 07:22:33 +01:00
response.Redirect(ctx.Route("unread"))
}
2017-11-25 01:09:10 +01:00
// OAuth2Unlink unlink an account from the external provider.
func (c *Controller) OAuth2Unlink(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-25 01:09:10 +01:00
provider := request.StringParam("provider", "")
if provider == "" {
2017-12-16 03:55:57 +01:00
logger.Info("[OAuth2] Invalid or missing provider")
2017-11-25 01:09:10 +01:00
response.Redirect(ctx.Route("login"))
return
}
authProvider, err := getOAuth2Manager(c.cfg).Provider(provider)
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[OAuth2] %v", err)
2017-11-25 01:09:10 +01:00
response.Redirect(ctx.Route("settings"))
return
}
user := ctx.LoggedUser()
if err := c.store.RemoveExtraField(user.ID, authProvider.GetUserExtraKey()); err != nil {
response.HTML().ServerError(err)
return
}
response.Redirect(ctx.Route("settings"))
return
}
2017-11-23 07:22:33 +01:00
func getOAuth2Manager(cfg *config.Config) *oauth2.Manager {
return oauth2.NewManager(
cfg.OAuth2ClientID(),
cfg.OAuth2ClientSecret(),
cfg.OAuth2RedirectURL(),
2017-11-23 07:22:33 +01:00
)
}