2021-12-13 12:08:10 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-10-17 17:15:05 +02:00
|
|
|
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
|
2022-03-11 12:33:18 +01:00
|
|
|
|
2023-07-08 01:33:10 +02:00
|
|
|
"go.linka.cloud/grpc-toolkit/interceptors"
|
|
|
|
"go.linka.cloud/grpc-toolkit/interceptors/metadata"
|
2021-12-13 12:08:10 +01: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 12:33:18 +01:00
|
|
|
|
|
|
|
func NewBearerClientInterceptors(token string) interceptors.ClientInterceptors {
|
2022-07-16 16:43:00 +02:00
|
|
|
return metadata.NewInterceptors("authorization", "bearer "+token)
|
2022-03-11 12:33:18 +01:00
|
|
|
}
|