features/working-on-scopes #13

Merged
jtom38 merged 28 commits from features/working-on-scopes into main 2024-04-04 15:31:54 -07:00
2 changed files with 53 additions and 15 deletions
Showing only changes of commit 7dc072e849 - Show all commits

View File

@ -30,8 +30,9 @@ func (h *Handler) AuthRegister(c echo.Context) error {
// if the user is not found, we can use that name // if the user is not found, we can use that name
if err.Error() != repositories.ErrUserNotFound { if err.Error() != repositories.ErrUserNotFound {
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
HttpCode: http.StatusInternalServerError,
Message: err.Error(), Message: err.Error(),
Success: true,
}) })
} }
} }
@ -43,7 +44,7 @@ func (h *Handler) AuthRegister(c echo.Context) error {
err = h.UserService.CheckPasswordForRequirements(password) err = h.UserService.CheckPasswordForRequirements(password)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
HttpCode: http.StatusInternalServerError, Success: false,
Message: err.Error(), Message: err.Error(),
}) })
} }
@ -51,7 +52,7 @@ func (h *Handler) AuthRegister(c echo.Context) error {
_, err = h.userRepo.Create(username, password, domain.ScopeRecipeRead) _, err = h.userRepo.Create(username, password, domain.ScopeRecipeRead)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{ return c.JSON(http.StatusInternalServerError, domain.ErrorResponse{
HttpCode: http.StatusInternalServerError, Success: false,
Message: err.Error(), Message: err.Error(),
}) })
} }
@ -101,9 +102,42 @@ func (h *Handler) validateAdminToken(c echo.Context, password string) error {
return c.JSON(http.StatusOK, token) return c.JSON(http.StatusOK, token)
} }
//func (h *Handler) AddScope(c echo.Context) error { func (h *Handler) AddScope(c echo.Context) error {
// token, err := h.getJwtToken(c)
//} if err != nil {
return h.ReturnUnauthorizedResponse(c, err.Error())
}
err = token.IsValid(domain.ScopeAll)
if err != nil {
return h.ReturnUnauthorizedResponse(c, err.Error())
}
request := domain.AddScopeRequest{}
err = (&echo.DefaultBinder{}).BindBody(c, &request)
if err != nil {
return c.JSON(http.StatusBadRequest, domain.ErrorResponse{
Success: false,
Message: err.Error(),
})
}
err = h.UserService.AddScopes(request.Username, request.Scopes)
if err != nil {
return h.InternalServerErrorResponse(c, err.Error())
}
return c.JSON(http.StatusOK, domain.ErrorResponse{
Success: true,
})
}
func (h *Handler) RemoveScope(c echo.Context) error {
return c.JSON(http.StatusOK, domain.ErrorResponse{
Success: false,
Message: "Not Implemented",
})
}
func (h *Handler) RefreshJwtToken(c echo.Context) error { func (h *Handler) RefreshJwtToken(c echo.Context) error {
return nil return nil

View File

@ -37,10 +37,14 @@ func (h *Handler) Register(v1 *echo.Group) {
SigningKey: []byte(h.Config.JwtSecret), SigningKey: []byte(h.Config.JwtSecret),
} }
v1.POST("/login", h.AuthLogin) auth := v1.Group("/auth")
v1.POST("/register", h.AuthRegister) auth.POST("/login", h.AuthLogin)
demo := v1.Group("/demo") auth.POST("/register", h.AuthRegister)
auth.Use(echojwt.WithConfig(jwtConfig))
auth.POST("/scopes/add", h.AddScope)
//auth.POST("/refresh", h.RefreshJwtToken)
demo := v1.Group("/demo")
demo.GET("/hello", h.DemoHello) demo.GET("/hello", h.DemoHello)
demo.GET("/hello/:who", h.HelloWho) demo.GET("/hello/:who", h.HelloWho)
@ -61,14 +65,14 @@ func (h *Handler) Register(v1 *echo.Group) {
func (h *Handler) ReturnUnauthorizedResponse(c echo.Context, message string) error { func (h *Handler) ReturnUnauthorizedResponse(c echo.Context, message string) error {
return c.JSON(http.StatusUnauthorized, domain.ErrorResponse{ return c.JSON(http.StatusUnauthorized, domain.ErrorResponse{
HttpCode: http.StatusUnauthorized, Success: false,
Message: message, Message: message,
}) })
} }
func (h *Handler) InternalServerErrorResponse(c echo.Context, message string) error { func (h *Handler) InternalServerErrorResponse(c echo.Context, message string) error {
return c.JSON(http.StatusServiceUnavailable, domain.ErrorResponse{ return c.JSON(http.StatusServiceUnavailable, domain.ErrorResponse{
HttpCode: http.StatusInternalServerError, Success: false,
Message: message, Message: message,
}) })
} }