/
auth.go
120 lines (96 loc) · 2.68 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package uaa
import (
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
"code.cloudfoundry.org/cli/api/uaa/constant"
"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(creds map[string]string, origin string, grantType constant.GrantType) (string, string, error) {
requestBody := url.Values{
"grant_type": {string(grantType)},
}
for k, v := range creds {
requestBody.Set(k, v)
}
type loginHint struct {
Origin string `json:"origin"`
}
originStruct := loginHint{origin}
originParam, err := json.Marshal(originStruct)
if err != nil {
return "", "", err
}
var query url.Values
if origin != "" {
query = url.Values{
"login_hint": {string(originParam)},
}
}
request, err := client.newRequest(requestOptions{
RequestName: internal.PostOAuthTokenRequest,
Header: http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
},
Body: strings.NewReader(requestBody.Encode()),
Query: query,
})
if err != nil {
return "", "", err
}
if grantType == constant.GrantTypePassword {
request.SetBasicAuth(client.config.UAAOAuthClient(), client.config.UAAOAuthClientSecret())
}
responseBody := AuthResponse{}
response := Response{
Result: &responseBody,
}
err = client.connection.Make(request, &response)
return responseBody.AccessToken, responseBody.RefreshToken, err
}
func (client Client) Revoke(token string) error {
jti, err := client.getJtiFromToken(token)
if err != nil {
return err
}
revokeRequest, err := client.newRequest(requestOptions{
RequestName: internal.DeleteTokenRequest,
URIParams: map[string]string{
"token_id": jti,
},
})
revokeRequest.Header.Set("Authorization", "Bearer "+token)
if err != nil {
return err
}
err = client.connection.Make(revokeRequest, &Response{})
return err
}
func (client Client) getJtiFromToken(token string) (string, error) {
segments := strings.Split(token, ".")
if len(segments) < 2 {
return "", errors.New("access token missing segments")
}
jsonPayload, err := base64.RawURLEncoding.DecodeString(segments[1])
if err != nil {
return "", errors.New("could not base64 decode token payload")
}
payload := make(map[string]interface{})
json.Unmarshal(jsonPayload, &payload)
jti, ok := payload["jti"].(string)
if !ok {
return "", errors.New("could not parse jti from payload")
}
return jti, nil
}