Creating dynamic permissions within an account JWT in Go #5798
-
I have an account resolver that gets the account JWT by the public key. I'm trying to implement dynamic permissions so I don't have to update the user JWT. // URL account resolver looks up the account JWT by public key
akp, _ := nkeys.CreateAccount()
askp, _ := nkeys.CreateAccount()
ac := jwt.NewAccountClaims(publicKey)
ac.SigningKeys.Add(aspk)
ac.Tags.Add(fmt.Sprintf("org:%s", org))
ac.DefaultPermissions.Pub.Allow.Add(
"$JS.{{tag(org)}}.API.>", // dynamic tag permission not working here ❌
"$JS.hardcodedOrgID.API.>", // does work ✅ (issue with scoped key implementation?)
)
return ac.Encode(akp) The dynamic tag doesn't appear to work. I believe it needs to be a scoped signing key, but I'm still stumped after reading through the ac := jwt.NewAccountClaims(publicKey)
ac.Name = myacct
ac.SigningKeys.Add(aspk)
scope, _ := ac.SigningKeys.GetScope(aspk) // signing key's public key
if scope == nil {
scope = jwt.NewUserScope()
scope.(*jwt.UserScope).Key = aspk
scope.(*jwt.UserScope).Role = "myrole"
scope.(*jwt.UserScope).Template.Limits.Subs = -1
scope.(*jwt.UserScope).Template.Limits.Payload = -1
scope.(*jwt.UserScope).Template.Limits.Data = -1
scope.(*jwt.UserScope).Template.Permissions.Pub.Allow.Add(
"$JS.{{tag(org)}}.API.STREAM.UPDATE.>",
)
}
ac.SigningKeys.AddScopedSigner(scope) Am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I found a test which helped me debug this issue: nats-server/server/jwt_test.go Lines 4388 to 4398 in 60589da I found some limitations with the templating that I'll document here. The culprit of my issue was having a template function in the stream name. For example, my stream name was Line 469 in 89b042d Something else I noticed is that tag values result in lowercase values despite the original casing. I was using a ULID which is typically uppercase. Thanks! |
Beta Was this translation helpful? Give feedback.
-
@brettinternet scoped signing keys allow you to have additional macros on the templates associated with the signing key:
(*) Instead if you want to permission, you should be attaching to With that said, tags in JWT (account, user, etc) are lower-cased in JWT, so if you have some application id, this must be lower case in the tag. For the $JS.API you are using, you shouldn't use |
Beta Was this translation helpful? Give feedback.
I found a test which helped me debug this issue:
nats-server/server/jwt_test.go
Lines 4388 to 4398 in 60589da
I found some limitations with the templating that I'll document here.
The culprit of my issue was having a template…