This repository was archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
143 lines (126 loc) · 3.2 KB
/
auth.go
File metadata and controls
143 lines (126 loc) · 3.2 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/antmanler/gnatsd-jwt/jwtauth"
"github.com/garyburd/redigo/redis"
"github.com/nats-io/gnatsd/server"
)
type customAuther interface {
server.Authentication
SetLogger(logger jwtauth.Logger)
}
type refuncAuth struct {
pool *redis.Pool
tokenAuther customAuther
logger jwtauth.Logger
}
// credSyncer syncs and manages funcinsts.
type credSyncer struct {
}
// static assert
var _ server.Authentication = (*refuncAuth)(nil)
func (auth *refuncAuth) Check(c server.ClientAuthentication) (verified bool) {
opts := c.GetOpts()
var username, password string
if opts.Authorization != "" {
if auth.tokenAuther == nil {
return
}
token, err := auth.tokenAuther.(*jwtauth.JWTAuth).Verify(opts.Authorization, &tokenExt{})
if err != nil {
auth.Errorf("failed to auth token, %v", err)
return
}
claims, ok := token.Claims.(*tokenExt)
if !ok {
return
}
if claims.AccessKeyRef == "" {
user := auth.tokenAuther.(*jwtauth.JWTAuth).GetUser(&claims.Token)
if user == nil {
return
}
auth.Debugf("Verified user %q by token, with perms %v", user.Username, user.Permissions != nil)
c.RegisterUser(user)
return true
}
username, password = claims.AccessKeyRef, claims.AccessKeyRef
} else {
username, password = opts.Username, opts.Password
}
if username == "" || password == "" {
return
}
user, err := auth.Get(username)
if err != nil {
auth.Errorf("Failed to get creds for %q, %v", username, err)
return
}
if user.Password != password {
return
}
c.RegisterUser(user)
auth.Debugf("Register user %q with permissions %v", user.Username, user.Permissions != nil)
return true
}
func (auth *refuncAuth) Get(key string) (*server.User, error) {
c := auth.pool.Get()
defer c.Close()
reply, err := c.Do("GET", key)
if err != nil {
return nil, err
}
bts, ok := reply.([]byte)
if !ok {
return nil, fmt.Errorf("value for %q is not bytes", key)
}
var user struct {
ID string `json:"id,omitempty"`
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
Permissions *server.Permissions `json:"permissions"`
}
if err := json.Unmarshal(bts, &user); err != nil {
return nil, err
}
if user.AccessKey != key {
return nil, fmt.Errorf("key not match %q != %q", user.AccessKey, key)
}
natsUser := &server.User{
Password: user.SecretKey,
Permissions: user.Permissions,
}
if user.ID != "" {
natsUser.Username = user.ID
} else {
natsUser.Username = user.AccessKey
}
return natsUser, nil
}
// SetLogger set logger
func (auth *refuncAuth) SetLogger(logger jwtauth.Logger) {
if auth.tokenAuther != nil {
auth.tokenAuther.SetLogger(logger)
}
auth.logger = logger
}
// Errorf for error logs
func (auth *refuncAuth) Errorf(format string, v ...interface{}) {
if auth.logger != nil {
auth.logger.Errorf(format, v...)
}
}
// Debugf for debug log
func (auth *refuncAuth) Debugf(format string, v ...interface{}) {
if auth.logger != nil {
auth.logger.Debugf(format, v...)
}
}
type tokenExt struct {
jwtauth.Token
AccessKeyRef string `json:"ref,omitempty"`
}
func (u tokenExt) Valid() error {
return u.Token.Valid()
}