github.com/jfrog/jfrog-cli-core/v2@v2.52.0/general/token/accesstokencreate.go (about)

     1  package token
     2  
     3  import (
     4  	"encoding/json"
     5  	rtUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
     6  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
     7  	"github.com/jfrog/jfrog-client-go/access/services"
     8  	"github.com/jfrog/jfrog-client-go/auth"
     9  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    10  	"strings"
    11  )
    12  
    13  const (
    14  	AdminScope        = "applied-permissions/admin"
    15  	GroupsScopePrefix = "applied-permissions/groups:"
    16  )
    17  
    18  type AccessTokenCreateCommand struct {
    19  	serverDetails *config.ServerDetails
    20  	username      string
    21  	projectKey    string
    22  
    23  	scope      string
    24  	groups     string
    25  	grantAdmin bool
    26  
    27  	expiry      *uint
    28  	refreshable bool
    29  	description string
    30  
    31  	audience              string
    32  	includeReferenceToken bool
    33  
    34  	response *auth.CreateTokenResponseData
    35  }
    36  
    37  func NewAccessTokenCreateCommand() *AccessTokenCreateCommand {
    38  	return &AccessTokenCreateCommand{response: new(auth.CreateTokenResponseData)}
    39  }
    40  
    41  func (atc *AccessTokenCreateCommand) SetServerDetails(serverDetails *config.ServerDetails) *AccessTokenCreateCommand {
    42  	atc.serverDetails = serverDetails
    43  	return atc
    44  }
    45  
    46  func (atc *AccessTokenCreateCommand) SetUsername(username string) *AccessTokenCreateCommand {
    47  	atc.username = username
    48  	return atc
    49  }
    50  
    51  func (atc *AccessTokenCreateCommand) SetProjectKey(projectKey string) *AccessTokenCreateCommand {
    52  	atc.projectKey = projectKey
    53  	return atc
    54  }
    55  
    56  func (atc *AccessTokenCreateCommand) SetGroups(groups string) *AccessTokenCreateCommand {
    57  	atc.groups = groups
    58  	return atc
    59  }
    60  
    61  func (atc *AccessTokenCreateCommand) SetScope(scope string) *AccessTokenCreateCommand {
    62  	atc.scope = scope
    63  	return atc
    64  }
    65  
    66  func (atc *AccessTokenCreateCommand) SetGrantAdmin(grantAdmin bool) *AccessTokenCreateCommand {
    67  	atc.grantAdmin = grantAdmin
    68  	return atc
    69  }
    70  
    71  func (atc *AccessTokenCreateCommand) SetExpiry(expiry *uint) *AccessTokenCreateCommand {
    72  	atc.expiry = expiry
    73  	return atc
    74  }
    75  
    76  func (atc *AccessTokenCreateCommand) SetRefreshable(refreshable bool) *AccessTokenCreateCommand {
    77  	atc.refreshable = refreshable
    78  	return atc
    79  }
    80  
    81  func (atc *AccessTokenCreateCommand) SetDescription(description string) *AccessTokenCreateCommand {
    82  	atc.description = description
    83  	return atc
    84  }
    85  
    86  func (atc *AccessTokenCreateCommand) SetAudience(audience string) *AccessTokenCreateCommand {
    87  	atc.audience = audience
    88  	return atc
    89  }
    90  
    91  func (atc *AccessTokenCreateCommand) SetIncludeReferenceToken(includeReferenceToken bool) *AccessTokenCreateCommand {
    92  	atc.includeReferenceToken = includeReferenceToken
    93  	return atc
    94  }
    95  
    96  func (atc *AccessTokenCreateCommand) Response() ([]byte, error) {
    97  	content, err := json.Marshal(*atc.response)
    98  	return content, errorutils.CheckError(err)
    99  }
   100  
   101  func (atc *AccessTokenCreateCommand) ServerDetails() (*config.ServerDetails, error) {
   102  	return atc.serverDetails, nil
   103  }
   104  
   105  func (atc *AccessTokenCreateCommand) CommandName() string {
   106  	return "jf_access_token_create"
   107  }
   108  
   109  func (atc *AccessTokenCreateCommand) Run() error {
   110  	servicesManager, err := rtUtils.CreateAccessServiceManager(atc.serverDetails, false)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	*atc.response, err = servicesManager.CreateAccessToken(atc.getTokenParams())
   116  	return err
   117  }
   118  
   119  func (atc *AccessTokenCreateCommand) getTokenParams() services.CreateTokenParams {
   120  	tokenParams := services.CreateTokenParams{}
   121  
   122  	tokenParams.Username = strings.ToLower(atc.username)
   123  	tokenParams.ProjectKey = atc.projectKey
   124  	tokenParams.Scope = atc.getScope()
   125  	tokenParams.ExpiresIn = atc.expiry
   126  	tokenParams.Refreshable = &atc.refreshable
   127  	tokenParams.Description = atc.description
   128  	tokenParams.Audience = atc.audience
   129  	tokenParams.IncludeReferenceToken = &atc.includeReferenceToken
   130  	return tokenParams
   131  }
   132  
   133  // If an explicit scope was provided, apply it.
   134  // Otherwise, if admin or groups scopes were requested, construct scope from them (space separated).
   135  // If no scopes were requested, leave scope empty to provide the default user scope.
   136  func (atc *AccessTokenCreateCommand) getScope() string {
   137  	if atc.scope != "" {
   138  		return atc.scope
   139  	}
   140  
   141  	var scopes []string
   142  	if atc.groups != "" {
   143  		scopes = append(scopes, GroupsScopePrefix+atc.groups)
   144  	}
   145  
   146  	if atc.grantAdmin {
   147  		scopes = append(scopes, AdminScope)
   148  	}
   149  	return strings.Join(scopes, " ")
   150  }