forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenIden.go
168 lines (153 loc) · 4.61 KB
/
tokenIden.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package handler
import (
"fmt"
"strings"
"time"
api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/api/util"
"github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
)
//TokenIdenAction TokenIdenAction
type TokenIdenAction struct{}
//CreateTokenIdenManager token identification
func CreateTokenIdenManager(conf option.Config) (*TokenIdenAction, error) {
return &TokenIdenAction{}, nil
}
//AddTokenIntoMap AddTokenIntoMap
func (t *TokenIdenAction) AddTokenIntoMap(rui *dbmodel.RegionUserInfo) {
m := GetDefaultTokenMap()
m[rui.Token] = rui
}
//DeleteTokenFromMap DeleteTokenFromMap
func (t *TokenIdenAction) DeleteTokenFromMap(oldtoken string, rui *dbmodel.RegionUserInfo) {
m := GetDefaultTokenMap()
t.AddTokenIntoMap(rui)
delete(m, oldtoken)
}
//GetAPIManager GetAPIManager
func (t *TokenIdenAction) GetAPIManager() map[string][]*dbmodel.RegionAPIClass {
return GetDefaultSourceURI()
}
//AddAPIManager AddAPIManager
func (t *TokenIdenAction) AddAPIManager(am *api_model.APIManager) *util.APIHandleError {
m := GetDefaultSourceURI()
ra := &dbmodel.RegionAPIClass{
ClassLevel: am.Body.ClassLevel,
Prefix: am.Body.Prefix,
}
if sourceList, ok := m[am.Body.ClassLevel]; ok {
sourceList = append(sourceList, ra)
} else {
//支持新增类型
newL := []*dbmodel.RegionAPIClass{ra}
m[am.Body.ClassLevel] = newL
}
ra.URI = am.Body.URI
ra.Alias = am.Body.Alias
ra.Remark = am.Body.Remark
if err := db.GetManager().RegionAPIClassDao().AddModel(ra); err != nil {
return util.CreateAPIHandleErrorFromDBError("add api manager", err)
}
return nil
}
//DeleteAPIManager DeleteAPIManager
func (t *TokenIdenAction) DeleteAPIManager(am *api_model.APIManager) *util.APIHandleError {
m := GetDefaultSourceURI()
if sourceList, ok := m[am.Body.ClassLevel]; ok {
var newL []*dbmodel.RegionAPIClass
for _, s := range sourceList {
if s.Prefix == am.Body.Prefix {
continue
}
newL = append(newL, s)
}
if len(newL) == 0 {
//该级别分组为空时,删除该资源分组
delete(m, am.Body.ClassLevel)
} else {
m[am.Body.ClassLevel] = newL
}
} else {
return util.CreateAPIHandleError(400, fmt.Errorf("have no api class level about %v", am.Body.ClassLevel))
}
if err := db.GetManager().RegionAPIClassDao().DeletePrefixInClass(am.Body.ClassLevel, am.Body.Prefix); err != nil {
return util.CreateAPIHandleErrorFromDBError("delete api prefix", err)
}
return nil
}
//CheckToken CheckToken
func (t *TokenIdenAction) CheckToken(token, uri string) bool {
m := GetDefaultTokenMap()
//logrus.Debugf("default token map is %v", m)
regionInfo, ok := m[token]
if !ok {
var err error
regionInfo, err = db.GetManager().RegionUserInfoDao().GetTokenByTokenID(token)
if err != nil {
return false
}
SetTokenCache(regionInfo)
}
if regionInfo.ValidityPeriod < int(time.Now().Unix()) {
return false
}
switch regionInfo.APIRange {
case dbmodel.ALLPOWER:
return true
case dbmodel.SERVERSOURCE:
sm := GetDefaultSourceURI()
smL, ok := sm[dbmodel.SERVERSOURCE]
if !ok {
return false
}
rc := false
for _, urinfo := range smL {
if strings.HasPrefix(uri, urinfo.Prefix) {
rc = true
}
}
return rc
case dbmodel.NODEMANAGER:
sm := GetDefaultSourceURI()
smL, ok := sm[dbmodel.NODEMANAGER]
if !ok {
return false
}
rc := false
for _, urinfo := range smL {
if strings.HasPrefix(uri, urinfo.Prefix) {
rc = true
}
}
return rc
}
return false
}
//InitTokenMap InitTokenMap
func (t *TokenIdenAction) InitTokenMap() error {
ruis, err := db.GetManager().RegionUserInfoDao().GetALLTokenInValidityPeriod()
if err != nil {
return err
}
m := GetDefaultTokenMap()
for _, rui := range ruis {
m[rui.Token] = rui
}
return nil
}