-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
89 lines (75 loc) · 1.69 KB
/
jwt.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
package formatter
import (
"github.com/BluePecker/JwtAuth/engine/client/formatter/context"
"github.com/BluePecker/JwtAuth/engine/server/parameter/jwt/response"
"bytes"
"strconv"
)
const (
AddrHeader = "CLIENT ADDR"
TLLHeader = "TOKEN TLL"
DeviceHeader = "DEVICE"
TokenHeader = "TOKEN"
QuietFormat = "{{.Token}}"
JwtTableFormat = "table {{.Addr}}\t{{.Tll}}\t{{.Device}}\t{{.Token}}"
)
type (
JsonWebToken struct {
context.BaseSubjectContext
truncate bool
jwt response.JsonWebToken
}
JsonWebTokenContext struct {
context.Context
JsonWebTokens []response.JsonWebToken
}
)
func (ctx JsonWebTokenContext) Write() {
switch ctx.Template {
case context.RawKey:
if ctx.Quiet {
ctx.Template = `Token: {{.Token}}`
} else {
ctx.Template = `Client Addr: {{.Addr}}\nToken TTL: {{.Tll}}\nDevice: {{.Device}}\nToken: {{.Token}}\n`
}
case context.TableKey:
if ctx.Quiet {
ctx.Template = QuietFormat
} else {
ctx.Template = JwtTableFormat
}
}
ctx.Buffer = bytes.NewBufferString("")
ctx.PreFormat()
tpl, err := ctx.Parser()
if err != nil {
return
}
for _, jwt := range ctx.JsonWebTokens {
jwtCtx := &JsonWebToken{
truncate: ctx.Truncate,
jwt: jwt,
}
err = ctx.ContextFormat(tpl, jwtCtx)
if err != nil {
return
}
}
ctx.FormFormat(tpl, &JsonWebToken{})
}
func (j *JsonWebToken) Addr() string {
j.AddHeader(AddrHeader)
return j.jwt.Addr
}
func (j *JsonWebToken) Tll() string {
j.AddHeader(TLLHeader)
return strconv.FormatFloat(j.jwt.TTL, 'f', -1, 64)
}
func (j *JsonWebToken) Device() string {
j.AddHeader(DeviceHeader)
return j.jwt.Device
}
func (j *JsonWebToken) Token() string {
j.AddHeader(TokenHeader)
return j.jwt.Singed
}