-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.go
64 lines (51 loc) · 1.37 KB
/
opts.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
package soffit
import (
"encoding/base64"
"net/http"
"strings"
jasypt "astuart.co/go-jasypt"
"github.com/dgrijalva/jwt-go"
)
const headerPrefix = "X-Soffit-"
// Headers provides structured access to the several headers that may be
// sent in the soffit payload.
type Headers struct {
Preferences map[string]interface{} `json:"preferences"`
Definition map[string]interface{} `json:"definition"`
Request map[string]interface{} `json:"request"`
}
// Receiver takes a password, provides utilities for handling incoming Soffit
// requests.
type Receiver struct {
Password string
}
// GetHeaders takes url.Values and returns the decrypted headers.
func (d Receiver) GetHeaders(h http.Header) (*Headers, error) {
var s Headers
for k := range h {
if strings.Index(k, "X-Soffit") != 0 {
continue
}
bs, err := base64.StdEncoding.DecodeString(h.Get(k))
if err != nil {
return nil, err
}
err = jasypt.DecryptJasypt(bs, d.Password)
if err != nil {
return nil, err
}
token, err := jwt.Parse(string(bs), nil)
if err != nil && !strings.Contains(err.Error(), "Keyfunc") {
return nil, err
}
switch k {
case "X-Soffit-Portalrequest":
s.Request = token.Claims.(jwt.MapClaims)
case "X-Soffit-Definition":
s.Definition = token.Claims.(jwt.MapClaims)
case "X-Soffit-Preferences":
s.Preferences = token.Claims.(jwt.MapClaims)
}
}
return &s, nil
}