Adds an internal library for JWS / JWT encoding
This commit is contained in:
32
jwt/base64.go
Normal file
32
jwt/base64.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Base64Encode encodes a payload of data as a string, like specified
|
||||
// in RFC 7515 (no trailing '='
|
||||
func Base64Encode(b []byte) string {
|
||||
return strings.TrimRight(base64.URLEncoding.EncodeToString(b), "=")
|
||||
}
|
||||
|
||||
// Base64Decode decodes a payload of data as a []byte, like specified
|
||||
// in RFC 7515 (no trailing '=')
|
||||
func Base64Decode(s string) ([]byte, error) {
|
||||
switch len(s) % 4 {
|
||||
case 2:
|
||||
s += "=="
|
||||
case 3:
|
||||
s += "="
|
||||
case 1:
|
||||
return nil, fmt.Errorf("jwt: Invalid base64 string (length:%d %% 4 == 1): '%s'", len(s), s)
|
||||
}
|
||||
d, err := base64.URLEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("jwt: %s", err)
|
||||
}
|
||||
return d, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user