kit icon indicating copy to clipboard operation
kit copied to clipboard

auth/casbin readme and example

Open abemedia opened this issue 4 years ago • 1 comments

It's not clear to me how auth/casbin is meant to be used. I had a look at the test cases but it looks like that's just using hard-coded values. Are we meant to wrap this with our own middleware that injects the subject & object and populates the context with the model & policy or is this just meant to serve as an example implementation?

Would be good to have a readme to add a little more context...

abemedia avatar Apr 04 '20 17:04 abemedia

@abeMedia I'm not familar with go-kit, but I guess it could be used like this

uppercaseHandlerWithMid := httptransport.NewServer(
		func(ctx context.Context, request interface{}) (response interface{}, err error) {
			ctx = context.WithValue(ctx, casbin.CasbinModelContextKey, "/path/to/model")
			ctx = context.WithValue(ctx, casbin.CasbinPolicyContextKey, "/path/to/policy")
			return casbin.NewEnforcer("alice", "data1", "read")(makeUppercaseEndpoint(svc))(ctx, request)
		}, decodeUppercaseRequest, encodeResponse, httptransport.ServerBefore(httptransport.PopulateRequestContext),
	)
	http.Handle("/uppercasem", uppercaseHandlerWithMid)
	http.Handle("/uppercase", uppercaseHandler)
	http.Handle("/count", countHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))

You could find out model and policy pattern from casbin

This isn't that elegent, request params could be parsed from request

kilosonc avatar Mar 29 '21 07:03 kilosonc