forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
149 lines (124 loc) · 4.1 KB
/
authentication.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package api
import (
"encoding/base64"
"fmt"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/net"
"github.com/cloudfoundry/cli/cf/terminal"
"net/url"
"os"
"strings"
)
type TokenRefresher interface {
RefreshAuthToken() (updatedToken string, apiErr error)
}
type AuthenticationRepository interface {
RefreshAuthToken() (updatedToken string, apiErr error)
Authenticate(credentials map[string]string) (apiErr error)
GetLoginPromptsAndSaveUAAServerURL() (map[string]configuration.AuthPrompt, error)
}
type UAAAuthenticationRepository struct {
config configuration.ReadWriter
gateway net.Gateway
}
func NewUAAAuthenticationRepository(gateway net.Gateway, config configuration.ReadWriter) (uaa UAAAuthenticationRepository) {
uaa.gateway = gateway
uaa.config = config
return
}
func (uaa UAAAuthenticationRepository) Authenticate(credentials map[string]string) (apiErr error) {
data := url.Values{
"grant_type": {"password"},
"scope": {""},
}
for key, val := range credentials {
data[key] = []string{val}
}
apiErr = uaa.getAuthToken(data)
switch response := apiErr.(type) {
case errors.HttpError:
if response.StatusCode() == 401 {
apiErr = errors.New("Credentials were rejected, please try again.")
}
}
return
}
type LoginResource struct {
Prompts map[string][]string
Links map[string]string
}
var knownAuthPromptTypes = map[string]configuration.AuthPromptType{
"text": configuration.AuthPromptTypeText,
"password": configuration.AuthPromptTypePassword,
}
func (r *LoginResource) parsePrompts() (prompts map[string]configuration.AuthPrompt) {
prompts = make(map[string]configuration.AuthPrompt)
for key, val := range r.Prompts {
prompts[key] = configuration.AuthPrompt{
Type: knownAuthPromptTypes[val[0]],
DisplayName: val[1],
}
}
return
}
func (uaa UAAAuthenticationRepository) GetLoginPromptsAndSaveUAAServerURL() (prompts map[string]configuration.AuthPrompt, apiErr error) {
url := fmt.Sprintf("%s/login", uaa.config.AuthenticationEndpoint())
resource := &LoginResource{}
apiErr = uaa.gateway.GetResource(url, resource)
prompts = resource.parsePrompts()
if resource.Links["uaa"] == "" {
uaa.config.SetUaaEndpoint(uaa.config.AuthenticationEndpoint())
} else {
uaa.config.SetUaaEndpoint(resource.Links["uaa"])
}
return
}
func (uaa UAAAuthenticationRepository) RefreshAuthToken() (updatedToken string, apiErr error) {
data := url.Values{
"refresh_token": {uaa.config.RefreshToken()},
"grant_type": {"refresh_token"},
"scope": {""},
}
apiErr = uaa.getAuthToken(data)
updatedToken = uaa.config.AccessToken()
if apiErr != nil {
fmt.Printf("%s\n\n", terminal.NotLoggedInText())
os.Exit(1)
}
return
}
func (uaa UAAAuthenticationRepository) getAuthToken(data url.Values) error {
type uaaErrorResponse struct {
Code string `json:"error"`
Description string `json:"error_description"`
}
type AuthenticationResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Error uaaErrorResponse `json:"error"`
}
path := fmt.Sprintf("%s/oauth/token", uaa.config.AuthenticationEndpoint())
request, err := uaa.gateway.NewRequest("POST", path, "Basic "+base64.StdEncoding.EncodeToString([]byte("cf:")), strings.NewReader(data.Encode()))
if err != nil {
return errors.NewWithError("Failed to start oauth request", err)
}
request.HttpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response := new(AuthenticationResponse)
_, err = uaa.gateway.PerformRequestForJSONResponse(request, &response)
switch err.(type) {
case nil:
case errors.HttpError:
return err
default:
return errors.NewWithError("auth request failed", err)
}
// TODO: get the actual status code
if response.Error.Code != "" {
return errors.NewHttpError(0, response.Error.Code, response.Error.Description)
}
uaa.config.SetAccessToken(fmt.Sprintf("%s %s", response.TokenType, response.AccessToken))
uaa.config.SetRefreshToken(response.RefreshToken)
return nil
}