forked from jeaniejung/Wildcard_Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
147 lines (121 loc) · 4.25 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
package authentication
import (
"encoding/base64"
"fmt"
"net/url"
"strings"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/net"
)
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]core_config.AuthPrompt, error)
}
type UAAAuthenticationRepository struct {
config core_config.ReadWriter
gateway net.Gateway
}
func NewUAAAuthenticationRepository(gateway net.Gateway, config core_config.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(T("Credentials were rejected, please try again."))
}
}
return
}
type LoginResource struct {
Prompts map[string][]string
Links map[string]string
}
var knownAuthPromptTypes = map[string]core_config.AuthPromptType{
"text": core_config.AuthPromptTypeText,
"password": core_config.AuthPromptTypePassword,
}
func (r *LoginResource) parsePrompts() (prompts map[string]core_config.AuthPrompt) {
prompts = make(map[string]core_config.AuthPrompt)
for key, val := range r.Prompts {
prompts[key] = core_config.AuthPrompt{
Type: knownAuthPromptTypes[val[0]],
DisplayName: val[1],
}
}
return
}
func (uaa UAAAuthenticationRepository) GetLoginPromptsAndSaveUAAServerURL() (prompts map[string]core_config.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() (string, error) {
data := url.Values{
"refresh_token": {uaa.config.RefreshToken()},
"grant_type": {"refresh_token"},
"scope": {""},
}
apiErr := uaa.getAuthToken(data)
updatedToken := uaa.config.AccessToken()
return updatedToken, apiErr
}
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(T("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
case *errors.InvalidTokenError:
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."))
default:
return errors.NewWithError(T("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
}