Skip to content

Commit

Permalink
remove github.com/satori/go.uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Sep 7, 2018
1 parent 108a437 commit 8483442
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 9 deletions.
6 changes: 3 additions & 3 deletions generates/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strconv"
"strings"

"github.com/satori/go.uuid"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/utils/uuid"
)

// NewAccessGenerate create to generate the access token instance
Expand All @@ -25,10 +25,10 @@ func (ag *AccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool) (
buf.WriteString(data.UserID)
buf.WriteString(strconv.FormatInt(data.CreateAt.UnixNano(), 10))

access = base64.URLEncoding.EncodeToString(uuid.NewV3(uuid.Must(uuid.NewV4()), buf.String()).Bytes())
access = base64.URLEncoding.EncodeToString(uuid.NewMD5(uuid.Must(uuid.NewRandom()), buf.Bytes()).Bytes())
access = strings.ToUpper(strings.TrimRight(access, "="))
if isGenRefresh {
refresh = base64.URLEncoding.EncodeToString(uuid.NewV5(uuid.Must(uuid.NewV4()), buf.String()).Bytes())
refresh = base64.URLEncoding.EncodeToString(uuid.NewSHA1(uuid.Must(uuid.NewRandom()), buf.Bytes()).Bytes())
refresh = strings.ToUpper(strings.TrimRight(refresh, "="))
}

Expand Down
4 changes: 2 additions & 2 deletions generates/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/base64"
"strings"

"github.com/satori/go.uuid"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/utils/uuid"
)

// NewAuthorizeGenerate create to generate the authorize code instance
Expand All @@ -21,7 +21,7 @@ type AuthorizeGenerate struct{}
func (ag *AuthorizeGenerate) Token(data *oauth2.GenerateBasic) (code string, err error) {
buf := bytes.NewBufferString(data.Client.GetID())
buf.WriteString(data.UserID)
token := uuid.NewV3(uuid.Must(uuid.NewV1()), buf.String())
token := uuid.NewMD5(uuid.Must(uuid.NewRandom()), buf.Bytes())
code = base64.URLEncoding.EncodeToString(token.Bytes())
code = strings.ToUpper(strings.TrimRight(code, "="))

Expand Down
4 changes: 2 additions & 2 deletions generates/jwt_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"time"

"github.com/dgrijalva/jwt-go"
uuid "github.com/satori/go.uuid"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/errors"
"gopkg.in/oauth2.v3/utils/uuid"
)

// JWTAccessClaims jwt claims
Expand Down Expand Up @@ -55,7 +55,7 @@ func (a *JWTAccessGenerate) Token(data *oauth2.GenerateBasic, isGenRefresh bool)
}

if isGenRefresh {
refresh = base64.URLEncoding.EncodeToString(uuid.NewV5(uuid.Must(uuid.NewV4()), access).Bytes())
refresh = base64.URLEncoding.EncodeToString(uuid.NewSHA1(uuid.Must(uuid.NewRandom()), []byte(access)).Bytes())
refresh = strings.ToUpper(strings.TrimRight(refresh, "="))
}

Expand Down
1 change: 1 addition & 0 deletions store/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gopkg.in/oauth2.v3"
)

// NewClientStore create client store
func NewClientStore() *ClientStore {
return &ClientStore{
data: make(map[string]oauth2.ClientInfo),
Expand Down
5 changes: 3 additions & 2 deletions store/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"time"

"github.com/satori/go.uuid"
"github.com/tidwall/buntdb"
"gopkg.in/oauth2.v3"
"gopkg.in/oauth2.v3/models"
"gopkg.in/oauth2.v3/utils/uuid"
)

// NewMemoryTokenStore create a token store instance based on memory
Expand Down Expand Up @@ -43,7 +43,8 @@ func (ts *TokenStore) Create(info oauth2.TokenInfo) (err error) {
_, _, err = tx.Set(code, string(jv), &buntdb.SetOptions{Expires: true, TTL: info.GetCodeExpiresIn()})
return
}
basicID := uuid.Must(uuid.NewV4()).String()

basicID := uuid.Must(uuid.NewRandom()).String()
aexp := info.GetAccessExpiresIn()
rexp := aexp
if refresh := info.GetRefresh(); refresh != "" {
Expand Down
105 changes: 105 additions & 0 deletions utils/uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package uuid

import (
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"hash"
"io"
)

// Nil empty UUID, all zeros
var Nil UUID

// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
// 4122.
type UUID [16]byte

// Bytes returns bytes slice representation of UUID.
func (uuid UUID) Bytes() []byte {
return uuid[:]
}

// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// , or "" if uuid is invalid.
func (uuid UUID) String() string {
var buf [36]byte
encodeHex(buf[:], uuid)
return string(buf[:])
}

func encodeHex(dst []byte, uuid UUID) {
hex.Encode(dst, uuid[:4])
dst[8] = '-'
hex.Encode(dst[9:13], uuid[4:6])
dst[13] = '-'
hex.Encode(dst[14:18], uuid[6:8])
dst[18] = '-'
hex.Encode(dst[19:23], uuid[8:10])
dst[23] = '-'
hex.Encode(dst[24:], uuid[10:])
}

// Must returns uuid if err is nil and panics otherwise.
func Must(uuid UUID, err error) UUID {
if err != nil {
panic(err)
}
return uuid
}

// NewRandom returns a Random (Version 4) UUID.
//
// The strength of the UUIDs is based on the strength of the crypto/rand
// package.
//
// A note about uniqueness derived from the UUID Wikipedia entry:
//
// Randomly generated UUIDs have 122 random bits. One's annual risk of being
// hit by a meteorite is estimated to be one chance in 17 billion, that
// means the probability is about 0.00000000006 (6 × 10−11),
// equivalent to the odds of creating a few tens of trillions of UUIDs in a
// year and having one duplicate.
func NewRandom() (UUID, error) {
var uuid UUID
_, err := io.ReadFull(rand.Reader, uuid[:])
if err != nil {
return Nil, err
}
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
return uuid, nil
}

// NewHash returns a new UUID derived from the hash of space concatenated with
// data generated by h. The hash should be at least 16 byte in length. The
// first 16 bytes of the hash are used to form the UUID. The version of the
// UUID will be the lower 4 bits of version.
func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID {
h.Reset()
h.Write(space[:])
h.Write(data)
s := h.Sum(nil)
var uuid UUID
copy(uuid[:], s)
uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4)
uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant
return uuid
}

// NewMD5 returns a new MD5 (Version 3) UUID based on the
// supplied name space and data. It is the same as calling:
//
// NewHash(md5.New(), space, data, 3)
func NewMD5(space UUID, data []byte) UUID {
return NewHash(md5.New(), space, data, 3)
}

// NewSHA1 returns a new SHA1 (Version 5) UUID based on the
// supplied name space and data. It is the same as calling:
//
// NewHash(sha1.New(), space, data, 5)
func NewSHA1(space UUID, data []byte) UUID {
return NewHash(sha1.New(), space, data, 5)
}

0 comments on commit 8483442

Please sign in to comment.