miniflux-v2/ui/login.go

77 lines
2.3 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +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-20 06:10:04 +01:00
import (
"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"
"github.com/miniflux/miniflux/ui/form"
2017-11-23 07:22:33 +01:00
2017-11-20 06:10:04 +01:00
"github.com/tomasen/realip"
)
2017-11-23 07:22:33 +01:00
// ShowLoginPage shows the login form.
func (c *Controller) ShowLoginPage(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-20 06:10:04 +01:00
if ctx.IsAuthenticated() {
2017-11-22 03:37:08 +01:00
response.Redirect(ctx.Route("unread"))
2017-11-20 06:10:04 +01:00
return
}
2017-11-22 03:30:16 +01:00
response.HTML().Render("login", tplParams{
2017-12-17 03:07:53 +01:00
"csrf": ctx.CSRF(),
2017-11-20 06:10:04 +01:00
})
}
2017-11-23 07:22:33 +01:00
// CheckLogin validates the username/password and redirects the user to the unread page.
func (c *Controller) CheckLogin(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-22 03:14:45 +01:00
authForm := form.NewAuthForm(request.Request())
2017-11-20 06:10:04 +01:00
tplParams := tplParams{
"errorMessage": "Invalid username or password.",
2017-12-17 03:07:53 +01:00
"csrf": ctx.CSRF(),
2017-12-23 01:30:17 +01:00
"form": authForm,
2017-11-20 06:10:04 +01:00
}
if err := authForm.Validate(); err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[Controller:CheckLogin] %v", err)
2017-11-22 03:30:16 +01:00
response.HTML().Render("login", tplParams)
2017-11-20 06:10:04 +01:00
return
}
if err := c.store.CheckPassword(authForm.Username, authForm.Password); err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[Controller:CheckLogin] %v", err)
2017-11-22 03:30:16 +01:00
response.HTML().Render("login", tplParams)
2017-11-20 06:10:04 +01:00
return
}
2017-12-16 21:15:33 +01:00
sessionToken, err := c.store.CreateUserSession(
2017-11-20 06:10:04 +01:00
authForm.Username,
2017-11-22 03:14:45 +01:00
request.Request().UserAgent(),
realip.RealIP(request.Request()),
2017-11-20 06:10:04 +01:00
)
2017-11-23 07:22:33 +01:00
2017-11-20 06:10:04 +01:00
if err != nil {
2017-11-22 03:30:16 +01:00
response.HTML().ServerError(err)
2017-11-20 06:10:04 +01:00
return
}
2017-12-16 03:55:57 +01:00
logger.Info("[Controller:CheckLogin] username=%s just logged in", authForm.Username)
2017-11-20 06:10:04 +01:00
response.SetCookie(cookie.New(cookie.CookieUserSessionID, sessionToken, c.cfg.IsHTTPS))
2017-11-22 03:37:08 +01:00
response.Redirect(ctx.Route("unread"))
2017-11-20 06:10:04 +01:00
}
2017-11-23 07:22:33 +01:00
// Logout destroy the session and redirects the user to the login page.
func (c *Controller) Logout(ctx *handler.Context, request *handler.Request, response *handler.Response) {
2017-11-22 03:37:08 +01:00
user := ctx.LoggedUser()
2017-11-20 06:10:04 +01:00
2017-12-17 03:07:53 +01:00
if err := c.store.RemoveUserSessionByToken(user.ID, ctx.UserSessionToken()); err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[Controller:Logout] %v", err)
2017-11-20 06:10:04 +01:00
}
response.SetCookie(cookie.Expired(cookie.CookieUserSessionID, c.cfg.IsHTTPS))
2017-11-22 03:37:08 +01:00
response.Redirect(ctx.Route("login"))
2017-11-20 06:10:04 +01:00
}