-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.go
481 lines (380 loc) · 12.6 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package authentication
import (
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/ONSdigital/eq-questionnaire-launcher/clients"
"github.com/ONSdigital/eq-questionnaire-launcher/settings"
"github.com/ONSdigital/eq-questionnaire-launcher/surveys"
"github.com/satori/go.uuid"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/json"
"gopkg.in/square/go-jose.v2/jwt"
"bytes"
"log"
"path"
"strconv"
"strings"
)
// KeyLoadError describes an error that can occur during key loading
type KeyLoadError struct {
// Op is the operation which caused the error, such as
// "read", "parse" or "cast".
Op string
// Err is a description of the error that occurred during the operation.
Err string
}
func (e *KeyLoadError) Error() string {
if e == nil {
return "<nil>"
}
return e.Op + ": " + e.Err
}
// PublicKeyResult is a wrapper for the public key and the kid that identifies it
type PublicKeyResult struct {
key *rsa.PublicKey
kid string
}
// PrivateKeyResult is a wrapper for the private key and the kid that identifies it
type PrivateKeyResult struct {
key *rsa.PrivateKey
kid string
}
func loadEncryptionKey() (*PublicKeyResult, *KeyLoadError) {
encryptionKeyPath := settings.Get("JWT_ENCRYPTION_KEY_PATH")
keyData, err := ioutil.ReadFile(encryptionKeyPath)
if err != nil {
return nil, &KeyLoadError{Op: "read", Err: "Failed to read encryption key from file: " + encryptionKeyPath}
}
block, _ := pem.Decode(keyData)
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, &KeyLoadError{Op: "parse", Err: "Failed to parse encryption key PEM"}
}
kid := fmt.Sprintf("%x", sha1.Sum(keyData))
publicKey, ok := pub.(*rsa.PublicKey)
if !ok {
return nil, &KeyLoadError{Op: "cast", Err: "Failed to cast key to rsa.PublicKey"}
}
return &PublicKeyResult{publicKey, kid}, nil
}
func loadSigningKey() (*PrivateKeyResult, *KeyLoadError) {
signingKeyPath := settings.Get("JWT_SIGNING_KEY_PATH")
keyData, err := ioutil.ReadFile(signingKeyPath)
if err != nil {
return nil, &KeyLoadError{Op: "read", Err: "Failed to read signing key from file: " + signingKeyPath}
}
block, _ := pem.Decode(keyData)
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, &KeyLoadError{Op: "parse", Err: "Failed to parse signing key from PEM"}
}
PublicKey, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey)
if err != nil {
return nil, &KeyLoadError{Op: "marshal", Err: "Failed to marshal public key"}
}
pubBytes := pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: PublicKey,
})
kid := fmt.Sprintf("%x", sha1.Sum(pubBytes))
return &PrivateKeyResult{privateKey, kid}, nil
}
// QuestionnaireSchema is a minimal representation of a questionnaire schema used for extracting the eq_id and form_type
type QuestionnaireSchema struct {
EqID string `json:"eq_id"`
FormType string `json:"form_type"`
Metadata []Metadata `json:"metadata"`
}
// Metadata is a representation of the metadata within the schema with an additional `Default` value
type Metadata struct {
Name string `json:"name"`
Validator string `json:"type"`
Default string `json:"default"`
}
func generateClaims(claimValues map[string][]string) (claims map[string]interface{}) {
var roles []string
if rolesValues, ok := claimValues["roles"]; ok {
roles = rolesValues
} else {
roles = []string{"dumper"}
}
claims = make(map[string]interface{})
claims["roles"] = roles
claims["tx_id"] = uuid.NewV4().String()
for key, value := range claimValues {
if key != "roles" {
claims[key] = value[0]
} else {
claims[key] = value
}
}
log.Printf("Claims: %s", claims)
return claims
}
// GenerateJwtClaims creates a jwtClaim needed to generate a token
func GenerateJwtClaims() (jwtClaims map[string]interface{}) {
issued := time.Now()
expires := issued.Add(time.Minute * 10) // TODO: Support custom exp: r.PostForm.Get("exp")
jwtClaims = make(map[string]interface{})
jwtClaims["iat"] = jwt.NewNumericDate(issued)
jwtClaims["exp"] = jwt.NewNumericDate(expires)
jwtClaims["jti"] = uuid.NewV4().String()
return jwtClaims
}
func launcherSchemaFromURL(url string) (launcherSchema surveys.LauncherSchema, error string) {
resp, err := clients.GetHTTPClient().Get(url)
if err != nil {
panic(err)
}
if resp.StatusCode != 200 {
return launcherSchema, fmt.Sprintf("Failed to load Schema from %s", url)
}
responseBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
panic(err)
}
validationError := validateSchema(responseBody)
if validationError != "" {
return launcherSchema, validationError
}
var schema QuestionnaireSchema
if err := json.Unmarshal(responseBody, &schema); err != nil {
panic(err)
}
cacheBust := ""
if !strings.Contains(url, "?") {
cacheBust = "?bust=" + time.Now().Format("20060102150405")
}
launcherSchema = surveys.LauncherSchema{
EqID: schema.EqID,
FormType: schema.FormType,
URL: url + cacheBust,
}
return launcherSchema, ""
}
func validateSchema(payload []byte) (error string) {
if settings.Get("SCHEMA_VALIDATOR_URL") == "" {
return ""
}
validateURL, _ := url.Parse(settings.Get("SCHEMA_VALIDATOR_URL"))
validateURL.Path = path.Join(validateURL.Path, "validate")
log.Println("Validating schema: ", validateURL.String())
resp, err := http.Post(validateURL.String(), "application/json", bytes.NewBuffer(payload))
if err != nil {
return err.Error()
}
responseBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err.Error()
}
if resp.StatusCode != 200 {
return string(responseBody)
}
return ""
}
func getSchemaClaims(LauncherSchema surveys.LauncherSchema) map[string]interface{} {
schemaClaims := make(map[string]interface{})
schemaClaims["eq_id"] = LauncherSchema.EqID
schemaClaims["form_type"] = LauncherSchema.FormType
if LauncherSchema.URL != "" {
schemaClaims["survey_url"] = LauncherSchema.URL
}
return schemaClaims
}
// TokenError describes an error that can occur during JWT generation
type TokenError struct {
// Err is a description of the error that occurred.
Desc string
// From is optionally the original error from which this one was caused.
From error
}
func (e *TokenError) Error() string {
if e == nil {
return "<nil>"
}
err := e.Desc
if e.From != nil {
err += " (" + e.From.Error() + ")"
}
return err
}
// generateTokenFromClaims creates a token though encryption using the private and public keys
func generateTokenFromClaims(cl map[string]interface{}) (string, *TokenError) {
privateKeyResult, keyErr := loadSigningKey()
if keyErr != nil {
return "", &TokenError{Desc: "Error loading signing key", From: keyErr}
}
publicKeyResult, keyErr := loadEncryptionKey()
if keyErr != nil {
return "", &TokenError{Desc: "Error loading encryption key", From: keyErr}
}
opts := jose.SignerOptions{}
opts.WithType("JWT")
opts.WithHeader("kid", privateKeyResult.kid)
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS256, Key: privateKeyResult.key}, &opts)
if err != nil {
return "", &TokenError{Desc: "Error creating JWT signer", From: err}
}
encryptor, err := jose.NewEncrypter(
jose.A256GCM,
jose.Recipient{Algorithm: jose.RSA_OAEP, Key: publicKeyResult.key, KeyID: publicKeyResult.kid},
(&jose.EncrypterOptions{}).WithType("JWT").WithContentType("JWT"))
if err != nil {
return "", &TokenError{Desc: "Error creating JWT signer", From: err}
}
token, err := jwt.SignedAndEncrypted(signer, encryptor).Claims(cl).CompactSerialize()
if err != nil {
return "", &TokenError{Desc: "Error signing and encrypting JWT", From: err}
}
log.Println("Created signed/encrypted JWT:", token)
return token, nil
}
func getBooleanOrDefault(key string, values map[string][]string, defaultValue bool) bool {
if keyValues, ok := values[key]; ok {
booleanValue, _ := strconv.ParseBool(keyValues[0])
return booleanValue
}
return defaultValue
}
func getStringOrDefault(key string, values map[string][]string, defaultValue string) string {
if keyValues, ok := values[key]; ok {
return keyValues[0]
}
return defaultValue
}
// GenerateTokenFromDefaults coverts a set of DEFAULT values into a JWT
func GenerateTokenFromDefaults(surveyURL string, accountServiceURL string, accountServiceLogOutURL string, urlValues url.Values) (token string, error string) {
claims := make(map[string]interface{})
urlValues["account_service_url"] = []string{accountServiceURL}
urlValues["account_service_log_out_url"] = []string{accountServiceLogOutURL}
claims = generateClaims(urlValues)
launcherSchema, validationError := launcherSchemaFromURL(surveyURL)
if validationError != "" {
return "", validationError
}
requiredMetadata, error := GetRequiredMetadata(launcherSchema)
if error != "" {
return "", fmt.Sprintf("GetRequiredMetadata failed err: %v", error)
}
for _, metadata := range requiredMetadata {
if metadata.Validator == "boolean" {
claims[metadata.Name] = getBooleanOrDefault(metadata.Name, urlValues, false)
continue
}
claims[metadata.Name] = getStringOrDefault(metadata.Name, urlValues, metadata.Default)
}
jwtClaims := GenerateJwtClaims()
for key, v := range jwtClaims {
claims[key] = v
}
schemaClaims := getSchemaClaims(launcherSchema)
for key, v := range schemaClaims {
claims[key] = v
}
token, tokenError := generateTokenFromClaims(claims)
if tokenError != nil {
return token, fmt.Sprintf("GenerateTokenFromDefaults failed err: %v", tokenError)
}
return token, ""
}
// GenerateTokenFromPost coverts a set of POST values into a JWT
func GenerateTokenFromPost(postValues url.Values) (string, string) {
log.Println("POST received: ", postValues)
schema := postValues.Get("schema")
launcherSchema := surveys.FindSurveyByName(schema)
claims := generateClaims(postValues)
jwtClaims := GenerateJwtClaims()
for key, v := range jwtClaims {
claims[key] = v
}
schemaClaims := getSchemaClaims(launcherSchema)
for key, v := range schemaClaims {
claims[key] = v
}
requiredMetadata, error := GetRequiredMetadata(launcherSchema)
if error != "" {
return "", fmt.Sprintf("GetRequiredMetadata failed err: %v", error)
}
for _, metadata := range requiredMetadata {
if metadata.Validator == "boolean" {
_, isset := claims[metadata.Name]
claims[metadata.Name] = isset
}
}
token, tokenError := generateTokenFromClaims(claims)
if tokenError != nil {
return token, fmt.Sprintf("GenerateTokenFromPost failed err: %v", tokenError)
}
return token, ""
}
// GetRequiredMetadata Gets the required metadata from a schema
func GetRequiredMetadata(launcherSchema surveys.LauncherSchema) ([]Metadata, string) {
var url string
if launcherSchema.URL != "" {
url = launcherSchema.URL
} else {
hostURL := settings.Get("SURVEY_RUNNER_SCHEMA_URL")
url = fmt.Sprintf("%s/schemas/%s/%s", hostURL, launcherSchema.EqID, launcherSchema.FormType)
}
log.Println("Loading metadata from schema:", url)
resp, err := clients.GetHTTPClient().Get(url)
if err != nil {
return nil, fmt.Sprintf("Failed to load Schema from %s", url)
}
if resp.StatusCode != 200 {
return nil, fmt.Sprintf("Failed to load Schema from %s", url)
}
responseBody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, fmt.Sprintf("Failed to load Schema from %s", url)
}
var schema QuestionnaireSchema
if err := json.Unmarshal(responseBody, &schema); err != nil {
log.Print(err)
return nil, fmt.Sprintf("Failed to unmarshal Schema from %s", url)
}
defaults := GetDefaultValues()
for i, value := range schema.Metadata {
schema.Metadata[i].Default = defaults[value.Name]
if value.Validator == "boolean" {
schema.Metadata[i].Default = "false"
}
}
return schema.Metadata, ""
}
// GetDefaultValues Returns a map of default values for metadata keys
func GetDefaultValues() map[string]string {
defaults := make(map[string]string)
defaults["user_id"] = "UNKNOWN"
defaults["period_id"] = "201605"
defaults["period_str"] = "May 2017"
defaults["collection_exercise_sid"] = uuid.NewV4().String()
defaults["ru_ref"] = "12346789012A"
defaults["ru_name"] = "ESSENTIAL ENTERPRISE LTD."
defaults["ref_p_start_date"] = "2016-05-01"
defaults["ref_p_end_date"] = "2016-05-31"
defaults["return_by"] = "2016-06-12"
defaults["trad_as"] = "ESSENTIAL ENTERPRISE LTD."
defaults["employment_date"] = "2016-06-10"
defaults["region_code"] = "GB-ENG"
defaults["language_code"] = "en"
defaults["case_ref"] = "1000000000000001"
defaults["address_line1"] = "68 Abingdon Road"
defaults["address_line2"] = ""
defaults["locality"] = ""
defaults["town_name"] = "Goathill"
defaults["postcode"] = "PE12 4GH"
defaults["display_address"] = "68 Abingdon Road, Goathill"
defaults["country"] = "E"
return defaults
}