Skip to content

Commit

Permalink
auth
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Sep 6, 2019
1 parent 0f8abf4 commit f98ec91
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
8 changes: 6 additions & 2 deletions examples/authrestrpc/auth_restrpc_example.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package authrestrpc

import (
"strconv"

"github.com/qiniu/http/examples/auth/authstub"
"github.com/qiniu/http/httputil"
)
Expand All @@ -10,6 +12,7 @@ type fooInfo struct {
A string `json:"a"'`
B string `json:"b"`
Id string `json:"id"`
Uid uint32 `json:"uid"`
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -49,12 +52,13 @@ JSON {id: <FooId>}
*/
func (p *Service) PostFoo_Bar(args *fooBarArgs, env *authstub.Env) (ret fooBarRet, err error) {

id := args.A + "." + args.B
id := strconv.Itoa(int(env.Uid)) + "." + args.A + "." + args.B
p.foos[id] = fooInfo{
Foo: args.CmdArgs[0],
A: args.A,
B: args.B,
Id: id,
Uid: env.Uid,
}
return fooBarRet{Id: id}, nil
}
Expand All @@ -73,7 +77,7 @@ JSON {a: <A>, b: <B>, foo: <Foo>, id: <FooId>}
func (p *Service) GetFoo_(args *reqArgs, env *authstub.Env) (ret fooInfo, err error) {

id := args.CmdArgs[0]
if foo, ok := p.foos[id]; ok {
if foo, ok := p.foos[id]; ok && foo.Uid == env.Uid {
return foo, nil
}
err = httputil.NewError(404, "id not found")
Expand Down
22 changes: 15 additions & 7 deletions examples/authrestrpc/auth_restrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestServer(t *testing.T) {
ctx.Exec(
`
host foo.com 192.168.0.10:9898
auth testuser |authstub -uid 1 -utype 4|
auth testuser1 |authstub -uid 1 -utype 4|
auth testuser2 |authstub -uid 2 -utype 4|
#this is a comment
#
Expand All @@ -47,18 +48,25 @@ func TestServer(t *testing.T) {
json '{"error":"bad token"}'
post http://foo.com/v1/foo/foo123/bar
auth testuser
auth testuser1
json '{"a": "1", "b": "2"}'
ret 200
json '{"id": $(id)}'
get http://foo.com/v1/foo/$(id)
auth testuser
auth testuser2
ret 404
json '{
"error": "id not found"
}'
get http://foo.com/v1/foo/$(id)
auth testuser1
ret 200
json '{"id": $(id), "foo": "foo123", "a": "1", "b": "2"}'
get http://foo.com/v1/foo/1.3
auth testuser
auth testuser1
ret 404
json '{
"error": "id not found"
Expand All @@ -67,15 +75,15 @@ func TestServer(t *testing.T) {
match $(abcd) 4578
println \n|base64 $(abcd)|
post http://foo.com/v1/foo/|base64 $(abcd)|/bar
auth testuser
auth testuser1
form a=$(id)&b=3
ret 200
json '{
"id": $(id2)
}'
post http://foo.com/v1/hosts/192.168.3.1
auth testuser
auth testuser1
json '{
"foo.com": "127.0.0.1",
"bar.com": "192.168.4.10"
Expand All @@ -91,7 +99,7 @@ func TestServer(t *testing.T) {
}'
get http://foo.com/v1/foo/$(id2)
auth testuser
auth testuser1
ret 200
json '{"foo": $(foo), "a": $(id), "b": "3"}'
Expand Down

0 comments on commit f98ec91

Please sign in to comment.