2023-03-27

Signed-off-by: Yuan Si <do4suki@gmail.com>
This commit is contained in:
2023-03-26 22:21:33 +08:00
parent c4da6c74f8
commit 966606afc2
19 changed files with 804 additions and 77 deletions
+50
View File
@@ -6,8 +6,11 @@ package utils
import (
"encoding/base64"
"encoding/hex"
"errors"
"math/rand"
"net/url"
"os"
"strconv"
"time"
)
@@ -58,3 +61,50 @@ func RandomBase64Token(len int) string {
return base64.RawStdEncoding.EncodeToString([]byte(mRandStr))
}
func ParseAuthorizeStr(authorize []string) (url.Values, error) {
if len(authorize) == 0 {
return nil, errors.New("authorize is null")
}
urlParams, err := url.ParseQuery(authorize[0])
if err != nil {
return nil, err
}
return urlParams, nil
}
func GetAuthorizeToken(authorize []string) (string, error) {
params, err := ParseAuthorizeStr(authorize)
if err != nil {
return "", err
}
token := params["token"]
if len(token) == 0 {
return "", errors.New("token is null")
}
return token[0], nil
}
func GetAuthorizeNonce(authorize []string) (int, error) {
params, err := ParseAuthorizeStr(authorize)
if err != nil {
return 0, err
}
nonce := params["nonce"]
if len(nonce) == 0 {
return 0, errors.New("nonce is null")
}
n_nonce, err := strconv.Atoi(nonce[0])
if err != nil {
return 0, err
}
if n_nonce == 0 {
return 0, errors.New("nonce is 0")
}
return n_nonce, nil
}