Created
May 28, 2018 12:35
-
-
Save shijuvar/798b299f165cbbe31ad675890fb59644 to your computer and use it in GitHub Desktop.
UnaryServerInterceptor for authorization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Authorization unary interceptor function to handle authorize per RPC call | |
func serverInterceptor(ctx context.Context, | |
req interface{}, | |
info *grpc.UnaryServerInfo, | |
handler grpc.UnaryHandler) (interface{}, error) { | |
start := time.Now() | |
// Skip authorize when GetJWT is requested | |
if info.FullMethod != "/proto.EventStoreService/GetJWT" { | |
if err := authorize(ctx); err != nil { | |
return nil, err | |
} | |
} | |
// Calls the handler | |
h, err := handler(ctx, req) | |
// Logging with grpclog (grpclog.LoggerV2) | |
grpcLog.Infof("Request - Method:%s\tDuration:%s\tError:%v\n", | |
info.FullMethod, | |
time.Since(start), | |
err) | |
return h, err | |
} | |
// authorize function authorizes the token received from Metadata | |
func authorize(ctx context.Context) error { | |
md, ok := metadata.FromIncomingContext(ctx) | |
if !ok { | |
return status.Errorf(codes.InvalidArgument, "Retrieving metadata is failed") | |
} | |
authHeader, ok := md["authorization"] | |
if !ok { | |
return status.Errorf(codes.Unauthenticated, "Authorization token is not supplied") | |
} | |
token := authHeader[0] | |
// validateToken function validates the token | |
err := validateToken(token) | |
if err != nil { | |
return status.Errorf(codes.Unauthenticated, err.Error()) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment