diff --git a/README.md b/README.md index cf74091..8baa2f6 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ See [cmd/microrouterd/plugins.go](cmd/microrouterd/plugins.go) for a list of ava Have a look at [internalService](cmd/microrouterd/main.go#L51 ), [jo-micro/auth2](https://git.jochum.dev/jo-micro/auth2/src/branch/main/cmd/microauth2sqld/main.go#L319) or the author's FOSS project [microlobby](https://github.com/pcdummy/microlobby). -Here's some code from the microlobby project +Here's some code from the jo-micro/auth2 project ```go import ( @@ -90,9 +90,8 @@ func main() { service.Init( micro.Action(func(c *cli.Context) error { s := service.Server() - // Register with https://jochum.dev/jo-micro/router r := router.NewHandler( - c.String("auth2_sqld_router_basepath"), + "api/auth/v1", router.NewRoute( router.Method(router.MethodGet), router.Path("/"), diff --git a/cmd/microrouterd/handler/handler.go b/cmd/microrouterd/handler/handler.go index f9584c6..70ea363 100644 --- a/cmd/microrouterd/handler/handler.go +++ b/cmd/microrouterd/handler/handler.go @@ -329,9 +329,12 @@ func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Ro req := h.service.Client().NewRequest(serviceName, route.Endpoint, request, client.WithContentType("application/json")) // Auth - u, err := h.routerAuth.Inspect(c.Request) - var ctx context.Context - if err != nil && authRequired { + u, authErr := h.routerAuth.Inspect(c.Request) + var ( + ctx context.Context + err error + ) + if authErr != nil && authRequired { c.JSON(http.StatusUnauthorized, gin.H{ "errors": []gin.H{ gin.H{ @@ -342,6 +345,18 @@ func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Ro }) c.Abort() return + } else if authErr != nil { + ctx, err = h.routerAuth.ForwardContext(auth2.AnonUser, c.Request, c) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "errors": []gin.H{ + gin.H{ + "id": "INTERNAL_SERVER_ERROR", + "message": err, + }, + }, + }) + } } else { ctx, err = h.routerAuth.ForwardContext(u, c.Request, c) if err != nil { @@ -356,7 +371,7 @@ func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Ro } } - if len(userRatelimiter) > 0 { + if authErr == nil && len(userRatelimiter) > 0 { for idx, l := range userRatelimiter { context, err := l.Get(c, fmt.Sprintf("%s-%s-%s", path, l.Rate.Formatted, u.Id)) if err != nil {