This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
authenticate.go
262 lines (226 loc) · 8.63 KB
/
authenticate.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package security
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"github.com/eclipse/codewind-installer/pkg/connections"
"github.com/eclipse/codewind-installer/pkg/utils"
"github.com/urfave/cli"
)
// AuthToken from the keycloak server after successfully authenticating
type AuthToken struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
NotBeforePolicy int `json:"not-before-policy"`
SessionState string `json:"session_state"`
Scope string `json:"scope"`
}
// SecAuthenticate - sends credentials to the auth server for a specific realm and returns an AuthToken
// connectionRealm can be used to override the supplied context arguments
func SecAuthenticate(httpClient utils.HTTPClient, c *cli.Context, connectionRealm string, connectionClient string) (*AuthToken, *SecError) {
cliHostname := strings.TrimSpace(strings.ToLower(c.String("host")))
cliUsername := strings.TrimSpace(strings.ToLower(c.String("username")))
cliRealm := strings.TrimSpace(strings.ToLower(c.String("realm")))
cliClient := strings.TrimSpace(strings.ToLower(c.String("client")))
cliPassword := strings.TrimSpace(c.String("password"))
connectionID := strings.TrimSpace(strings.ToLower(c.String("conid")))
// Check supplied context flags
if connectionID == "" && (cliHostname == "" || cliUsername == "" || cliRealm == "" || cliClient == "") {
err := errors.New("Must supply a connection ID or connection details")
return nil, &SecError{errOpConConfig, err, err.Error()}
}
hostname := ""
username := ""
password := ""
realm := ""
client := ""
// Check connection is known
connection, ConErr := connections.GetConnectionByID(connectionID)
if connectionID != "" && ConErr != nil {
return nil, &SecError{errOpConConfig, ConErr.Err, ConErr.Desc}
}
if connection != nil {
hostname = connection.AuthURL
realm = connection.Realm
client = connection.ClientID
}
// Use command line context flags in preference to loaded connection fields
if cliHostname != "" {
hostname = cliHostname
}
if cliUsername != "" {
username = cliUsername
}
if cliRealm != "" {
realm = cliRealm
}
if cliClient != "" {
client = cliClient
}
// When a matching connection exist retrieve secret from the keyring
if connection != nil {
secret, secError := SecKeyGetSecret(connection.ID, username)
if secError != nil && cliPassword == "" {
return nil, secError
}
password = secret
}
if cliPassword != "" {
password = cliPassword
}
// If a connection realm was supplied, use that instead of the command line Context flags.
// This allows this function to be used by other realms such as master when admins are performing initial setup of keycloak
if connectionRealm != "" {
realm = connectionRealm
}
if connectionClient != "" {
client = connectionClient
}
// Pre-flight check
if hostname == "" || realm == "" || username == "" || password == "" || client == "" {
err := errors.New(textInvalidOptions)
return nil, &SecError{errOpCLICommand, err, err.Error()}
}
// build REST request
url := hostname + "/auth/realms/" + realm + "/protocol/openid-connect/token"
payload := strings.NewReader("grant_type=password&client_id=" + client + "&username=" + username + "&password=" + password)
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")
req.Header.Add("Cache-Control", "no-cache")
req.Header.Add("cache-control", "no-cache")
// send request
res, err := httpClient.Do(req)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
// Handle special case http status codes
switch httpCode := res.StatusCode; {
case httpCode == http.StatusBadRequest, httpCode == http.StatusUnauthorized:
keycloakAPIError := parseKeycloakError(string(body), res.StatusCode)
kcError := errors.New(string(keycloakAPIError.ErrorDescription))
return nil, &SecError{keycloakAPIError.Error, kcError, kcError.Error()}
case httpCode == http.StatusNotFound:
keycloakAPIError := parseKeycloakError(string(body), res.StatusCode)
kcError := errors.New(string(keycloakAPIError.Error))
return nil, &SecError{errOpResponse, kcError, kcError.Error()}
case httpCode == http.StatusServiceUnavailable:
txtError := errors.New(textAuthIsDown)
return nil, &SecError{errOpResponse, txtError, txtError.Error()}
case httpCode != http.StatusOK:
err = errors.New(string(body))
return nil, &SecError{errOpResponse, err, err.Error()}
}
// Parse and return authtoken
authToken := AuthToken{}
err = json.Unmarshal([]byte(body), &authToken)
if err != nil {
return nil, &SecError{errOpResponseFormat, err, textUnableToParse}
}
// store access and refresh tokens in keyring if a connection is known
if connection != nil {
secErr := SecKeyUpdate(connectionID, "access_token", authToken.AccessToken)
if secErr != nil {
return &authToken, secErr
}
secErr = SecKeyUpdate(connectionID, "refresh_token", authToken.RefreshToken)
if secErr != nil {
return &authToken, secErr
}
// login successful, update users password in keyring
if password != "" {
secErr = SecKeyUpdate(connectionID, username, password)
if secErr != nil {
return &authToken, secErr
}
}
}
return &authToken, nil
}
// SecRefreshTokens : Retrieve new tokens using the cached refresh token
func SecRefreshTokens(httpClient utils.HTTPClient, c *cli.Context) (*AuthToken, *SecError) {
conID := c.String("conid")
// Read connection
connection, conErr := connections.GetConnectionByID(conID)
if conErr != nil {
secErr := &SecError{Op: conErr.Op, Err: conErr.Err, Desc: conErr.Desc}
return nil, secErr
}
// Read refresh token
refreshToken, secErr := SecKeyGetSecret(connection.ID, "refresh_token")
if secErr != nil {
return nil, secErr
}
// Get token
authTokens, secErr := SecRefreshAccessToken(httpClient, connection, refreshToken)
if secErr != nil {
return nil, secErr
}
return authTokens, nil
}
// SecRefreshAccessToken : Obtain an access token using a refresh token
func SecRefreshAccessToken(httpClient utils.HTTPClient, connection *connections.Connection, refreshToken string) (*AuthToken, *SecError) {
// build REST request
url := connection.AuthURL + "/auth/realms/" + connection.Realm + "/protocol/openid-connect/token"
payload := strings.NewReader("grant_type=refresh_token&client_id=" + connection.ClientID + "&refresh_token=" + refreshToken)
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Cache-Control", "no-cache")
req.Header.Add("cache-control", "no-cache")
// send request
res, err := httpClient.Do(req)
if err != nil {
return nil, &SecError{errOpConnection, err, err.Error()}
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
// Handle special case http status codes
switch httpCode := res.StatusCode; {
case httpCode == http.StatusBadRequest, httpCode == http.StatusUnauthorized:
keycloakAPIError := parseKeycloakError(string(body), res.StatusCode)
kcError := errors.New(string(keycloakAPIError.ErrorDescription))
return nil, &SecError{keycloakAPIError.Error, kcError, kcError.Error()}
case httpCode != http.StatusOK:
err = errors.New(string(body))
return nil, &SecError{errOpResponse, err, err.Error()}
}
// Parse and return AuthToken
authToken := AuthToken{}
err = json.Unmarshal([]byte(body), &authToken)
if err != nil {
// re-save the access and refresh token
secErr := SecKeyUpdate(connection.ID, "access_token", authToken.AccessToken)
if secErr != nil {
return &authToken, secErr
}
secErr = SecKeyUpdate(connection.ID, "refresh_token", authToken.RefreshToken)
if secErr != nil {
return &authToken, secErr
}
respErr := errors.New(string(body))
return nil, &SecError{errOpResponse, respErr, respErr.Error()}
}
return &authToken, nil
}