/
auth_rpc.go
178 lines (137 loc) · 3.66 KB
/
auth_rpc.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package auth_rpc
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/weedbox/common-modules/http_server"
"github.com/weedbox/websocket-modules/websocket_server"
"go.uber.org/fx"
"go.uber.org/zap"
)
type AuthenticateResponse struct {
Success bool `json:"success"`
Message bool `json:"message,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
}
type AuthRPC struct {
params Params
logger *zap.Logger
router *gin.RouterGroup
scope string
uri string
// Implementations
authenticator Authenticator
}
type Params struct {
fx.In
Lifecycle fx.Lifecycle
Logger *zap.Logger
HTTPServer *http_server.HTTPServer
WebSocketServer *websocket_server.WebSocketServer
}
type Option func(*AuthRPC)
func WithAuthenticator(a Authenticator) Option {
return func(arpc *AuthRPC) {
arpc.authenticator = a
}
}
func WithHTTPAuthenticator() Option {
return func(arpc *AuthRPC) {
arpc.authenticator = NewHTTPAuthenticator("")
}
}
func WithJWTAuthenticator() Option {
return func(arpc *AuthRPC) {
arpc.authenticator = NewJWTAuthenticator("")
}
}
func Module(scope string, uri string, opts ...Option) fx.Option {
var arpc *AuthRPC
return fx.Module(
scope,
fx.Provide(func(p Params) *AuthRPC {
arpc := &AuthRPC{
params: p,
logger: p.Logger.Named(scope),
scope: scope,
uri: uri,
authenticator: NewJWTAuthenticator(""),
}
arpc.initDefaultConfigs()
for _, o := range opts {
o(arpc)
}
return arpc
}),
fx.Populate(&arpc),
fx.Invoke(func(p Params) {
p.Lifecycle.Append(
fx.Hook{
OnStart: arpc.onStart,
OnStop: arpc.onStop,
},
)
}),
)
}
func (arpc *AuthRPC) getConfigPath(key string) string {
return fmt.Sprintf("%s.%s", arpc.scope, key)
}
func (arpc *AuthRPC) initDefaultConfigs() {
viper.SetDefault(arpc.getConfigPath("secret"), "")
viper.SetDefault(arpc.getConfigPath("auth_url"), "http://0.0.0.0/auth")
}
func (arpc *AuthRPC) onStart(ctx context.Context) error {
arpc.logger.Info("Starting Auth RPC",
zap.String("uri", arpc.uri),
)
// Set secret if it is JWT authenticator
if a, ok := arpc.authenticator.(*JWTAuthenticator); ok {
secret := viper.GetString(arpc.getConfigPath("secret"))
a.UpdateSecret(secret)
}
// Set url if it is HTTP authenticator
if a, ok := arpc.authenticator.(*HTTPAuthenticator); ok {
auth_url := viper.GetString(arpc.getConfigPath("auth_url"))
a.UpdateUrl(auth_url)
}
ep := arpc.params.WebSocketServer.GetEndpoint(arpc.uri)
if ep == nil {
return errors.New("Not found endpoint")
}
ep.GetAdapter().Register("Auth.Authenticate", arpc.authenticate)
return nil
}
func (arpc *AuthRPC) onStop(ctx context.Context) error {
arpc.logger.Info("Stopped Auth RPC")
return nil
}
func (arpc *AuthRPC) authenticate(c *websocket_server.Context) (interface{}, error) {
parameters := c.GetRequest().Params.([]interface{})
if len(parameters) != 1 {
return nil, websocket_server.NewError(websocket_server.ErrorCode_InvalidParams_Insufficient_Arguments, nil)
}
token, ok := parameters[0].(string)
if !ok {
return nil, websocket_server.NewError(websocket_server.ErrorCode_InvalidParams_Invalid_Arguments, nil)
}
if len(token) == 0 {
return nil, websocket_server.NewError(websocket_server.ErrorCode_InvalidParams, nil)
}
res := &AuthenticateResponse{
Success: true,
}
info, err := arpc.authenticator.Authenticate(token)
if err != nil {
res.Success = false
return res, nil
}
res.Data = make(map[string]interface{})
for k, v := range info.Data {
res.Data[k] = v
c.GetMeta().Set(k, v)
}
return res, nil
}