forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
105 lines (87 loc) · 2.94 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
package api
import (
"cf/configuration"
"cf/net"
"cf/terminal"
"encoding/base64"
"fmt"
"net/url"
"os"
"strings"
)
type AuthenticationRepository interface {
Authenticate(email string, password string) (apiResponse net.ApiResponse)
RefreshAuthToken() (updatedToken string, apiResponse net.ApiResponse)
}
type UAAAuthenticationRepository struct {
configRepo configuration.ConfigurationRepository
config *configuration.Configuration
gateway net.Gateway
}
func NewUAAAuthenticationRepository(gateway net.Gateway, configRepo configuration.ConfigurationRepository) (uaa UAAAuthenticationRepository) {
uaa.gateway = gateway
uaa.configRepo = configRepo
uaa.config, _ = configRepo.Get()
return
}
func (uaa UAAAuthenticationRepository) Authenticate(email string, password string) (apiResponse net.ApiResponse) {
data := url.Values{
"username": {email},
"password": {password},
"grant_type": {"password"},
"scope": {""},
}
apiResponse = uaa.getAuthToken(data)
if apiResponse.IsNotSuccessful() && apiResponse.StatusCode == 401 {
apiResponse.Message = "Password is incorrect, please try again."
}
return
}
func (uaa UAAAuthenticationRepository) RefreshAuthToken() (updatedToken string, apiResponse net.ApiResponse) {
data := url.Values{
"refresh_token": {uaa.config.RefreshToken},
"grant_type": {"refresh_token"},
"scope": {""},
}
apiResponse = uaa.getAuthToken(data)
updatedToken = uaa.config.AccessToken
if apiResponse.IsError() {
fmt.Printf("%s\n\n", terminal.NotLoggedInText())
os.Exit(1)
}
return
}
func (uaa UAAAuthenticationRepository) getAuthToken(data url.Values) (apiResponse net.ApiResponse) {
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.AuthorizationEndpoint)
request, apiResponse := uaa.gateway.NewRequest("POST", path, "Basic "+base64.StdEncoding.EncodeToString([]byte("cf:")), strings.NewReader(data.Encode()))
if apiResponse.IsNotSuccessful() {
return
}
request.HttpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response := new(AuthenticationResponse)
_, apiResponse = uaa.gateway.PerformRequestForJSONResponse(request, &response)
if apiResponse.IsNotSuccessful() {
return
}
if response.Error.Code != "" {
apiResponse = net.NewApiResponseWithMessage("Authentication Server error: %s", response.Error.Description)
return
}
uaa.config.AccessToken = fmt.Sprintf("%s %s", response.TokenType, response.AccessToken)
uaa.config.RefreshToken = response.RefreshToken
err := uaa.configRepo.Save()
if err != nil {
apiResponse = net.NewApiResponseWithError("Error setting configuration", err)
}
return
}