micro / go-micro

A Go microservices framework
https://go-micro.dev
Apache License 2.0
21.86k stars 2.35k forks source link

[go-micro/auth.Inspect()] Is this function working? #2674

Closed thuc201995 closed 2 months ago

thuc201995 commented 10 months ago

Please help!!!!! I don't know how to use it, It doesn't inspect anything from the token.

https://github.com/go-micro/go-micro/blob/7392705af8122a66d00d51582693d0cc05ce1eee/auth/auth.go#L35 https://github.com/go-micro/examples/blob/758a9e786e6ae78456bf54bcd56d6bf672534e30/auth/auth.go#L57C4-L57C4

jochumdev commented 10 months ago

@Davincible

hhniao commented 9 months ago

@thuc201995 this code maby helpful for you.

type JwtAuth struct {
    auth.Auth
}

func (a JwtAuth) Inspect(token string) (*auth.Account, error) {
    conf := config.Config()
    claims, err := Jwt{}.Validation(token, conf.JWT.Secret, false)
    if err != nil {
        return nil, err
    }

    id, _ := claims.GetSubject()
    return &auth.Account{
        ID: id,
    }, nil
}

func NewAuthWrapper(service micro.Service) server.HandlerWrapper {
    return func(h server.HandlerFunc) server.HandlerFunc {
        return func(ctx context.Context, req server.Request, rsp interface{}) error {
            ... the rest code
            a := service.Options().Auth
            acc, err := a.Inspect(token)
                        ... the rest code
        }
    }
}