-
Notifications
You must be signed in to change notification settings - Fork 23
/
metadata.go
99 lines (90 loc) · 2.38 KB
/
metadata.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
package jwt
import (
"github.com/project-flogo/core/data/coerce"
)
type Settings struct{}
type Input struct {
Token string `md:"token"`
Key string `md:"key"`
SigningMethod string `md:"signingMethod"`
Issuer string `md:"iss"`
Subject string `md:"sub"`
Audience string `md:"aud"`
}
func (r *Input) FromMap(values map[string]interface{}) error {
var err error
r.Token, err = coerce.ToString(values["token"])
if err != nil {
return err
}
r.Key, err = coerce.ToString(values["key"])
if err != nil {
return err
}
r.SigningMethod, err = coerce.ToString(values["signingMethod"])
if err != nil {
return err
}
r.Issuer, err = coerce.ToString(values["iss"])
if err != nil {
return err
}
r.Subject, err = coerce.ToString(values["sub"])
if err != nil {
return err
}
r.Audience, err = coerce.ToString(values["aud"])
if err != nil {
return err
}
return nil
}
func (r *Input) ToMap() map[string]interface{} {
return map[string]interface{}{
"token": r.Token,
"key": r.Key,
"signingMethod": r.SigningMethod,
"iss": r.Issuer,
"sub": r.Subject,
"aud": r.Audience,
}
}
type Output struct {
Valid bool `md:"valid"`
Token ParsedToken `md:"token"`
ValidationMessage string `md:"validationMessage"`
Error bool `md:"error"`
ErrorMessage string `md:"errorMessage"`
}
// ParsedToken is a parsed JWT token.
type ParsedToken struct {
Claims map[string]interface{} `json:"claims"`
Signature string `json:"signature"`
SigningMethod string `json:"signingMethod"`
Header map[string]interface{} `json:"header"`
}
func (o *Output) FromMap(values map[string]interface{}) error {
valid, err := coerce.ToBool(values["valid"])
if err != nil {
return err
}
o.Valid = valid
token, err := coerce.ToAny(values["token"])
if err != nil {
return err
}
o.Token = token.(ParsedToken)
o.ValidationMessage = values["validationMessage"].(string)
o.Error = values["error"].(bool)
o.ErrorMessage = values["errorMessage"].(string)
return nil
}
func (o *Output) ToMap() map[string]interface{} {
return map[string]interface{}{
"valid": o.Valid,
"token": o.Token,
"validationMessage": o.ValidationMessage,
"error": o.Error,
"errorMessage": o.ErrorMessage,
}
}