From f98ec91c4a2ecba1ddefb28bad7278d98d65ec7c Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 6 Sep 2019 20:25:41 +0800 Subject: [PATCH] auth --- examples/authrestrpc/auth_restrpc_example.go | 8 +++++-- examples/authrestrpc/auth_restrpc_test.go | 22 +++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/examples/authrestrpc/auth_restrpc_example.go b/examples/authrestrpc/auth_restrpc_example.go index 3d4ba0a..d2b85e8 100644 --- a/examples/authrestrpc/auth_restrpc_example.go +++ b/examples/authrestrpc/auth_restrpc_example.go @@ -1,6 +1,8 @@ package authrestrpc import ( + "strconv" + "github.com/qiniu/http/examples/auth/authstub" "github.com/qiniu/http/httputil" ) @@ -10,6 +12,7 @@ type fooInfo struct { A string `json:"a"'` B string `json:"b"` Id string `json:"id"` + Uid uint32 `json:"uid"` } // --------------------------------------------------------------------------- @@ -49,12 +52,13 @@ JSON {id: } */ 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 } @@ -73,7 +77,7 @@ JSON {a: , b: , foo: , id: } 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") diff --git a/examples/authrestrpc/auth_restrpc_test.go b/examples/authrestrpc/auth_restrpc_test.go index 40c0f60..6be0312 100644 --- a/examples/authrestrpc/auth_restrpc_test.go +++ b/examples/authrestrpc/auth_restrpc_test.go @@ -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 # @@ -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" @@ -67,7 +75,7 @@ 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 '{ @@ -75,7 +83,7 @@ func TestServer(t *testing.T) { }' 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" @@ -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"}'