-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
80 lines (62 loc) · 1.78 KB
/
utils.go
File metadata and controls
80 lines (62 loc) · 1.78 KB
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
package tmpauth
import (
"encoding/base64"
"fmt"
"github.com/1lann/gjson"
"github.com/dgrijalva/jwt-go"
)
func (t *Tmpauth) DebugLog(str string) {
if !t.Config.Debug {
return
}
t.Config.Logger.Output(2, str)
}
func getJSONPath(jsonData, path string) string {
result := gjson.Get(jsonData, path)
if !result.Exists() {
return ""
}
return result.String()
}
func getJSONPathMany(jsonData, path string) []string {
var results []string
result := gjson.Get(jsonData, path)
for _, val := range result.Array() {
results = append(results, val.String())
}
return results
}
type HeaderOption struct {
Format string `json:"format"`
Optional bool `json:"optional"`
}
func (h *HeaderOption) Evaluate(jsonData string) (string, error) {
result := getJSONPath(jsonData, h.Format)
if result == "" && !h.Optional {
return "", fmt.Errorf("tmpauth: requested header format yielded no results on claim")
}
return result, nil
}
func (t *Tmpauth) CookieName() string {
return "__Host-tmpauth_" + t.Config.ClientID
}
func (t *Tmpauth) StateIDCookieName(id string) string {
t.hmacMutex.Lock()
t.HMAC.Reset()
t.HMAC.Write([]byte(id))
name := base64.RawURLEncoding.EncodeToString(t.HMAC.Sum(nil))
t.hmacMutex.Unlock()
return "__Host-tmpauth-stateid_" + name
}
func (t *Tmpauth) VerifyWithPublicKey(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("tmpauth: expected ECDSA signing method, got: %v", token.Header["alg"])
}
return t.Config.PublicKey, nil
}
func (t *Tmpauth) VerifyWithSecret(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("tmpauth: expected HMAC signing method, got: %v", token.Header["alg"])
}
return t.Config.Secret, nil
}