Skip to content

Instantly share code, notes, and snippets.

@asim
Created October 16, 2016 15:13
Show Gist options
  • Save asim/06c5999dcc4f92af263c8155994672e3 to your computer and use it in GitHub Desktop.
Save asim/06c5999dcc4f92af263c8155994672e3 to your computer and use it in GitHub Desktop.
func permWrapper(perms map[string][]string) server.HandlerWrapper {
return func(fn server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
md, ok := metadata.FromContext(ctx)
if !ok {
// just exec
return fn(ctx, req, rsp)
}
role := md["role"]
// check roles
if roles, ok := perms[req.Method()]; ok {
var authorised bool
for _, r := roles {
if r == role {
authorised = true
break
}
}
// is authorised?
if !authorised {
return errors.Forbidden("go.micro.auth.error", "unauthorised role")
}
}
// no perms for method
return fn(ctx, req, rsp)
}
}
}
@asim
Copy link
Author

asim commented Oct 16, 2016

w := permWrapper(map[string][]string{
  "Foo.Bar": []string{"admin"},
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment