summaryrefslogtreecommitdiff
path: root/vendor/github.com/dvsekhvalnov/jose2go/compact/compact.go
blob: 1e9dd2f51c80f117b14cb1d7ad68a6b3808e6f5d (plain)
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 compact provides function to work with json compact serialization format
package compact

import (
	"github.com/dvsekhvalnov/jose2go/base64url"
	"strings"
)

// Parse splitting & decoding compact serialized json web token, returns slice of byte arrays, each representing part of token
func Parse(token string) (result [][]byte, e error) {
	parts := strings.Split(token, ".")

	result = make([][]byte, len(parts))

	for i, part := range parts {
		if result[i], e = base64url.Decode(part); e != nil {
			return nil, e
		}
	}

	return result, nil
}

// Serialize converts given parts into compact serialization format
func Serialize(parts ...[]byte) string {
	result := make([]string, len(parts))

	for i, part := range parts {
		result[i] = base64url.Encode(part)
	}

	return strings.Join(result, ".")
}