mirror of
https://github.com/simon987/task_tracker.git
synced 2025-12-10 21:48:52 +00:00
some work on auth/sessions
This commit is contained in:
53
api/auth.go
Normal file
53
api/auth.go
Normal 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")
|
||||
}
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
17
api/main.go
17
api/main.go
@@ -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,
|
||||
|
||||
@@ -3,7 +3,7 @@ package api
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/Sirupsen/logrus"
|
||||
"src/task_tracker/storage"
|
||||
"github.com/simon987/task_tracker/storage"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user