Authenticate sends a username and password to UAA then returns an access token and a refresh token.
(creds map[string]string, origin string, grantType constant.GrantType)
| 22 | // Authenticate sends a username and password to UAA then returns an access |
| 23 | // token and a refresh token. |
| 24 | func (client Client) Authenticate(creds map[string]string, origin string, grantType constant.GrantType) (string, string, error) { |
| 25 | requestBody := url.Values{ |
| 26 | "grant_type": {string(grantType)}, |
| 27 | } |
| 28 | |
| 29 | for k, v := range creds { |
| 30 | requestBody.Set(k, v) |
| 31 | } |
| 32 | |
| 33 | type loginHint struct { |
| 34 | Origin string `json:"origin"` |
| 35 | } |
| 36 | |
| 37 | originStruct := loginHint{origin} |
| 38 | originParam, err := json.Marshal(originStruct) |
| 39 | if err != nil { |
| 40 | return "", "", err |
| 41 | } |
| 42 | |
| 43 | var query url.Values |
| 44 | if origin != "" { |
| 45 | query = url.Values{ |
| 46 | "login_hint": {string(originParam)}, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | request, err := client.newRequest(requestOptions{ |
| 51 | RequestName: internal.PostOAuthTokenRequest, |
| 52 | Header: http.Header{ |
| 53 | "Content-Type": {"application/x-www-form-urlencoded"}, |
| 54 | }, |
| 55 | Body: strings.NewReader(requestBody.Encode()), |
| 56 | Query: query, |
| 57 | }) |
| 58 | |
| 59 | if err != nil { |
| 60 | return "", "", err |
| 61 | } |
| 62 | |
| 63 | if grantType == constant.GrantTypePassword { |
| 64 | request.SetBasicAuth(client.config.UAAOAuthClient(), client.config.UAAOAuthClientSecret()) |
| 65 | } else if grantType == constant.GrantTypeJwtBearer { |
| 66 | // overwrite client authentication in case of provided parameters in cf auth clientid clientsecret or use defaults as done in password grant |
| 67 | clientId := client.config.UAAOAuthClient() |
| 68 | clientSecret := client.config.UAAOAuthClientSecret() |
| 69 | if creds["client_id"] != "" { |
| 70 | clientId = creds["client_id"] |
| 71 | } |
| 72 | if creds["client_secret"] != "" { |
| 73 | clientSecret = creds["client_secret"] |
| 74 | } |
| 75 | request.SetBasicAuth(clientId, clientSecret) |
| 76 | } |
| 77 | |
| 78 | responseBody := AuthResponse{} |
| 79 | response := Response{ |
| 80 | Result: &responseBody, |
| 81 | } |
nothing calls this directly
no test coverage detected