Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for external authorization in accounts #54

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,43 @@ func (a *AccountData) Limits() AccountLimits {
return &accountLimits{data: a}
}

func (a *AccountData) SetExternalAuthorizationUser(users []User, accounts []Account, encryption string) error {
if users == nil {
// disable
a.Claim.Authorization.AuthUsers = nil
a.Claim.Authorization.AllowedAccounts = nil
a.Claim.Authorization.XKey = ""
} else {
var ukeys []string
for _, u := range users {
ukeys = append(ukeys, u.Subject())
}
a.Claim.Authorization.AuthUsers = ukeys

var akeys []string
for _, a := range accounts {
akeys = append(akeys, a.Subject())
}
a.Claim.Authorization.AllowedAccounts = akeys

if encryption != "" {
key, err := KeyFrom(encryption, nkeys.PrefixByteCurve)
if err != nil {
return err
}
a.Claim.Authorization.XKey = key.Public
} else {
a.Claim.Authorization.XKey = ""
}
}
return a.update()
}

func (a *AccountData) ExternalAuthorization() ([]string, []string, string) {
config := a.Claim.Authorization
return config.AuthUsers, config.AllowedAccounts, config.XKey
}

type exports struct {
*AccountData
}
Expand Down
51 changes: 51 additions & 0 deletions tests/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"fmt"
"github.com/nats-io/nkeys"
"time"

"github.com/nats-io/jwt/v2"
Expand Down Expand Up @@ -871,3 +872,53 @@ func (t *ProviderSuite) Test_TracingContext() {
t.NoError(a.SetTracingContext(nil))
t.Nil(a.GetTracingContext())
}

func (t *ProviderSuite) Test_ExternalAuthorization() {
auth, err := authb.NewAuth(t.Provider)
t.NoError(err)

a := t.MaybeCreate(auth, "O", "A")
t.NotNil(a)

external := t.MaybeCreate(auth, "O", "AUTH")
t.NotNil(external)
u, err := external.Users().Add("service", external.Subject())
t.NoError(err)

curve, err := authb.KeyFor(nkeys.PrefixByteCurve)
t.NoError(err)

users, accounts, key := external.ExternalAuthorization()
t.Empty(users)
t.Empty(accounts)
t.Empty(key)

t.NoError(external.SetExternalAuthorizationUser([]authb.User{u}, []authb.Account{a}, curve.Public))

t.NoError(auth.Commit())
t.NoError(auth.Reload())

external = t.GetAccount(auth, "O", "AUTH")
t.NotNil(external)

users, accounts, key = external.ExternalAuthorization()
t.Equal(users, []string{u.Subject()})
t.Equal(accounts, []string{a.Subject()})
t.Equal(key, curve.Public)

t.NoError(external.SetExternalAuthorizationUser(nil, nil, ""))
users, accounts, key = external.ExternalAuthorization()
t.Nil(users)
t.Nil(accounts)
t.Empty(key)

t.NoError(auth.Commit())
t.NoError(auth.Reload())

external = t.GetAccount(auth, "O", "AUTH")
t.NotNil(external)
users, accounts, key = external.ExternalAuthorization()
t.Nil(users)
t.Nil(accounts)
t.Empty(key)
}
8 changes: 8 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ type Account interface {
SetTracingContext(opts *TracingContext) error
// Tags returns an object that you can use to manage tags for the account
Tags() Tags

// SetExternalAuthorizationUser updates external authorization by associating users public keys, account public keys, and an encryption key.
// ExternalAuthorization requires at the very list one user
SetExternalAuthorizationUser(users []User, accounts []Account, encryption string) error

// ExternalAuthorization retrieves a list of authorized users, associated accounts, and encryption key.
// if the users value is nil, ExternalAuthorization is not enabled
ExternalAuthorization() ([]string, []string, string)
}

// Users is an interface for managing users
Expand Down
Loading