mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 01:02:29 +00:00
add metadata interceptors, auth client interceptors
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
@ -8,13 +8,15 @@ import (
|
||||
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
||||
|
||||
"go.linka.cloud/grpc/errors"
|
||||
"go.linka.cloud/grpc/interceptors"
|
||||
"go.linka.cloud/grpc/interceptors/metadata"
|
||||
)
|
||||
|
||||
func BasicAuth(user, password string) string {
|
||||
return "basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+password))
|
||||
}
|
||||
|
||||
type BasicValidator func(ctx context.Context, user, password string) (context.Context,error)
|
||||
type BasicValidator func(ctx context.Context, user, password string) (context.Context, error)
|
||||
|
||||
func makeBasicAuthFunc(v BasicValidator) grpc_auth.AuthFunc {
|
||||
return func(ctx context.Context) (context.Context, error) {
|
||||
@ -34,3 +36,7 @@ func makeBasicAuthFunc(v BasicValidator) grpc_auth.AuthFunc {
|
||||
return v(ctx, cs[:s], cs[s+1:])
|
||||
}
|
||||
}
|
||||
|
||||
func NewBasicAuthClientIntereptors(user, password string) interceptors.ClientInterceptors {
|
||||
return metadata.NewInterceptors("authorization", BasicAuth(user, password))
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"strings"
|
||||
|
||||
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
||||
@ -40,8 +41,8 @@ func NewServerInterceptors(opts ...Option) interceptors.ServerInterceptors {
|
||||
return &interceptor{o: o, authFn: ChainedAuthFuncs(o.authFns...)}
|
||||
}
|
||||
|
||||
type interceptor struct{
|
||||
o options
|
||||
type interceptor struct {
|
||||
o options
|
||||
authFn grpc_auth.AuthFunc
|
||||
}
|
||||
|
||||
@ -92,3 +93,7 @@ func (i *interceptor) isNotProtected(endpoint string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func Equals(s1, s2 string) bool {
|
||||
return subtle.ConstantTimeCompare([]byte(s1), []byte(s2)) == 1
|
||||
}
|
||||
|
@ -102,11 +102,11 @@ func TestChainedAuthFuncs(t *testing.T) {
|
||||
code: codes.PermissionDenied,
|
||||
},
|
||||
{
|
||||
name: "internal error",
|
||||
auth: "bearer internal",
|
||||
name: "internal error",
|
||||
auth: "bearer internal",
|
||||
internalError: true,
|
||||
err: true,
|
||||
code: codes.PermissionDenied,
|
||||
err: true,
|
||||
code: codes.PermissionDenied,
|
||||
},
|
||||
{
|
||||
name: "multiple auth: first basic valid",
|
||||
|
@ -4,6 +4,9 @@ import (
|
||||
"context"
|
||||
|
||||
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
||||
|
||||
"go.linka.cloud/grpc/interceptors"
|
||||
"go.linka.cloud/grpc/interceptors/metadata"
|
||||
)
|
||||
|
||||
type TokenValidator func(ctx context.Context, token string) (context.Context, error)
|
||||
@ -17,3 +20,7 @@ func makeTokenAuthFunc(v TokenValidator) grpc_auth.AuthFunc {
|
||||
return v(ctx, a)
|
||||
}
|
||||
}
|
||||
|
||||
func NewBearerClientInterceptors(token string) interceptors.ClientInterceptors {
|
||||
return metadata.NewInterceptors("authorization", "Bearer "+token)
|
||||
}
|
||||
|
54
interceptors/metadata/metadata.go
Normal file
54
interceptors/metadata/metadata.go
Normal file
@ -0,0 +1,54 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
|
||||
"go.linka.cloud/grpc/interceptors"
|
||||
)
|
||||
|
||||
func NewInterceptors(pairs ...string) interceptors.Interceptors {
|
||||
return mdInterceptors{pairs: pairs}
|
||||
}
|
||||
|
||||
type mdInterceptors struct {
|
||||
pairs []string
|
||||
}
|
||||
|
||||
func (i mdInterceptors) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
|
||||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
if err := grpc.SetHeader(ctx, metadata.Pairs(i.pairs...)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler(ctx, req)
|
||||
}
|
||||
}
|
||||
|
||||
func (i mdInterceptors) StreamServerInterceptor() grpc.StreamServerInterceptor {
|
||||
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||
if err := grpc.SetHeader(ss.Context(), metadata.Pairs(i.pairs...)); err != nil {
|
||||
return err
|
||||
}
|
||||
return handler(srv, ss)
|
||||
}
|
||||
}
|
||||
|
||||
func (i mdInterceptors) UnaryClientInterceptor() grpc.UnaryClientInterceptor {
|
||||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||
if err := grpc.SetHeader(ctx, metadata.Pairs(i.pairs...)); err != nil {
|
||||
return err
|
||||
}
|
||||
return invoker(ctx, method, req, reply, cc, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
func (i mdInterceptors) StreamClientInterceptor() grpc.StreamClientInterceptor {
|
||||
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
||||
if err := grpc.SetHeader(ctx, metadata.Pairs(i.pairs...)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return streamer(ctx, desc, cc, method, opts...)
|
||||
}
|
||||
}
|
@ -30,6 +30,3 @@ func (i *recovery) UnaryClientInterceptor() grpc.UnaryClientInterceptor {
|
||||
func (i *recovery) StreamClientInterceptor() grpc.StreamClientInterceptor {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@ package sentry
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/johnbellone/grpc-middleware-sentry"
|
||||
grpc_sentry "github.com/johnbellone/grpc-middleware-sentry"
|
||||
|
||||
"go.linka.cloud/grpc/interceptors"
|
||||
)
|
||||
@ -31,6 +31,3 @@ func (i *interceptor) UnaryClientInterceptor() grpc.UnaryClientInterceptor {
|
||||
func (i *interceptor) StreamClientInterceptor() grpc.StreamClientInterceptor {
|
||||
return grpc_sentry.StreamClientInterceptor(i.opts...)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user