Use Token Auth via OCM SDK
@@ -2,9 +2,9 @@ package ocm
import (
"context"
- "encoding/json"
"fmt"
+ amgmtv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"github.com/patrickmn/go-cache"
)
@@ -33,61 +33,30 @@ func (a authentication) AuthenticatePullSecret(ctx context.Context, pullSecret s
}
defer connection.Close()
- request := connection.Post()
- request.Path("/api/accounts_mgmt/v1/token_authorization")
+ accessTokenAPI := connection.AccountsMgmt().V1()
+ request, err := amgmtv1.NewTokenAuthorizationRequest().AuthorizationToken(pullSecret).Build()
- type TokenAuthorizationRequest struct {
- AuthorizationToken string `json:"authorization_token"`
- }
-
- tokenAuthorizationRequest := TokenAuthorizationRequest{
- AuthorizationToken: pullSecret,
- }
-
- var jsonData []byte
- jsonData, err = json.Marshal(tokenAuthorizationRequest)
if err != nil {
return nil, err
}
- request.Bytes(jsonData)
- postResp, err := request.SendContext(ctx)
+ response, err := accessTokenAPI.TokenAuthorization().Post().Request(request).Send()
+
if err != nil {
return nil, err
}
- if postResp.Status() != 200 {
- return nil, fmt.Errorf("Failed to validate Pull Secret Token")
- }
+ responseVal, ok := response.GetResponse()
- type TokenAuthorizationResponse struct {
- Account struct {
- ID string `json:"id"`
- Kind string `json:"kind"`
- Href string `json:"href"`
- FirstName string `json:"first_name"`
- LastName string `json:"last_name"`
- Username string `json:"username"`
- Email string `json:"email"`
- Organization struct {
- ID string `json:"id"`
- Kind string `json:"kind"`
- Href string `json:"href"`
- Name string `json:"name"`
- ExternalID string `json:"external_id"`
- } `json:"organization"`
- } `json:"account"`
+ if !ok {
+ return nil, fmt.Errorf("Failed to validate Pull Secret Token")
}
- var tokenAuthorizationResponse TokenAuthorizationResponse
- if err := json.Unmarshal(postResp.Bytes(), &tokenAuthorizationResponse); err != nil {
- return nil, err
- }
payload := &AuthPayload{}
- payload.Username = tokenAuthorizationResponse.Account.Username
- payload.FirstName = tokenAuthorizationResponse.Account.FirstName
- payload.LastName = tokenAuthorizationResponse.Account.LastName
- payload.Email = tokenAuthorizationResponse.Account.Email
+ payload.Username = responseVal.Account().Username()
+ payload.FirstName = responseVal.Account().FirstName()
+ payload.LastName = responseVal.Account().LastName()
+ payload.Email = responseVal.Account().Email()
a.client.Cache.Set(pullSecret, payload, cache.DefaultExpiration)
return payload, nil
