Skip to content

Commit

Permalink
Support access tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Dec 16, 2018
1 parent 6a0910d commit af471e7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The library can be used as a go-module, which should be added to your project's
rtDetails.SetApiKey("apikey")
rtDetails.SetUser("user")
rtDetails.SetPassword("password")
rtDetails.SetAccessToken("accesstoken")
```

### Creating a Service Manager
Expand Down Expand Up @@ -449,6 +450,7 @@ Optional flags:
| `-rt.apikey` | [Optional] Artifactory API key. |
| `-rt.sshKeyPath` | [Optional] Ssh key file path. Should be used only if the Artifactory URL format is ssh://[domain]:port |
| `-rt.sshPassphrase` | [Optional] Ssh key passphrase. |
| `-rt.accessToken` | [Optional] Artifactory access token. |
| `-log-level` | [Default: INFO] Sets the log level. |


Expand Down
20 changes: 16 additions & 4 deletions artifactory/auth/rtdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ type ArtifactoryDetails interface {
GetUser() string
GetPassword() string
GetApiKey() string
GetAccessToken() string
GetSshAuthHeaders() map[string]string
GetVersion() (string, error)
SetUrl(url string)
SetUser(user string)
SetPassword(password string)
SetApiKey(apiKey string)
SetAccessToken(accessToken string)
SetSshAuthHeaders(sshAuthHeaders map[string]string)

AuthenticateSsh(sshKey, sshPassphrase string) error
Expand All @@ -39,6 +41,7 @@ type artifactoryDetails struct {
User string `json:"-"`
Password string `json:"-"`
ApiKey string `json:"-"`
AccessToken string `json:"-"`
version string `json:"-"`
SshAuthHeaders map[string]string `json:"-"`
}
Expand All @@ -59,6 +62,10 @@ func (rt *artifactoryDetails) GetApiKey() string {
return rt.ApiKey
}

func (rt *artifactoryDetails) GetAccessToken() string {
return rt.AccessToken
}

func (rt *artifactoryDetails) GetSshAuthHeaders() map[string]string {
return rt.SshAuthHeaders
}
Expand All @@ -79,6 +86,10 @@ func (rt *artifactoryDetails) SetApiKey(apiKey string) {
rt.ApiKey = apiKey
}

func (rt *artifactoryDetails) SetAccessToken(accessToken string) {
rt.AccessToken = accessToken
}

func (rt *artifactoryDetails) SetSshAuthHeaders(sshAuthHeaders map[string]string) {
rt.SshAuthHeaders = sshAuthHeaders
}
Expand All @@ -95,10 +106,11 @@ func (rt *artifactoryDetails) AuthenticateSsh(sshKeyPath, sshPassphrase string)

func (rt *artifactoryDetails) CreateHttpClientDetails() httputils.HttpClientDetails {
return httputils.HttpClientDetails{
User: rt.User,
Password: rt.Password,
ApiKey: rt.ApiKey,
Headers: utils.CopyMap(rt.SshAuthHeaders)}
User: rt.User,
Password: rt.Password,
ApiKey: rt.ApiKey,
AccessToken: rt.AccessToken,
Headers: utils.CopyMap(rt.SshAuthHeaders)}
}

func (rt *artifactoryDetails) GetVersion() (string, error) {
Expand Down
3 changes: 1 addition & 2 deletions artifactory/services/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/httpclient"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/http"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (ps *PingService) Ping() ([]byte, error) {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(respBody)))
return respBody, errorutils.CheckError(errors.New("Artifactory response: " + resp.Status))
}
log.Debug("Artifactory response: ", resp.Status)
return respBody, nil
Expand Down
12 changes: 11 additions & 1 deletion httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,17 @@ func setAuthentication(req *http.Request, httpClientsDetails httputils.HttpClien
} else {
req.Header.Set("X-JFrog-Art-Api", httpClientsDetails.ApiKey)
}
} else if httpClientsDetails.Password != "" {
return
}
if httpClientsDetails.AccessToken != "" {
if httpClientsDetails.User != "" {
req.SetBasicAuth(httpClientsDetails.User, httpClientsDetails.AccessToken)
} else {
req.Header.Set("Authorization", "Bearer "+httpClientsDetails.AccessToken)
}
return
}
if httpClientsDetails.Password != "" {
req.SetBasicAuth(httpClientsDetails.User, httpClientsDetails.Password)
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var RtPassword *string
var RtApiKey *string
var RtSshKeyPath *string
var RtSshPassphrase *string
var RtAccessToken *string
var LogLevel *string
var testsUploadService *services.UploadService
var testsSearchService *services.SearchService
Expand All @@ -43,6 +44,7 @@ func init() {
RtApiKey = flag.String("rt.apikey", "", "Artifactory user API key")
RtSshKeyPath = flag.String("rt.sshKeyPath", "", "Ssh key file path")
RtSshPassphrase = flag.String("rt.sshPassphrase", "", "Ssh key passphrase")
RtAccessToken = flag.String("rt.accessToken", "", "Artifactory access token")
LogLevel = flag.String("log-level", "INFO", "Sets the log level")
}

Expand Down Expand Up @@ -74,6 +76,8 @@ func getArtDetails() auth.ArtifactoryDetails {
if !fileutils.IsSshUrl(rtDetails.GetUrl()) {
if *RtApiKey != "" {
rtDetails.SetApiKey(*RtApiKey)
} else if *RtAccessToken != "" {
rtDetails.SetAccessToken(*RtAccessToken)
} else {
rtDetails.SetUser(*RtUser)
rtDetails.SetPassword(*RtPassword)
Expand Down
20 changes: 11 additions & 9 deletions utils/io/httputils/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import (
)

type HttpClientDetails struct {
User string
Password string
ApiKey string
Headers map[string]string
Transport *http.Transport
User string
Password string
ApiKey string
AccessToken string
Headers map[string]string
Transport *http.Transport
}

func (httpClientDetails HttpClientDetails) Clone() *HttpClientDetails {
headers := make(map[string]string)
utils.MergeMaps(httpClientDetails.Headers, headers)
return &HttpClientDetails{
User: httpClientDetails.User,
Password: httpClientDetails.Password,
ApiKey: httpClientDetails.ApiKey,
Headers: headers}
User: httpClientDetails.User,
Password: httpClientDetails.Password,
ApiKey: httpClientDetails.ApiKey,
AccessToken: httpClientDetails.AccessToken,
Headers: headers}
}

0 comments on commit af471e7

Please sign in to comment.