mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 08:00:57 +00:00
1.simplify reading and writing file code
2.add apiauth test
This commit is contained in:
parent
872a964145
commit
db67ffbb94
25
cache/file.go
vendored
25
cache/file.go
vendored
@ -22,6 +22,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
@ -222,33 +223,13 @@ func exists(path string) (bool, error) {
|
||||
// FileGetContents Get bytes to file.
|
||||
// if non-exist, create this file.
|
||||
func FileGetContents(filename string) (data []byte, e error) {
|
||||
f, e := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
stat, e := f.Stat()
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
data = make([]byte, stat.Size())
|
||||
result, e := f.Read(data)
|
||||
if e != nil || int64(result) != stat.Size() {
|
||||
return nil, e
|
||||
}
|
||||
return
|
||||
return ioutil.ReadFile(filename)
|
||||
}
|
||||
|
||||
// FilePutContents Put bytes to file.
|
||||
// if non-exist, create this file.
|
||||
func FilePutContents(filename string, content []byte) error {
|
||||
fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fp.Close()
|
||||
_, err = fp.Write(content)
|
||||
return err
|
||||
return ioutil.WriteFile(filename, content, os.ModePerm)
|
||||
}
|
||||
|
||||
// GobEncode Gob encodes file cache item.
|
||||
|
@ -56,6 +56,7 @@
|
||||
package apiauth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
@ -128,53 +129,32 @@ func APISecretAuth(f AppIDToAppSecret, timeout int) beego.FilterFunc {
|
||||
|
||||
// Signature used to generate signature with the appsecret/method/params/RequestURI
|
||||
func Signature(appsecret, method string, params url.Values, RequestURL string) (result string) {
|
||||
var query string
|
||||
var b bytes.Buffer
|
||||
keys := make([]string, len(params))
|
||||
pa := make(map[string]string)
|
||||
for k, v := range params {
|
||||
pa[k] = v[0]
|
||||
keys = append(keys, k)
|
||||
}
|
||||
vs := mapSorter(pa)
|
||||
vs.Sort()
|
||||
for i := 0; i < vs.Len(); i++ {
|
||||
if vs.Keys[i] == "signature" {
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, key := range keys {
|
||||
if key == "signature" {
|
||||
continue
|
||||
}
|
||||
if vs.Keys[i] != "" && vs.Vals[i] != "" {
|
||||
query = fmt.Sprintf("%v%v%v", query, vs.Keys[i], vs.Vals[i])
|
||||
|
||||
val := pa[key]
|
||||
if key != "" && val != "" {
|
||||
b.WriteString(key)
|
||||
b.WriteString(val)
|
||||
}
|
||||
}
|
||||
stringToSign := fmt.Sprintf("%v\n%v\n%v\n", method, query, RequestURL)
|
||||
|
||||
stringToSign := fmt.Sprintf("%v\n%v\n%v\n", method, b.String(), RequestURL)
|
||||
|
||||
sha256 := sha256.New
|
||||
hash := hmac.New(sha256, []byte(appsecret))
|
||||
hash.Write([]byte(stringToSign))
|
||||
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
type valSorter struct {
|
||||
Keys []string
|
||||
Vals []string
|
||||
}
|
||||
|
||||
func mapSorter(m map[string]string) *valSorter {
|
||||
vs := &valSorter{
|
||||
Keys: make([]string, 0, len(m)),
|
||||
Vals: make([]string, 0, len(m)),
|
||||
}
|
||||
for k, v := range m {
|
||||
vs.Keys = append(vs.Keys, k)
|
||||
vs.Vals = append(vs.Vals, v)
|
||||
}
|
||||
return vs
|
||||
}
|
||||
|
||||
func (vs *valSorter) Sort() {
|
||||
sort.Sort(vs)
|
||||
}
|
||||
|
||||
func (vs *valSorter) Len() int { return len(vs.Keys) }
|
||||
func (vs *valSorter) Less(i, j int) bool { return vs.Keys[i] < vs.Keys[j] }
|
||||
func (vs *valSorter) Swap(i, j int) {
|
||||
vs.Vals[i], vs.Vals[j] = vs.Vals[j], vs.Vals[i]
|
||||
vs.Keys[i], vs.Keys[j] = vs.Keys[j], vs.Keys[i]
|
||||
}
|
||||
|
20
plugins/apiauth/apiauth_test.go
Normal file
20
plugins/apiauth/apiauth_test.go
Normal file
@ -0,0 +1,20 @@
|
||||
package apiauth
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSignature(t *testing.T) {
|
||||
appsecret := "beego secret"
|
||||
method := "GET"
|
||||
RequestURL := "http://localhost/test/url"
|
||||
params := make(url.Values)
|
||||
params.Add("arg1", "hello")
|
||||
params.Add("arg2", "beego")
|
||||
|
||||
signature := "mFdpvLh48ca4mDVEItE9++AKKQ/IVca7O/ZyyB8hR58="
|
||||
if Signature(appsecret, method, params, RequestURL) != signature {
|
||||
t.Error("Signature error")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user