Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
50a21d60
authored
2014-08-28 10:05:02 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
apiauth add more comments & improve
1 parent
5a087b28
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
9 deletions
plugins/apiauth/apiauth.go
plugins/auth/basic.go
plugins/apiauth/apiauth.go
View file @
50a21d6
...
...
@@ -21,10 +21,35 @@
//
// func main(){
// // apiauth every request
// beego.InsertFilter("*", beego.BeforeRouter,a
uth.API
Auth("appid","appkey"))
// beego.InsertFilter("*", beego.BeforeRouter,a
piauth.APIBaisc
Auth("appid","appkey"))
// beego.Run()
// }
//
// Advanced Usage:
//
// func getAppSecret(appid string) string {
// // get appsecret by appid
// // maybe store in configure, maybe in database
// }
//
// beego.InsertFilter("*", beego.BeforeRouter,apiauth.APIAuthWithFunc(getAppSecret, 360))
//
// in the request user should include these params in the query
//
// 1. appid
//
// appid is asigned to the application
//
// 2. signature
//
// get the signature use apiauth.Signature()
//
// >>> should use url.QueryEscape()
//
// 3. timestamp:
//
// send the request time, the format is yyyy-mm-dd HH:ii:ss
//
package
apiauth
import
(
...
...
@@ -34,7 +59,6 @@ import (
"fmt"
"net/url"
"sort"
"strings"
"time"
"github.com/astaxie/beego"
...
...
@@ -83,7 +107,7 @@ func APIAuthWithFunc(f AppIdToAppSecret, timeout int) beego.FilterFunc {
return
}
t
:=
time
.
Now
()
if
(
t
.
Second
()
-
u
.
Second
())
>
timeout
{
if
t
.
Sub
(
u
)
.
Seconds
()
>
float64
(
timeout
)
{
ctx
.
Output
.
SetStatus
(
403
)
ctx
.
WriteString
(
"timeout! the request time is long ago, please try again"
)
return
...
...
@@ -117,12 +141,7 @@ func Signature(appsecret, method string, params url.Values, RequestURI string) (
sha256
:=
sha256
.
New
hash
:=
hmac
.
New
(
sha256
,
[]
byte
(
appsecret
))
hash
.
Write
([]
byte
(
string_to_sign
))
sha
:=
base64
.
StdEncoding
.
EncodeToString
(
hash
.
Sum
(
nil
))
sha
=
url
.
QueryEscape
(
sha
)
sha
=
strings
.
Replace
(
sha
,
"+"
,
"%20"
,
-
1
)
sha
=
strings
.
Replace
(
sha
,
"*"
,
"%2A"
,
-
1
)
sha
=
strings
.
Replace
(
sha
,
"%7E"
,
"~"
,
-
1
)
return
sha
return
base64
.
StdEncoding
.
EncodeToString
(
hash
.
Sum
(
nil
))
}
type
valSorter
struct
{
...
...
plugins/auth/basic.go
View file @
50a21d6
...
...
@@ -27,6 +27,7 @@
//
//
// Advanced Usage:
//
// func SecretAuth(username, password string) bool {
// return username == "astaxie" && password == "helloBeego"
// }
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment