forked from lujiacn/revauth_v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevauth.go
180 lines (153 loc) · 4.63 KB
/
revauth.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
179
180
package revauth
import (
"context"
"crypto/tls"
"fmt"
"log"
"net/url"
"path"
"strings"
"github.com/lujiacn/mgodo"
"github.com/lujiacn/revauth_v1/app/models"
gAuth "github.com/lujiacn/revauth_v1/auth"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/revel/revel"
)
var (
grpcDial string
grpcAuthConnect string
grpcAuthCertPath string
conn *grpc.ClientConn // keep connection
)
// Init reading LDAP configuration
func Init() {
// update grpcauth server and port to grpc://connection_string
var found bool
var grpcAuthHost string
var grpcAuthPort string
// compatible with previouse setting, check grpcauth.connect if not found
// check grpcauth.host and grpcauth.port
if grpcAuthConnect, found = revel.Config.String("grpcauth.connect"); !found {
if grpcAuthHost, found = revel.Config.String("grpcauth.server"); !found {
panic("grpcauth connection or server not defined")
}
grpcAuthPort = revel.Config.StringDefault("grpcauth.port", "50051")
if grpcAuthCertPath, found = revel.Config.String("grpcauth.cert.path"); found {
grpcAuthConnect = fmt.Sprintf("grpcs://%s:%s", grpcAuthHost, grpcAuthPort)
} else {
grpcAuthConnect = fmt.Sprintf("grpc://%s:%s", grpcAuthHost, grpcAuthPort)
}
} else {
grpcAuthCertPath, _ = revel.Config.String("grpcauth.cert.path")
}
connect()
}
func connect() {
fmt.Println("Debug grpcauth", grpcAuthConnect)
// parse connection scheme
h, err := url.Parse(grpcAuthConnect)
if err != nil {
panic("Invalid connection format. eg: grpc://host:port/path")
}
if h.Scheme == "grpc" || h.Scheme == "" {
conn, err = grpc.Dial(path.Join(h.Host, h.Path), grpc.WithInsecure())
if err != nil {
revel.AppLog.Critf("%v", err)
}
}
if h.Scheme == "grpcs" {
var creds credentials.TransportCredentials
if grpcAuthCertPath == "" {
// client will not verify the server certificate, may cause man-in-middle attacks
config := &tls.Config{
InsecureSkipVerify: true,
}
creds = credentials.NewTLS(config)
log.Printf("grpcauth.cert.path is empty in app.conf, please specify the path to the cert file to make connection more secure")
} else {
tlsServerNameOverride := revel.Config.StringDefault("grpcauth.cert.cn", "")
creds, err = credentials.NewClientTLSFromFile(grpcAuthCertPath, tlsServerNameOverride)
if err != nil {
revel.AppLog.Critf("%v", err)
panic("failed to process the credentials")
}
}
conn, err = grpc.Dial(path.Join(h.Host, h.Path), grpc.WithTransportCredentials(creds))
if err != nil {
revel.AppLog.Critf("%v", err)
}
}
}
// Authenticate do auth and return Auth object including user information and lognin success or not
func Authenticate(account, password string) *gAuth.AuthReply {
if conn == nil {
connect()
}
c := gAuth.NewAuthClient(conn)
r, err := c.Authenticate(context.Background(), &gAuth.AuthRequest{Account: account, Password: password})
if err != nil {
return &gAuth.AuthReply{Error: fmt.Sprintf("Authenticate failed due to %v ", err)}
}
return r
}
func Query(account string) *gAuth.QueryReply {
if conn == nil {
connect()
}
c := gAuth.NewAuthClient(conn)
r, err := c.Query(context.Background(), &gAuth.QueryRequest{Account: account})
if err != nil {
return &gAuth.QueryReply{Error: fmt.Sprintf("User not found: %v ", err)}
}
return r
}
func QueryMail(email string) *gAuth.QueryReply {
if conn == nil {
connect()
}
c := gAuth.NewAuthClient(conn)
r, err := c.Query(context.Background(), &gAuth.QueryRequest{Email: email})
if err != nil {
return &gAuth.QueryReply{Error: fmt.Sprintf("User not found: %v ", err)}
}
return r
}
func QueryMailAndSave(email string) (*models.User, error) {
authUser := QueryMail(email)
if authUser.Error != "" && authUser.Error != "<nil>" {
return nil, fmt.Errorf(authUser.Error)
}
if authUser.NotExist {
return nil, fmt.Errorf("User not exist")
}
user := new(models.User)
user.Identity = strings.ToLower(authUser.Account)
user.Mail = authUser.Email
user.Avatar = authUser.Avatar
user.Name = authUser.Name
user.Depart = authUser.Depart
s := mgodo.NewMgoSession()
defer s.Close()
user.SaveUser(s)
return user, nil
}
func QueryAndSave(account string) (*models.User, error) {
authUser := Query(account)
if authUser.Error != "" && authUser.Error != "<nil>" {
return nil, fmt.Errorf(authUser.Error)
}
if authUser.NotExist {
return nil, fmt.Errorf("User not exist")
}
user := new(models.User)
user.Identity = strings.ToLower(account)
user.Mail = authUser.Email
user.Avatar = authUser.Avatar
user.Name = authUser.Name
user.Depart = authUser.Depart
s := mgodo.NewMgoSession()
defer s.Close()
user.SaveUser(s)
return user, nil
}