Refactor TokenGen
This change updates our TokenGen struct (generates both authorize
and access tokens) to use the new utility crypto library for the
OAuth server.
package osinserver
import (
- "encoding/base64"
"strings"
- "crypto/rand"
-
"github.com/RangelReale/osin"
+
+ "github.com/openshift/origin/pkg/oauthserver/server/crypto"
+)
+
+var (
+ _ osin.AuthorizeTokenGen = TokenGen{}
+ _ osin.AccessTokenGen = TokenGen{}
)
-func randomBytes(len int) []byte {
- b := make([]byte, len)
- if _, err := rand.Read(b); err != nil {
- // rand.Read should never fail
- panic(err)
- }
- return b
-}
func randomToken() string {
for {
- // 32 bytes (256 bits) = 43 base64-encoded characters
- b := randomBytes(32)
- // Use URLEncoding to ensure we don't get / characters
- s := base64.URLEncoding.EncodeToString(b)
+ // guaranteed to have no / characters and no trailing ='s
+ token := crypto.Random256BitsString()
+
// Don't generate tokens with leading dashes... they're hard to use on the command line
- if strings.HasPrefix(s, "-") {
+ if strings.HasPrefix(token, "-") {
continue
}
- // Strip trailing ='s... they're ugly
- return strings.TrimRight(s, "=")
+
+ return token
}
}
-// TokenGen is an authorization and access token generator
-type TokenGen struct {
-}
+type TokenGen struct{}
-// GenerateAuthorizeToken generates a random UUID code
func (TokenGen) GenerateAuthorizeToken(data *osin.AuthorizeData) (ret string, err error) {
return randomToken(), nil
}
-// GenerateAccessToken generates random UUID access and refresh tokens
func (TokenGen) GenerateAccessToken(data *osin.AccessData, generaterefresh bool) (string, string, error) {
accesstoken := randomToken()
@@ -49,5 +40,6 @@ func (TokenGen) GenerateAccessToken(data *osin.AccessData, generaterefresh bool)
if generaterefresh {
refreshtoken = randomToken()
}
+
return accesstoken, refreshtoken, nil
}
