* Change auth validity feedback Move from a bool indicating validity to lack of an error. This allows the error to be directly returned and typed. * Fix erroneous line
198 lines
5.7 KiB
Go
198 lines
5.7 KiB
Go
package authtokens
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/boundary/internal/auth"
|
|
"github.com/hashicorp/boundary/internal/authtoken"
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/authtokens"
|
|
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
|
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
const orgIdFieldName = "org_id"
|
|
|
|
var (
|
|
reInvalidID = regexp.MustCompile("[^A-Za-z0-9]")
|
|
)
|
|
|
|
// Service handles request as described by the pbs.AuthTokenServiceServer interface.
|
|
type Service struct {
|
|
repoFn func() (*authtoken.Repository, error)
|
|
}
|
|
|
|
// NewService returns a user service which handles user related requests to boundary.
|
|
func NewService(repo func() (*authtoken.Repository, error)) (Service, error) {
|
|
if repo == nil {
|
|
return Service{}, fmt.Errorf("nil iam repository provided")
|
|
}
|
|
return Service{repoFn: repo}, nil
|
|
}
|
|
|
|
var _ pbs.AuthTokenServiceServer = Service{}
|
|
|
|
// ListAuthTokens implements the interface pbs.AuthTokenServiceServer.
|
|
func (s Service) ListAuthTokens(ctx context.Context, req *pbs.ListAuthTokensRequest) (*pbs.ListAuthTokensResponse, error) {
|
|
authResults := auth.Verify(ctx)
|
|
if authResults.Error != nil {
|
|
return nil, authResults.Error
|
|
}
|
|
if err := validateListRequest(req); err != nil {
|
|
return nil, err
|
|
}
|
|
ul, err := s.listFromRepo(ctx, authResults.Scope.GetId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, item := range ul {
|
|
item.Scope = authResults.Scope
|
|
}
|
|
return &pbs.ListAuthTokensResponse{Items: ul}, nil
|
|
}
|
|
|
|
// GetAuthTokens implements the interface pbs.AuthTokenServiceServer.
|
|
func (s Service) GetAuthToken(ctx context.Context, req *pbs.GetAuthTokenRequest) (*pbs.GetAuthTokenResponse, error) {
|
|
authResults := auth.Verify(ctx)
|
|
if authResults.Error != nil {
|
|
return nil, authResults.Error
|
|
}
|
|
if err := validateGetRequest(req); err != nil {
|
|
return nil, err
|
|
}
|
|
u, err := s.getFromRepo(ctx, req.GetId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
u.Scope = authResults.Scope
|
|
return &pbs.GetAuthTokenResponse{Item: u}, nil
|
|
}
|
|
|
|
// DeleteAuthToken implements the interface pbs.AuthTokenServiceServer.
|
|
func (s Service) DeleteAuthToken(ctx context.Context, req *pbs.DeleteAuthTokenRequest) (*pbs.DeleteAuthTokenResponse, error) {
|
|
authResults := auth.Verify(ctx)
|
|
if authResults.Error != nil {
|
|
return nil, authResults.Error
|
|
}
|
|
if err := validateDeleteRequest(req); err != nil {
|
|
return nil, err
|
|
}
|
|
existed, err := s.deleteFromRepo(ctx, req.GetId())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pbs.DeleteAuthTokenResponse{Existed: existed}, nil
|
|
}
|
|
|
|
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthToken, error) {
|
|
repo, err := s.repoFn()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
u, err := repo.LookupAuthToken(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrRecordNotFound) {
|
|
return nil, handlers.NotFoundErrorf("AuthToken %q doesn't exist.", id)
|
|
}
|
|
return nil, err
|
|
}
|
|
if u == nil {
|
|
return nil, handlers.NotFoundErrorf("AuthToken %q doesn't exist.", id)
|
|
}
|
|
return toProto(u), nil
|
|
}
|
|
|
|
func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
|
|
repo, err := s.repoFn()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
rows, err := repo.DeleteAuthToken(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, db.ErrRecordNotFound) {
|
|
return false, nil
|
|
}
|
|
return false, status.Errorf(codes.Internal, "Unable to delete user: %v.", err)
|
|
}
|
|
return rows > 0, nil
|
|
}
|
|
|
|
func (s Service) listFromRepo(ctx context.Context, orgId string) ([]*pb.AuthToken, error) {
|
|
repo, err := s.repoFn()
|
|
_ = repo
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ul, err := repo.ListAuthTokens(ctx, orgId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var outUl []*pb.AuthToken
|
|
for _, u := range ul {
|
|
outUl = append(outUl, toProto(u))
|
|
}
|
|
return outUl, nil
|
|
}
|
|
|
|
func toProto(in *authtoken.AuthToken) *pb.AuthToken {
|
|
out := pb.AuthToken{
|
|
Id: in.GetPublicId(),
|
|
CreatedTime: in.GetCreateTime().GetTimestamp(),
|
|
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
|
|
ApproximateLastUsedTime: in.GetApproximateLastAccessTime().GetTimestamp(),
|
|
ExpirationTime: in.GetExpirationTime().GetTimestamp(),
|
|
UserId: in.GetIamUserId(),
|
|
AuthMethodId: in.GetAuthMethodId(),
|
|
}
|
|
return &out
|
|
}
|
|
|
|
// A validateX method should exist for each method above. These methods do not make calls to any backing service but enforce
|
|
// requirements on the structure of the request. They verify that:
|
|
// * The path passed in is correctly formatted
|
|
// * All required parameters are set
|
|
// * There are no conflicting parameters provided
|
|
func validateGetRequest(req *pbs.GetAuthTokenRequest) error {
|
|
badFields := map[string]string{}
|
|
if !validId(req.GetId(), authtoken.AuthTokenPrefix+"_") {
|
|
badFields["id"] = "Invalid formatted user id."
|
|
}
|
|
if len(badFields) > 0 {
|
|
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateDeleteRequest(req *pbs.DeleteAuthTokenRequest) error {
|
|
badFields := map[string]string{}
|
|
if !validId(req.GetId(), authtoken.AuthTokenPrefix+"_") {
|
|
badFields["id"] = "Incorrectly formatted identifier."
|
|
}
|
|
if len(badFields) > 0 {
|
|
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateListRequest(req *pbs.ListAuthTokensRequest) error {
|
|
badFields := map[string]string{}
|
|
if len(badFields) > 0 {
|
|
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validId(id, prefix string) bool {
|
|
if !strings.HasPrefix(id, prefix) {
|
|
return false
|
|
}
|
|
id = strings.TrimPrefix(id, prefix)
|
|
return !reInvalidID.Match([]byte(id))
|
|
}
|