feat: Added basic user authorization
This commit is contained in:
63
router/controller/auth/controller.go
Normal file
63
router/controller/auth/controller.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/dto"
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/auth"
|
||||
httpApp "git.ostiwe.com/ostiwe-com/status/pkg/http"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
||||
"github.com/go-andiamo/chioas"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/render"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
authModule auth.Module
|
||||
}
|
||||
|
||||
func (*Controller) New() controller.Controller {
|
||||
return &Controller{
|
||||
authModule: auth.New(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) plainLogin(w http.ResponseWriter, r *http.Request) {
|
||||
var payload dto.LoginRequest
|
||||
|
||||
if err := render.Bind(r, &payload); err != nil {
|
||||
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
|
||||
if sendErr != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
jwtString, err := c.authModule.Proceed(payload.Login, payload.Password)
|
||||
if err != nil {
|
||||
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
|
||||
if sendErr != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
response := dto.LoginResponse{
|
||||
AccessToken: *jwtString,
|
||||
}
|
||||
|
||||
render.JSON(w, r, response)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func (c *Controller) Group(r chi.Router) {
|
||||
r.Post("/auth/plain", c.plainLogin)
|
||||
}
|
||||
|
||||
func (c *Controller) Documentate() (*chioas.Paths, *chioas.Components) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -3,13 +3,16 @@ package service
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/jwt"
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
http2 "git.ostiwe.com/ostiwe-com/status/pkg/http"
|
||||
"git.ostiwe.com/ostiwe-com/status/repository"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/midlleware"
|
||||
"git.ostiwe.com/ostiwe-com/status/transform"
|
||||
"github.com/go-andiamo/chioas"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
@@ -32,7 +35,16 @@ func (c *Controller) public(r chi.Router) {
|
||||
}
|
||||
|
||||
func (c *Controller) internal(r chi.Router) {
|
||||
r.Get("/api/v1/service", c.GetAllServices)
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(
|
||||
jwtauth.Verifier(jwt.TokenAuth),
|
||||
jwtauth.Authenticator(jwt.TokenAuth),
|
||||
midlleware.SetUserFromJWT,
|
||||
)
|
||||
|
||||
r.Get("/api/v1/service", c.GetAllServices)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func (c *Controller) GetAllServicesPublic(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
33
router/midlleware/user.go
Normal file
33
router/midlleware/user.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package midlleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/repository"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
)
|
||||
|
||||
func SetUserFromJWT(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
token, _, err := jwtauth.FromContext(ctx)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
userRepo := repository.NewUserRepository()
|
||||
user, err := userRepo.FindByLogin(token.Subject())
|
||||
if err != nil || user == nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, "user", user)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/log"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller/auth"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller/ping"
|
||||
"git.ostiwe.com/ostiwe-com/status/router/controller/service"
|
||||
"git.ostiwe.com/ostiwe-com/status/version"
|
||||
@@ -19,6 +20,7 @@ func getControllers() []controller.Controller {
|
||||
return []controller.Controller{
|
||||
new(ping.Controller).New(),
|
||||
new(service.Controller).New(),
|
||||
new(auth.Controller).New(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user