forked from smallnest/rpcx
/
authorization.go
93 lines (78 loc) · 2.71 KB
/
authorization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package rpcx
import (
"bytes"
"encoding/gob"
"net/rpc"
)
// AuthorizationServerPlugin is used to authorize clients.
type AuthorizationServerPlugin struct {
AuthorizationFunc AuthorizationFunc
}
// AuthorizationFunc defines a method type which handles Authorization info
type AuthorizationFunc func(p *AuthorizationAndServiceMethod) error
// AuthorizationAndServiceMethod represents Authorization header and ServiceMethod.
type AuthorizationAndServiceMethod struct {
Authorization string // Authorization
ServiceMethod string // real ServiceMethod name
Tag string // extra tag for Authorization
}
func init() {
// This type must match exactly what youre going to be using,
// down to whether or not its a pointer
gob.Register(&AuthorizationAndServiceMethod{})
}
// PostReadRequestHeader extracts Authorization header from ServiceMethod field.
func (plugin *AuthorizationServerPlugin) PostReadRequestHeader(r *rpc.Request) (err error) {
b := bytes.NewBufferString(r.ServiceMethod)
var aAndS AuthorizationAndServiceMethod
dec := gob.NewDecoder(b)
err = dec.Decode(&aAndS)
if err == nil {
r.ServiceMethod = aAndS.ServiceMethod
if plugin.AuthorizationFunc != nil {
err = plugin.AuthorizationFunc(&aAndS)
}
}
return
}
// Name return name of this plugin.
func (plugin *AuthorizationServerPlugin) Name() string {
return "AuthorizationServerPlugin"
}
// Description return description of this plugin.
func (plugin *AuthorizationServerPlugin) Description() string {
return "a Authorization plugin"
}
// AuthorizationClientPlugin is used to set Authorization info at client side.
type AuthorizationClientPlugin struct {
AuthorizationAndServiceMethod *AuthorizationAndServiceMethod
}
// NewAuthorizationClientPlugin creates a AuthorizationClientPlugin with authorization header and tag
func NewAuthorizationClientPlugin(authorization, tag string) *AuthorizationClientPlugin {
return &AuthorizationClientPlugin{
AuthorizationAndServiceMethod: &AuthorizationAndServiceMethod{
Authorization: authorization,
Tag: tag,
},
}
}
// PreWriteRequest adds Authorization info in requests
func (plugin *AuthorizationClientPlugin) PreWriteRequest(r *rpc.Request, body interface{}) error {
var b bytes.Buffer
enc := gob.NewEncoder(&b)
plugin.AuthorizationAndServiceMethod.ServiceMethod = r.ServiceMethod
err := enc.Encode(plugin.AuthorizationAndServiceMethod)
if err != nil {
return err
}
r.ServiceMethod = b.String()
return nil
}
// Name return name of this plugin.
func (plugin *AuthorizationClientPlugin) Name() string {
return "AuthorizationClientPlugin"
}
// Description return description of this plugin.
func (plugin *AuthorizationClientPlugin) Description() string {
return "a Authorization plugin"
}