Skip to content

Commit

Permalink
Merge pull request #42 from codefresh-io/update-x-access-token
Browse files Browse the repository at this point in the history
Update GetXAccessToken
  • Loading branch information
sharon-codefresh authored Nov 18, 2021
2 parents d94095f + 097c00a commit 780ff4b
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions client/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"log"
"net/http"
)

type ApiKeySubject struct {
Expand All @@ -30,6 +29,14 @@ type ApiKey struct {
Created string `json:"created,omitempty"`
}

type TokenResponse struct {
AccessToken string `json:"accessToken"`
User struct {
UserName string `json:"userName,omitempty"`
Email string `json:"email,omitempty"`
} `json:"user"`
}

func (client *Client) GetAPIKey(keyID string) (*ApiKey, error) {

opts := RequestOptions{
Expand Down Expand Up @@ -134,55 +141,56 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
// GetXAccessToken
func (client *Client) GetXAccessToken(userID string, accountId string) (string, error) {

url := fmt.Sprintf("%s/admin/user/loginAsUser?userId=%s", client.Host, userID)
request, err := http.NewRequest("GET", url, nil)
fullPath := fmt.Sprintf("/admin/user/loginAsUser?userId=%s", userID)
opts := RequestOptions{
Path: fullPath,
Method: "GET",
}

resp, err := client.RequestAPI(&opts)

if err != nil {
return "", err
}

request.Header.Set("Authorization", client.Token)
request.Header.Set("Content-Type", "application/json; charset=utf-8")
var userCfAccessToken string
var asUserTokenResponse TokenResponse

resp, err := client.Client.Do(request)
err = DecodeResponseInto(resp, &asUserTokenResponse)
if err != nil {
return "", err
}

defer resp.Body.Close()
userCfAccessToken = asUserTokenResponse.AccessToken

var userCfAccessToken string
for _, cookie := range resp.Cookies() {
if cookie.Name == "cf-access-token" {
userCfAccessToken = cookie.Value
break
}
}
if userCfAccessToken == "" {
return "", fmt.Errorf("Failed to GetXAccessToken for userId = %s", userID)
}

// change account
changeAccURL := fmt.Sprintf("%s/user/changeaccount/%s", client.Host, accountId)
changeAccRequest, err := http.NewRequest("POST", changeAccURL, nil)
if err != nil {
return "", err
fullPath = fmt.Sprintf("/user/changeaccount/%s", accountId)
opts = RequestOptions{
Path: fullPath,
Method: "POST",
XAccessToken: userCfAccessToken,
}

changeAccRequest.Header.Set("x-access-token", userCfAccessToken)
changeAccRequest.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err = client.RequestApiXAccessToken(&opts)

changeAccResp, err := client.Client.Do(changeAccRequest)
if err != nil {
return "", err
}

var accCfAccessToken string
for _, cookie := range changeAccResp.Cookies() {
if cookie.Name == "cf-access-token" {
accCfAccessToken = cookie.Value
break
}
var changeAccountTokenResponse TokenResponse

err = DecodeResponseInto(resp, &changeAccountTokenResponse)
if err != nil {
return "", err
}

accCfAccessToken = changeAccountTokenResponse.AccessToken

if accCfAccessToken == "" {
return "", fmt.Errorf("Failed to GetXAccessToken for userId = %s after ChangeAcocunt to %s", userID, accountId)
}
Expand Down

0 comments on commit 780ff4b

Please sign in to comment.