Forward an anon user if auth is not required
continuous-integration/drone/tag Build is passing Details

master v0.3.9
René Jochum 2 years ago
parent 1b2a6b26f6
commit c09905378e
Signed by: jochum
GPG Key ID: F7D906F5E51E8E5E

@ -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). 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 ```go
import ( import (
@ -90,9 +90,8 @@ func main() {
service.Init( service.Init(
micro.Action(func(c *cli.Context) error { micro.Action(func(c *cli.Context) error {
s := service.Server() s := service.Server()
// Register with https://jochum.dev/jo-micro/router
r := router.NewHandler( r := router.NewHandler(
c.String("auth2_sqld_router_basepath"), "api/auth/v1",
router.NewRoute( router.NewRoute(
router.Method(router.MethodGet), router.Method(router.MethodGet),
router.Path("/"), router.Path("/"),

@ -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")) req := h.service.Client().NewRequest(serviceName, route.Endpoint, request, client.WithContentType("application/json"))
// Auth // Auth
u, err := h.routerAuth.Inspect(c.Request) u, authErr := h.routerAuth.Inspect(c.Request)
var ctx context.Context var (
if err != nil && authRequired { ctx context.Context
err error
)
if authErr != nil && authRequired {
c.JSON(http.StatusUnauthorized, gin.H{ c.JSON(http.StatusUnauthorized, gin.H{
"errors": []gin.H{ "errors": []gin.H{
gin.H{ gin.H{
@ -342,6 +345,18 @@ func (h *Handler) proxy(serviceName string, route *routerclientpb.RoutesReply_Ro
}) })
c.Abort() c.Abort()
return 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 { } else {
ctx, err = h.routerAuth.ForwardContext(u, c.Request, c) ctx, err = h.routerAuth.ForwardContext(u, c.Request, c)
if err != nil { 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 { for idx, l := range userRatelimiter {
context, err := l.Get(c, fmt.Sprintf("%s-%s-%s", path, l.Rate.Formatted, u.Id)) context, err := l.Get(c, fmt.Sprintf("%s-%s-%s", path, l.Rate.Formatted, u.Id))
if err != nil { if err != nil {

Loading…
Cancel
Save