2022-04-25 15:52:44 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2022-10-25 21:33:31 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-07-07 07:44:49 -07:00
|
|
|
package authtoken
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/auth/password"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/iam"
|
2020-08-19 16:47:18 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/kms"
|
2025-01-24 10:23:33 -08:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2020-07-07 07:44:49 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2022-09-13 20:29:01 -04:00
|
|
|
// TestAuthToken, despite its name, does more than just return an auth token; it
|
|
|
|
|
// also creates an auth method, an account, and a user and binds them together,
|
|
|
|
|
// then creates an auth token against it
|
2022-04-25 15:52:44 -04:00
|
|
|
func TestAuthToken(t testing.TB, conn *db.DB, kms *kms.Kms, scopeId string, opt ...Option) *AuthToken {
|
2020-07-07 07:44:49 -07:00
|
|
|
t.Helper()
|
2022-10-25 21:33:31 -04:00
|
|
|
|
|
|
|
|
opts := getOpts(opt...)
|
|
|
|
|
passwordOpts := password.GetOpts(opts.withPasswordOptions...)
|
|
|
|
|
loginName := passwordOpts.WithLoginName
|
|
|
|
|
if loginName == "" {
|
|
|
|
|
loginName = "name1"
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 11:43:03 -07:00
|
|
|
authMethod := password.TestAuthMethods(t, conn, scopeId, 1)[0]
|
2020-07-07 07:44:49 -07:00
|
|
|
// auth account is only used to join auth method to user.
|
|
|
|
|
// We don't do anything else with the auth account in the test setup.
|
2022-10-25 21:33:31 -04:00
|
|
|
acct := password.TestAccount(t, conn, authMethod.GetPublicId(), loginName, opts.withPasswordOptions...)
|
2020-07-07 07:44:49 -07:00
|
|
|
|
2020-07-31 11:43:03 -07:00
|
|
|
ctx := context.Background()
|
2020-07-07 07:44:49 -07:00
|
|
|
rw := db.New(conn)
|
2023-06-06 17:09:08 -04:00
|
|
|
iamRepo, err := iam.NewRepository(ctx, rw, rw, kms)
|
2020-07-31 11:43:03 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2022-10-25 21:33:31 -04:00
|
|
|
u := iam.TestUser(t, iamRepo, scopeId, append(opts.withIamOptions, iam.WithAccountIds(acct.PublicId))...)
|
2020-07-31 11:43:03 -07:00
|
|
|
|
2023-06-06 17:09:08 -04:00
|
|
|
repo, err := NewRepository(ctx, rw, rw, kms)
|
2020-07-23 09:53:37 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-07 07:44:49 -07:00
|
|
|
|
2021-04-08 20:54:32 -04:00
|
|
|
at, err := repo.CreateAuthToken(ctx, u, acct.GetPublicId(), opt...)
|
2020-07-23 09:53:37 -07:00
|
|
|
require.NoError(t, err)
|
2020-07-07 07:44:49 -07:00
|
|
|
return at
|
|
|
|
|
}
|
2025-01-24 10:23:33 -08:00
|
|
|
|
|
|
|
|
// TestRoleGrantsForToken contains information used by TestAuthTokenWithRoles to create
|
|
|
|
|
// roles and their associated grants (with grant scopes)
|
|
|
|
|
type TestRoleGrantsForToken struct {
|
|
|
|
|
RoleScopeID string
|
|
|
|
|
GrantStrings []string
|
|
|
|
|
GrantScopes []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestAuthTokenWithRoles creates auth token associated with roles as requested by the caller along
|
|
|
|
|
// with any required resources to achieve said token
|
|
|
|
|
func TestAuthTokenWithRoles(t testing.TB, conn *db.DB, kms *kms.Kms, scopeId string, roles []TestRoleGrantsForToken) *AuthToken {
|
|
|
|
|
t.Helper()
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
rw := db.New(conn)
|
|
|
|
|
atRepo, err := NewRepository(ctx, rw, rw, kms)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
iamRepo, err := iam.NewRepository(ctx, rw, rw, kms)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
authMethod := password.TestAuthMethods(t, conn, scopeId, 1)[0]
|
|
|
|
|
|
|
|
|
|
loginName, err := uuid.GenerateUUID()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
acct := password.TestAccount(t, conn, authMethod.GetPublicId(), loginName)
|
|
|
|
|
user := iam.TestUser(t, iamRepo, scopeId, iam.WithAccountIds(acct.GetPublicId()))
|
|
|
|
|
for _, r := range roles {
|
|
|
|
|
role := iam.TestRoleWithGrants(t, conn, r.RoleScopeID, r.GrantScopes, r.GrantStrings)
|
|
|
|
|
_ = iam.TestUserRole(t, conn, role.PublicId, user.PublicId)
|
|
|
|
|
}
|
|
|
|
|
fullGrantToken, err := atRepo.CreateAuthToken(ctx, user, acct.GetPublicId())
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
return fullGrantToken
|
|
|
|
|
}
|