-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
55 lines (44 loc) · 1016 Bytes
/
encode.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
package securelogin
import (
"encoding/base64"
"io"
"strings"
)
var base64Encode = base64.StdEncoding.EncodeToString
type Encoder struct {
w io.Writer
}
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w}
}
func (e *Encoder) Encode(t Token) error {
data := Marshal(t)
_, err := e.w.Write(data)
return err
}
// Marshal returns encoded Token as defied by the spec.
func Marshal(t Token) []byte {
return []byte(MarshalString(t))
}
// MarshalString returns encoded Token as defied by the spec to string.
func MarshalString(t Token) string {
return escapeJoin([]string{
string(t.rawPayload),
escapeJoin([]string{
base64Encode(t.Signature),
base64Encode(t.HMACSignature),
}),
escapeJoin([]string{
base64Encode(t.PublicKey),
base64Encode(t.HMACSecret),
}),
t.Email,
})
}
func escapeJoin(s []string) string {
var escaped = make([]string, len(s))
for i := 0; i < len(s); i++ {
escaped[i] = strings.Replace(s[i], ",", "%2C", -1)
}
return strings.Join(escaped, ",")
}