forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
45 lines (38 loc) · 1.19 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package uaa
import (
"net/http"
"net/url"
"strings"
"code.cloudfoundry.org/cli/api/uaa/internal"
)
// AuthResponse contains the access token and refresh token which are granted
// after UAA has authorized a user.
type AuthResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
// Authenticate sends a username and password to UAA then returns an access
// token and a refresh token.
func (client Client) Authenticate(username string, password string) (string, string, error) {
requestBody := url.Values{}
requestBody.Set("username", username)
requestBody.Set("password", password)
requestBody.Set("grant_type", "password")
request, err := client.newRequest(requestOptions{
RequestName: internal.PostOAuthTokenRequest,
Header: http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
},
Body: strings.NewReader(requestBody.Encode()),
})
if err != nil {
return "", "", err
}
request.SetBasicAuth(client.id, client.secret)
responseBody := AuthResponse{}
response := Response{
Result: &responseBody,
}
err = client.connection.Make(request, &response)
return responseBody.AccessToken, responseBody.RefreshToken, err
}