Skip to content

Update GetXAccessToken #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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