auth.go
10.8 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/auth/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/auth/query"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
"math/rand"
"strconv"
"strings"
)
// 认证服务
type AuthService struct {
}
// 用户按公司登录
func (authService *AuthService) AccessToken(accessTokenCommand *command.AccessTokenCommand) (interface{}, error) {
if err := accessTokenCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var claim = &domain.UserTokenClaim{}
var result bool
if result, err = domain.ValidToken(accessTokenCommand.AuthCode, claim); err != nil || !result {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
userId := claim.UserId
companyId := claim.CompanyId
rspMapData := map[string]interface{}{
"accessToken": domain.SignToken(int64(userId), companyId),
"refreshToken": domain.SignToken(int64(userId), companyId),
}
return rspMapData, nil
}
// 修改手机号密码
func (authService *AuthService) ChangePassword(changePasswordCommand *command.ChangePasswordCommand) (interface{}, error) {
if err := changePasswordCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
userAuth *domain.UserAuth
)
UserAuthRepository, _ := factory.CreateUserAuthRepository(map[string]interface{}{"transactionContext": transactionContext})
userAuth, err = UserAuthRepository.FindOne(map[string]interface{}{"phone": changePasswordCommand.Phone})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if !strings.EqualFold(userAuth.PhoneAuth.Password, changePasswordCommand.OldPwd) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "密码有误!")
}
if err = userAuth.Update(map[string]interface{}{"password": changePasswordCommand.NewPwd}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if _, err = UserAuthRepository.Save(userAuth); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 用户登录 返回有权限的公司列表
func (authService *AuthService) Login(loginQuery *query.LoginQuery) (interface{}, error) {
if err := loginQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
userAuth *domain.UserAuth
users []*domain.User
rspMapData = make(map[string]interface{})
userCompanies []interface{}
)
UserAuthRepository, _ := factory.CreateUserAuthRepository(map[string]interface{}{"transactionContext": transactionContext})
UserRepository, _ := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
switch loginQuery.GrantType {
case "signInPassword":
if len(loginQuery.Password) == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "密码不能为空!")
}
userAuth, err = UserAuthRepository.FindOne(map[string]interface{}{"phone": loginQuery.Phone})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if !strings.EqualFold(userAuth.PhoneAuth.Password, loginQuery.Password) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "密码有误!")
}
case "signInCaptcha":
case "signInCredentials":
default:
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "undefined grantType:"+loginQuery.GrantType)
}
_, users, err = UserRepository.Find(map[string]interface{}{"inUserIds": userAuth.Users, "status": domain.StatusEnable})
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
credentials := "cred:" + strconv.Itoa(rand.Int())
rspMapData["credentials"] = credentials
CompanyRepository, _ := factory.CreateCompanyRepository(map[string]interface{}{"transactionContext": transactionContext})
for i := range users {
company, _ := CompanyRepository.FindOne(map[string]interface{}{"companyId": users[i].CompanyId, "status": domain.StatusEnable})
if company == nil {
continue
}
item := map[string]interface{}{
"user": users[i],
"company": company,
}
userCompanies = append(userCompanies, item)
}
return map[string]interface{}{"userCompanies": userCompanies, "credentials": credentials}, nil
}
// 用户按公司登录
func (authService *AuthService) LoginByCompany(loginByCompanyCommand *command.LoginByCompanyCommand) (interface{}, error) {
if err := loginByCompanyCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
//todo:valid/refresh credentials
if len(loginByCompanyCommand.Credentials) == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "credentials expire")
}
var user *domain.User
UserRepository, _ := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
user, err = UserRepository.FindOne(map[string]interface{}{"userId": loginByCompanyCommand.UserId, "userType": loginByCompanyCommand.UserType})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
CompanyRepository, _ := factory.CreateCompanyRepository(map[string]interface{}{"transactionContext": transactionContext})
company, err := CompanyRepository.FindOne(map[string]interface{}{"companyId": user.CompanyId, "status": domain.StatusEnable})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
authCode := domain.SignToken(user.UserId, company.CompanyId)
return map[string]interface{}{"user": user, "company": company, "authCode": authCode}, nil
}
// 更新授权令牌accessToken
func (authService *AuthService) RefreshToken(refreshTokenCommand *command.RefreshTokenCommand) (interface{}, error) {
if err := refreshTokenCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var claim = &domain.UserTokenClaim{}
var result bool
if result, err = domain.ValidToken(refreshTokenCommand.RefreshToken, claim); err != nil || !result {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
userId := claim.UserId
companyId := claim.CompanyId
rspMapData := map[string]interface{}{
"accessToken": domain.SignToken(int64(userId), companyId),
"refreshToken": domain.SignToken(int64(userId), companyId),
}
return rspMapData, nil
}
// 注销登录
func (authService *AuthService) Revoke(revokeCommand *command.RevokeCommand) (interface{}, error) {
if err := revokeCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 发送验证码
func (authService *AuthService) SendSmsCode(sendSmsCodeCommand *command.SendSmsCodeCommand) (interface{}, error) {
if err := sendSmsCodeCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
func NewAuthService(options map[string]interface{}) *AuthService {
newAuthService := &AuthService{}
return newAuthService
}