Skip to content

Instantly share code, notes, and snippets.

@vladfr
Created June 5, 2021 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladfr/4c4053f0ed32cfdfc0bc742e555fad94 to your computer and use it in GitHub Desktop.
Save vladfr/4c4053f0ed32cfdfc0bc742e555fad94 to your computer and use it in GitHub Desktop.
func (a *IDPConfig) AuthUnaryInterceptor(authFunc AuthFunc) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
meta, ok := metadata.FromIncomingContext(ctx)
// [...]
token, err := a.validateToken(ctx, rawToken)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "invalid auth token: %v", err)
}
// put stuff in the ctx for use in the override methods
userMeta, err := NewUserAccessMetadataFromToken(token)
if err != nil {
return nil, err
}
// call the given authFunc, or overrides, if the server implements ServiceAuthFuncOverride
newCtx := NewContext(ctx, userMeta)
var errF error
if overrideSrv, ok := info.Server.(ServiceAuthFuncOverride); ok {
// THIS IS WHERE I FAIL:
authFunc = overrideSrv.AuthFuncOverride(newCtx, info.FullMethod)
}
if authFunc != nil {
newCtx, errF = authFunc(newCtx)
}
if errF != nil {
return nil, errF
}
return handler(newCtx, req)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment