2015-07-29 07:01:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-12-28 01:40:20 +00:00
|
|
|
"context"
|
2015-07-29 07:01:23 +00:00
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2015-07-31 19:02:03 +00:00
|
|
|
"strings"
|
2015-07-29 07:01:23 +00:00
|
|
|
|
2017-12-24 18:44:24 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
2015-07-29 07:01:23 +00:00
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/sessions"
|
2017-12-24 19:10:43 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-07-29 07:01:23 +00:00
|
|
|
)
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
func loginHandler(c context.Context, res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
2015-07-29 07:01:23 +00:00
|
|
|
var (
|
2017-12-24 18:44:24 +00:00
|
|
|
username = strings.ToLower(r.FormValue("username"))
|
|
|
|
password = r.FormValue("password")
|
|
|
|
deprecatedPassword = fmt.Sprintf("%x", sha1.Sum([]byte(cfg.PasswordSalt+r.FormValue("password")))) // Here for backwards compatibility
|
2015-07-29 07:01:23 +00:00
|
|
|
)
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
if !storage.IsPresent(c, createUserFilename(username)) {
|
2015-07-29 07:01:23 +00:00
|
|
|
(*ctx)["error"] = true
|
2015-07-29 07:49:23 +00:00
|
|
|
return stringPointer("login.html"), nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
userFileRaw, err := storage.Read(c, createUserFilename(username))
|
2015-07-29 07:01:23 +00:00
|
|
|
if err != nil {
|
2017-12-24 19:10:43 +00:00
|
|
|
log.WithError(err).Error("Unable to read user file")
|
2015-07-29 08:10:46 +00:00
|
|
|
(*ctx)["error"] = true
|
|
|
|
return stringPointer("login.html"), nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 08:10:46 +00:00
|
|
|
userFile, _ := readDataObject(userFileRaw)
|
2015-07-29 07:01:23 +00:00
|
|
|
|
2017-12-24 18:44:24 +00:00
|
|
|
bcryptValidationError := bcrypt.CompareHashAndPassword([]byte(userFile.MetaData.Password), []byte(password))
|
|
|
|
if bcryptValidationError != nil && userFile.MetaData.Password != deprecatedPassword {
|
2015-07-29 07:01:23 +00:00
|
|
|
(*ctx)["error"] = true
|
2015-07-29 07:49:23 +00:00
|
|
|
return stringPointer("login.html"), nil
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auth, ok := session.Values["authorizedAccounts"].(authorizedAccounts)
|
|
|
|
if !ok {
|
|
|
|
auth = authorizedAccounts{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range auth {
|
|
|
|
if v.Name == username {
|
|
|
|
http.Redirect(res, r, fmt.Sprintf("u/%d/overview", i), http.StatusFound)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auth = append(auth, authorizedAccount{
|
|
|
|
Name: username,
|
|
|
|
UserFile: createUserFilename(username),
|
|
|
|
})
|
|
|
|
|
|
|
|
session.Values["authorizedAccounts"] = auth
|
|
|
|
if err := session.Save(r, res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(res, r, fmt.Sprintf("u/%d/overview", len(auth)-1), http.StatusFound)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
func logoutHandler(c context.Context, res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
|
2015-07-29 07:01:23 +00:00
|
|
|
session.Values["authorizedAccounts"] = authorizedAccounts{}
|
|
|
|
session.Save(r, res)
|
|
|
|
http.Redirect(res, r, "overview", http.StatusFound)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:40:20 +00:00
|
|
|
func checkLogin(c context.Context, r *http.Request, session *sessions.Session) (*authorizedAccount, error) {
|
2015-07-29 07:01:23 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
idx, err := strconv.ParseInt(vars["userIndex"], 10, 64)
|
|
|
|
if err != nil {
|
2015-07-29 08:10:46 +00:00
|
|
|
return nil, err
|
2015-07-29 07:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auth, ok := session.Values["authorizedAccounts"].(authorizedAccounts)
|
|
|
|
if !ok {
|
|
|
|
auth = authorizedAccounts{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(auth)-1 < int(idx) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &auth[idx], nil
|
|
|
|
}
|