MCPcopy Index your code
hub / github.com/cloudfoundry/cli / getAuthToken

Method getAuthToken

cf/api/authentication/authentication.go:220–270  ·  view source on GitHub ↗
(data url.Values)

Source from the content-addressed store, hash-verified

218}
219
220func (uaa UAARepository) getAuthToken(data url.Values) error {
221 var accessToken string
222
223 type uaaErrorResponse struct {
224 Code string `json:"error"`
225 Description string `json:"error_description"`
226 }
227
228 type AuthenticationResponse struct {
229 AccessToken string `json:"access_token"`
230 TokenType string `json:"token_type"`
231 RefreshToken string `json:"refresh_token"`
232 Error uaaErrorResponse `json:"error"`
233 }
234
235 path := fmt.Sprintf("%s/oauth/token", uaa.config.AuthenticationEndpoint())
236
237 if uaa.config.UAAGrantType() != "client_credentials" {
238 accessToken = "Basic " + base64.StdEncoding.EncodeToString([]byte(uaa.config.UAAOAuthClient()+":"+uaa.config.UAAOAuthClientSecret()))
239 }
240
241 request, err := uaa.gateway.NewRequest("POST", path, accessToken, strings.NewReader(data.Encode()))
242
243 if err != nil {
244 return fmt.Errorf("%s: %s", T("Failed to start oauth request"), err.Error())
245 }
246 request.HTTPReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
247
248 response := new(AuthenticationResponse)
249 _, err = uaa.gateway.PerformRequestForJSONResponse(request, &response)
250
251 switch err.(type) {
252 case nil:
253 case errors.HTTPError:
254 return err
255 case *errors.InvalidTokenError:
256 return errors.New(T("Authentication has expired. Please log back in to re-authenticate.\n\nTIP: Use `cf login -a <endpoint> -u <user> -o <org> -s <space>` to log back in and re-authenticate."))
257 default:
258 return fmt.Errorf("%s: %s", T("auth request failed"), err.Error())
259 }
260
261 // TODO: get the actual status code
262 if response.Error.Code != "" {
263 return errors.NewHTTPError(0, response.Error.Code, response.Error.Description)
264 }
265
266 uaa.config.SetAccessToken(fmt.Sprintf("%s %s", response.TokenType, response.AccessToken))
267 uaa.config.SetRefreshToken(response.RefreshToken)
268
269 return nil
270}

Callers 2

AuthenticateMethod · 0.95
RefreshTokenMethod · 0.95

Calls 12

NewFunction · 0.92
NewHTTPErrorFunction · 0.92
NewRequestMethod · 0.80
UAAGrantTypeMethod · 0.65
UAAOAuthClientMethod · 0.65
UAAOAuthClientSecretMethod · 0.65
ErrorMethod · 0.65
SetMethod · 0.65
SetAccessTokenMethod · 0.65
SetRefreshTokenMethod · 0.65

Tested by

no test coverage detected