forked from as/signer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.go
More file actions
33 lines (27 loc) · 767 Bytes
/
token.go
File metadata and controls
33 lines (27 loc) · 767 Bytes
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
package signer
import "encoding/base64"
var (
codec = base64.RawURLEncoding
)
// Token is a byte slice that knows how to marshal and unmarshal itself in base64
type Token []byte
// String returns a url-safe base64-encoded token
func (t Token) String() string {
s, _ := t.MarshalText()
return string(s)
}
// MarshalText returns a url-safe base64-encoded token as a byte slice
func (t Token) MarshalText() ([]byte, error) {
dst := make([]byte, codec.EncodedLen(len(t)))
codec.Encode(dst, t)
return dst, nil
}
// UnmarshalText decodes a url-safe base64-encoded token
func (t *Token) UnmarshalText(p []byte) error {
n := codec.DecodedLen(len(p))
if len(*t) < n {
*t = append(*t, make([]byte, n-len(*t))...)
}
_, err := codec.Decode(*t, p)
return err
}