Skip to content

Commit 1f3ff32

Browse files
authored
[FEAT] added a description to a scope role to enable tools discern between same name roles (#220)
1 parent c2d30e2 commit 1f3ff32

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

v2/signingkeys.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 The NATS Authors
2+
* Copyright 2020-2024 The NATS Authors
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -67,10 +67,11 @@ func (t *ScopeType) UnmarshalJSON(b []byte) error {
6767
}
6868

6969
type UserScope struct {
70-
Kind ScopeType `json:"kind"`
71-
Key string `json:"key"`
72-
Role string `json:"role"`
73-
Template UserPermissionLimits `json:"template"`
70+
Kind ScopeType `json:"kind"`
71+
Key string `json:"key"`
72+
Role string `json:"role"`
73+
Template UserPermissionLimits `json:"template"`
74+
Description string `json:"description"`
7475
}
7576

7677
func NewUserScope() *UserScope {

v2/signingkeys_test.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 The NATS Authors
2+
* Copyright 2020-2024 The NATS Authors
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -251,6 +251,56 @@ func TestGetKeys(t *testing.T) {
251251
}
252252
}
253253

254+
func TestScopeJSON(t *testing.T) {
255+
ac, apk := makeAccount(t, nil, nil)
256+
pk := publicKey(createAccountNKey(t), t)
257+
us := NewUserScope()
258+
us.Key = pk
259+
us.Role = "Admin"
260+
us.Description = "Admin Key"
261+
us.Template = UserPermissionLimits{
262+
Permissions: Permissions{
263+
Pub: Permission{Allow: []string{"foo"}},
264+
},
265+
}
266+
ac.SigningKeys.AddScopedSigner(us)
267+
268+
token, err := ac.Encode(apk)
269+
if err != nil {
270+
t.Fatal(err)
271+
}
272+
273+
ac, err = DecodeAccountClaims(token)
274+
if err != nil {
275+
t.Fatal(err)
276+
}
277+
278+
s, ok := ac.SigningKeys.GetScope(pk)
279+
if !ok {
280+
t.Fatal("expected to find a scope admin")
281+
}
282+
us, ok = s.(*UserScope)
283+
if !ok {
284+
t.Fatal("expected to find an user scope")
285+
}
286+
287+
if us.Key != pk {
288+
t.Fatal("expected public key to match")
289+
}
290+
291+
if !us.Template.Permissions.Pub.Allow.Contains("foo") {
292+
t.Fatal("expected permissions to contain foo")
293+
}
294+
295+
if us.Description != "Admin Key" {
296+
t.Fatal("expected description to match")
297+
}
298+
299+
if us.Role != "Admin" {
300+
t.Fatal("expected role to match")
301+
}
302+
}
303+
254304
func TestJson(t *testing.T) {
255305
ac, apk := makeAccount(t, nil, nil)
256306
ac.SigningKeys.Add(publicKey(createAccountNKey(t), t))
@@ -278,5 +328,4 @@ func TestJson(t *testing.T) {
278328
if len(myAcc.SigningKeys) != 3 {
279329
t.Fatalf("Expected 3 signing keys got: %d", len(myAcc.SigningKeys))
280330
}
281-
282331
}

0 commit comments

Comments
 (0)