Skip to content

Commit a7c6b0b

Browse files
committed
refactor: overhaul token management system with optimized architecture
- Replace api_token_manager.go with new modular token services - Add TokenController for centralized token endpoint handling - Implement optimized BrankaToken service with comprehensive tests - Add ProjectTokenManager for improved project-level token management - Streamline authentication controller by removing redundant token logic - Enhance GraphQL resolver with simplified token mutation handling - Update models for better token structure and validation - Refactor router configuration for new token endpoints - Clean up system schema definitions and object models - Minor fixes in CORS middleware and Dockerfile optimization This refactor significantly reduces code complexity (764 deletions, 195 insertions) while improving token management performance and maintainability.
1 parent 18b3741 commit a7c6b0b

20 files changed

+1586
-423
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ WORKDIR /app
5959
COPY --from=builder /build/engine .
6060

6161
# Copy required directories with proper ownership
62-
COPY --chown=apito:apito plugins ./plugins/
6362
COPY --chown=apito:apito keys ./keys/
6463

6564
# Make binary executable

controller/auth_controller.go

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
ae "github.com/apito-io/engine/err"
1212
"github.com/apito-io/engine/models"
1313
"github.com/apito-io/engine/resolver"
14-
"github.com/apito-io/engine/services"
1514
"github.com/apito-io/engine/utility"
1615
"github.com/getsentry/sentry-go"
1716
"github.com/ilyakaznacheev/cleanenv"
@@ -248,135 +247,6 @@ func (a *authCtrl) errorHandler(router echo.Context, response *models.HttpRespon
248247
}
249248

250249

251-
func (a *authCtrl) GenerateAPIKey(c echo.Context) error {
252-
253-
var req map[string]interface{}
254-
if err := c.Bind(&req); err != nil {
255-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
256-
Message: err.Error(),
257-
Code: http.StatusBadRequest,
258-
})
259-
}
260-
261-
var tenantID string
262-
if _, ok := req["tenant_id"]; ok {
263-
tenantID = req["tenant_id"].(string)
264-
}
265-
266-
userID := c.Get("user")
267-
if userID == nil {
268-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
269-
Message: "user is missing in the token payload",
270-
Code: http.StatusBadRequest,
271-
})
272-
}
273-
274-
projectID := c.Get("project_id")
275-
if projectID == nil {
276-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
277-
Message: "You have to switch to a project in order for this to work",
278-
Code: http.StatusBadRequest,
279-
})
280-
}
281-
282-
ctx := c.Request().Context()
283-
284-
t := services.GetBrankaToken(a.Cfg, a.graphQLServer.SystemDriver)
285-
286-
apiKey, err := t.GenerateAPIKey(ctx, userID.(string), projectID.(string), tenantID, "api_key", time.Now().Add(time.Hour*24*30).Unix())
287-
if err != nil {
288-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
289-
Message: err.Error(),
290-
Code: http.StatusBadRequest,
291-
})
292-
}
293-
294-
return c.JSON(http.StatusOK, &models.HttpResponse{
295-
Token: *apiKey,
296-
Code: http.StatusOK,
297-
})
298-
}
299-
300-
func (a *authCtrl) SyncProject(c echo.Context) error {
301-
302-
type SyncProjectRequest struct {
303-
Token string `json:"token"`
304-
Project *models.Project `json:"project"`
305-
}
306-
307-
var req SyncProjectRequest
308-
if err := c.Bind(&req); err != nil {
309-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
310-
Message: "Bad boy, Jason ...",
311-
Code: http.StatusBadRequest,
312-
})
313-
}
314-
315-
if req.Token == "" {
316-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
317-
Message: "token is missing",
318-
Code: http.StatusBadRequest,
319-
})
320-
}
321-
322-
if req.Project == nil {
323-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
324-
Message: "project is missing",
325-
Code: http.StatusBadRequest,
326-
})
327-
}
328-
329-
if req.Project.Schema == nil {
330-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
331-
Message: "project does not contain any model. Nothing to sync",
332-
Code: http.StatusBadRequest,
333-
})
334-
}
335-
336-
userId := c.Get("user")
337-
if userId == nil {
338-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
339-
Message: "user is missing in the token payload",
340-
Code: http.StatusBadRequest,
341-
})
342-
}
343-
344-
ctx := c.Request().Context()
345-
346-
t := services.GetBrankaToken(a.Cfg, a.graphQLServer.SystemDriver)
347-
348-
decodedToken, err := t.Validate(ctx, req.Token)
349-
if err != nil {
350-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
351-
Message: err.Error(),
352-
Code: http.StatusBadRequest,
353-
})
354-
}
355-
356-
if decodedToken.UserID != userId {
357-
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
358-
Message: "token is invalid",
359-
Code: http.StatusBadRequest,
360-
})
361-
}
362-
363-
if req.Project.SyncedProperty != nil {
364-
// check
365-
} else {
366-
req.Project.SyncedProperty = &models.SyncProject{
367-
ProjectID: req.Project.ID,
368-
SyncedTokenUsed: req.Token,
369-
LocalProjectID: req.Project.ID,
370-
LastSyncedAt: utility.GetCurrentTime(),
371-
}
372-
}
373-
374-
return c.JSON(http.StatusOK, &models.HttpResponse{
375-
Body: req.Project.SyncedProperty,
376-
Code: http.StatusOK,
377-
})
378-
}
379-
380250
func (a *authCtrl) ProjectSwitchV2(c echo.Context) error {
381251
var req *models.ProjectCreateRequest
382252
if err := c.Bind(&req); err != nil {

controller/graph_controller.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,13 @@ func (g *GraphCtrl) PublicGraphQL(i echo.Context) error {
477477
})
478478
}
479479

480+
if req.Query == "" {
481+
return i.JSON(http.StatusBadRequest, &models.HttpResponse{
482+
Message: "Query can not be empty!",
483+
Code: http.StatusBadRequest,
484+
})
485+
}
486+
480487
/*fmt.Println(fmt.Sprintf("req %s", req.Query))
481488
_var, _ := json.Marshal(req.Variables)
482489
fmt.Println(fmt.Sprintf("variable %s", string(_var)))*/
@@ -652,6 +659,30 @@ func (g *GraphCtrl) GetSystemCacheInfo(i echo.Context) error {
652659
})
653660
}
654661

662+
func (g *GraphCtrl) SystemHealth(c echo.Context) error {
663+
664+
userId := c.Get("user")
665+
if userId == nil {
666+
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
667+
Message: "user is missing in the token payload",
668+
Code: http.StatusBadRequest,
669+
})
670+
}
671+
672+
if userId.(string) == "" {
673+
return c.JSON(http.StatusBadRequest, &models.HttpResponse{
674+
Message: "system health check is only allowed for authenticated users",
675+
Code: http.StatusBadRequest,
676+
})
677+
}
678+
679+
return c.JSON(http.StatusOK, map[string]interface{}{
680+
"success": true,
681+
"message": "System is healthy",
682+
"version": "v2.0.0",
683+
})
684+
}
685+
655686
func (g *GraphCtrl) SystemGraphQL(i echo.Context) error {
656687

657688
var req models.GraphQLIncomingRequest

0 commit comments

Comments
 (0)