feat(auth): Add /token endpoint to request a access token
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"git.secnex.io/secnex/oauth2-api/services"
|
||||
"git.secnex.io/secnex/oauth2-api/utils"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
@@ -15,15 +17,21 @@ type AuthorizeRequest struct {
|
||||
}
|
||||
|
||||
func AuthorizeController(c *fiber.Ctx) error {
|
||||
masterlog.Debug("Authorize request received", map[string]interface{}{"path": c.Path()})
|
||||
var request AuthorizeRequest
|
||||
if err := c.BodyParser(&request); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
||||
masterlog.Debug("Failed to parse request", map[string]interface{}{"error": err.Error()})
|
||||
return utils.NewHTTPResponse(fiber.StatusBadRequest, &fiber.Map{"error": err.Error()}, "", nil, nil).Send(c)
|
||||
}
|
||||
|
||||
if err := validator.New().Struct(request); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
||||
masterlog.Debug("Failed to validate request", map[string]interface{}{"error": err.Error()})
|
||||
return utils.NewHTTPResponse(fiber.StatusBadRequest, &fiber.Map{"error": err.Error()}, "", nil, nil).Send(c)
|
||||
}
|
||||
|
||||
masterlog.Debug("Authorize request validated", map[string]interface{}{"path": c.Path()})
|
||||
|
||||
response := services.Authorize(c.Locals("user").(string), request.ClientID, request.RedirectURI, request.ResponseType, request.Scope, request.State)
|
||||
masterlog.Debug("Authorize response sent", map[string]interface{}{"path": c.Path()})
|
||||
return response.Send(c)
|
||||
}
|
||||
|
||||
37
app/controllers/token.go
Normal file
37
app/controllers/token.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"git.secnex.io/secnex/oauth2-api/services"
|
||||
"git.secnex.io/secnex/oauth2-api/utils"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type TokenRequest struct {
|
||||
ClientID string `json:"client_id" validate:"required"`
|
||||
GrantType string `json:"grant_type" validate:"required"`
|
||||
Code string `json:"code" validate:"required"`
|
||||
RedirectURI string `json:"redirect_uri" validate:"required"`
|
||||
ClientSecret string `json:"client_secret" validate:"required"`
|
||||
}
|
||||
|
||||
func TokenController(c *fiber.Ctx) error {
|
||||
masterlog.Debug("Token request received", map[string]interface{}{"path": c.Path()})
|
||||
var request TokenRequest
|
||||
if err := c.BodyParser(&request); err != nil {
|
||||
masterlog.Debug("Failed to parse request", map[string]interface{}{"error": err.Error()})
|
||||
return utils.NewHTTPResponse(fiber.StatusBadRequest, &fiber.Map{"error": err.Error()}, "", nil, nil).Send(c)
|
||||
}
|
||||
|
||||
if err := validator.New().Struct(request); err != nil {
|
||||
masterlog.Debug("Failed to validate request", map[string]interface{}{"error": err.Error()})
|
||||
return utils.NewHTTPResponse(fiber.StatusBadRequest, &fiber.Map{"error": err.Error()}, "", nil, nil).Send(c)
|
||||
}
|
||||
|
||||
masterlog.Debug("Token request validated", map[string]interface{}{"path": c.Path()})
|
||||
|
||||
response := services.Token(request.ClientID, request.GrantType, request.Code, request.RedirectURI, request.ClientSecret)
|
||||
masterlog.Debug("Token response sent", map[string]interface{}{"path": c.Path()})
|
||||
return response.Send(c)
|
||||
}
|
||||
12
app/controllers/userinfo.go
Normal file
12
app/controllers/userinfo.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"git.secnex.io/secnex/oauth2-api/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func UserinfoController(c *fiber.Ctx) error {
|
||||
masterlog.Debug("Userinfo request received", map[string]interface{}{"path": c.Path()})
|
||||
return utils.NewHTTPResponse(fiber.StatusOK, &fiber.Map{"message": "Userinfo request received"}, "", nil, nil).Send(c)
|
||||
}
|
||||
Reference in New Issue
Block a user