Skip to content

Commit 097c00a

Browse files
committed
Update GetXAccessToken
1 parent b4ad609 commit 097c00a

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

client/api_key.go

+34-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
77
"log"
8-
"net/http"
98
)
109

1110
type ApiKeySubject struct {
@@ -30,6 +29,14 @@ type ApiKey struct {
3029
Created string `json:"created,omitempty"`
3130
}
3231

32+
type TokenResponse struct {
33+
AccessToken string `json:"accessToken"`
34+
User struct {
35+
UserName string `json:"userName,omitempty"`
36+
Email string `json:"email,omitempty"`
37+
} `json:"user"`
38+
}
39+
3340
func (client *Client) GetAPIKey(keyID string) (*ApiKey, error) {
3441

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

137-
url := fmt.Sprintf("%s/admin/user/loginAsUser?userId=%s", client.Host, userID)
138-
request, err := http.NewRequest("GET", url, nil)
144+
fullPath := fmt.Sprintf("/admin/user/loginAsUser?userId=%s", userID)
145+
opts := RequestOptions{
146+
Path: fullPath,
147+
Method: "GET",
148+
}
149+
150+
resp, err := client.RequestAPI(&opts)
151+
139152
if err != nil {
140153
return "", err
141154
}
142155

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

146-
resp, err := client.Client.Do(request)
159+
err = DecodeResponseInto(resp, &asUserTokenResponse)
147160
if err != nil {
148161
return "", err
149162
}
150163

151-
defer resp.Body.Close()
164+
userCfAccessToken = asUserTokenResponse.AccessToken
152165

153-
var userCfAccessToken string
154-
for _, cookie := range resp.Cookies() {
155-
if cookie.Name == "cf-access-token" {
156-
userCfAccessToken = cookie.Value
157-
break
158-
}
159-
}
160166
if userCfAccessToken == "" {
161167
return "", fmt.Errorf("Failed to GetXAccessToken for userId = %s", userID)
162168
}
163169

164170
// change account
165-
changeAccURL := fmt.Sprintf("%s/user/changeaccount/%s", client.Host, accountId)
166-
changeAccRequest, err := http.NewRequest("POST", changeAccURL, nil)
167-
if err != nil {
168-
return "", err
171+
fullPath = fmt.Sprintf("/user/changeaccount/%s", accountId)
172+
opts = RequestOptions{
173+
Path: fullPath,
174+
Method: "POST",
175+
XAccessToken: userCfAccessToken,
169176
}
170177

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

174-
changeAccResp, err := client.Client.Do(changeAccRequest)
175180
if err != nil {
176181
return "", err
177182
}
178183

179184
var accCfAccessToken string
180-
for _, cookie := range changeAccResp.Cookies() {
181-
if cookie.Name == "cf-access-token" {
182-
accCfAccessToken = cookie.Value
183-
break
184-
}
185+
var changeAccountTokenResponse TokenResponse
186+
187+
err = DecodeResponseInto(resp, &changeAccountTokenResponse)
188+
if err != nil {
189+
return "", err
185190
}
191+
192+
accCfAccessToken = changeAccountTokenResponse.AccessToken
193+
186194
if accCfAccessToken == "" {
187195
return "", fmt.Errorf("Failed to GetXAccessToken for userId = %s after ChangeAcocunt to %s", userID, accountId)
188196
}

0 commit comments

Comments
 (0)