2021-12-13 11:08:10 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
2022-03-11 11:33:18 +00:00
|
|
|
|
|
|
|
"go.linka.cloud/grpc/interceptors"
|
|
|
|
"go.linka.cloud/grpc/interceptors/metadata"
|
2021-12-13 11:08:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TokenValidator func(ctx context.Context, token string) (context.Context, error)
|
|
|
|
|
|
|
|
func makeTokenAuthFunc(v TokenValidator) grpc_auth.AuthFunc {
|
|
|
|
return func(ctx context.Context) (context.Context, error) {
|
|
|
|
a, err := grpc_auth.AuthFromMD(ctx, "bearer")
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
return v(ctx, a)
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 11:33:18 +00:00
|
|
|
|
|
|
|
func NewBearerClientInterceptors(token string) interceptors.ClientInterceptors {
|
|
|
|
return metadata.NewInterceptors("authorization", "Bearer "+token)
|
|
|
|
}
|