2014-12-17 06:58:39 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2014-12-17 18:57:05 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker-registry/common"
|
2014-12-17 06:58:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// joseBase64UrlEncode encodes the given data using the standard base64 url
|
|
|
|
// encoding format but with all trailing '=' characters ommitted in accordance
|
|
|
|
// with the jose specification.
|
|
|
|
// http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-31#section-2
|
|
|
|
func joseBase64UrlEncode(b []byte) string {
|
|
|
|
return strings.TrimRight(base64.URLEncoding.EncodeToString(b), "=")
|
|
|
|
}
|
|
|
|
|
|
|
|
// joseBase64UrlDecode decodes the given string using the standard base64 url
|
|
|
|
// decoder but first adds the appropriate number of trailing '=' characters in
|
|
|
|
// accordance with the jose specification.
|
|
|
|
// http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-31#section-2
|
|
|
|
func joseBase64UrlDecode(s string) ([]byte, error) {
|
|
|
|
switch len(s) % 4 {
|
|
|
|
case 0:
|
|
|
|
case 2:
|
|
|
|
s += "=="
|
|
|
|
case 3:
|
|
|
|
s += "="
|
|
|
|
default:
|
|
|
|
return nil, errors.New("illegal base64url string")
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.DecodeString(s)
|
|
|
|
}
|
|
|
|
|
2014-12-17 18:57:05 +00:00
|
|
|
// actionSet is a special type of stringSet.
|
|
|
|
type actionSet struct {
|
|
|
|
common.StringSet
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 18:57:05 +00:00
|
|
|
func newActionSet(actions ...string) actionSet {
|
|
|
|
return actionSet{common.NewStringSet(actions...)}
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 18:57:05 +00:00
|
|
|
// Contains calls StringSet.Contains() for
|
2014-12-17 06:58:39 +00:00
|
|
|
// either "*" or the given action string.
|
2014-12-17 18:57:05 +00:00
|
|
|
func (s actionSet) Contains(action string) bool {
|
|
|
|
return s.StringSet.Contains("*") || s.StringSet.Contains(action)
|
2014-12-17 06:58:39 +00:00
|
|
|
}
|