some work on auth/sessions

This commit is contained in:
simon987
2019-02-01 21:21:14 -05:00
parent d3188c512d
commit 9e09246a29
24 changed files with 265 additions and 66 deletions

53
api/auth.go Normal file
View File

@@ -0,0 +1,53 @@
package api
import (
"encoding/json"
"github.com/Sirupsen/logrus"
"github.com/kataras/go-sessions"
"github.com/simon987/task_tracker/storage"
)
type LoginRequest struct {
Username []byte
Password []byte
}
type LoginResponse struct {
Ok bool
Message string
Manager *storage.Manager
}
func (api *WebAPI) Login(r *Request) {
req := &LoginRequest{}
err := json.Unmarshal(r.Ctx.Request.Body(), req)
if err != nil {
r.Json(LoginResponse{
Ok: false,
Message: "Could not parse request",
}, 400)
return
}
manager, err := api.Database.ValidateCredentials(req.Username, req.Password)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"username": string(manager.Username),
}).Warning("Login attempt")
r.Json(LoginResponse{
Ok: false,
Message: "Invalid username/password",
}, 403)
return
}
sess := sessions.StartFasthttp(r.Ctx)
sess.Set("manager", manager)
logrus.WithFields(logrus.Fields{
"username": string(manager.Username),
}).Info("Logged in")
}

View File

@@ -7,10 +7,10 @@ import (
"encoding/json"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/simon987/task_tracker/config"
"github.com/simon987/task_tracker/storage"
"github.com/valyala/fasthttp"
"hash"
"src/task_tracker/config"
"src/task_tracker/storage"
"strings"
)

View File

@@ -4,9 +4,9 @@ import (
"encoding/json"
"errors"
"github.com/Sirupsen/logrus"
"github.com/simon987/task_tracker/config"
"github.com/simon987/task_tracker/storage"
"github.com/valyala/fasthttp"
"src/task_tracker/config"
"src/task_tracker/storage"
"time"
)

View File

@@ -4,15 +4,17 @@ import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/buaazp/fasthttprouter"
"github.com/kataras/go-sessions"
"github.com/simon987/task_tracker/config"
"github.com/simon987/task_tracker/storage"
"github.com/valyala/fasthttp"
"src/task_tracker/config"
"src/task_tracker/storage"
)
type WebAPI struct {
server *fasthttp.Server
router *fasthttprouter.Router
Database *storage.Database
server *fasthttp.Server
router *fasthttprouter.Router
Database *storage.Database
SessionConfig *sessions.Config
}
type Info struct {
@@ -36,6 +38,11 @@ func New() *WebAPI {
api.router = &fasthttprouter.Router{}
api.SessionConfig = &sessions.Config{
Cookie: config.Cfg.SessionCookieName,
Expires: config.Cfg.SessionCookieExpiration,
}
api.server = &fasthttp.Server{
Handler: api.router.Handler,
Name: info.Name,

View File

@@ -3,7 +3,7 @@ package api
import (
"encoding/json"
"github.com/Sirupsen/logrus"
"src/task_tracker/storage"
"github.com/simon987/task_tracker/storage"
"strconv"
)

View File

@@ -9,7 +9,7 @@ import (
"errors"
"github.com/Sirupsen/logrus"
"github.com/dchest/siphash"
"src/task_tracker/storage"
"github.com/simon987/task_tracker/storage"
"strconv"
)

View File

@@ -3,8 +3,8 @@ package api
import (
"encoding/json"
"github.com/Sirupsen/logrus"
"github.com/simon987/task_tracker/storage"
"math/rand"
"src/task_tracker/storage"
"strconv"
"time"
)