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).
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("/"),

@ -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 {

Loading…
Cancel
Save