access.go (1119B)
1 package generates 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/base64" 7 "strconv" 8 "strings" 9 10 "github.com/google/uuid" 11 "github.com/superseriousbusiness/oauth2/v4" 12 ) 13 14 // NewAccessGenerate create to generate the access token instance 15 func NewAccessGenerate() *AccessGenerate { 16 return &AccessGenerate{} 17 } 18 19 // AccessGenerate generate the access token 20 type AccessGenerate struct { 21 } 22 23 // Token based on the UUID generated token 24 func (ag *AccessGenerate) Token(ctx context.Context, data *oauth2.GenerateBasic, isGenRefresh bool) (string, string, error) { 25 buf := bytes.NewBufferString(data.Client.GetID()) 26 buf.WriteString(data.UserID) 27 buf.WriteString(strconv.FormatInt(data.CreateAt.UnixNano(), 10)) 28 29 access := base64.URLEncoding.EncodeToString([]byte(uuid.NewMD5(uuid.Must(uuid.NewRandom()), buf.Bytes()).String())) 30 access = strings.ToUpper(strings.TrimRight(access, "=")) 31 refresh := "" 32 if isGenRefresh { 33 refresh = base64.URLEncoding.EncodeToString([]byte(uuid.NewSHA1(uuid.Must(uuid.NewRandom()), buf.Bytes()).String())) 34 refresh = strings.ToUpper(strings.TrimRight(refresh, "=")) 35 } 36 37 return access, refresh, nil 38 }