@@ -16,11 +16,11 @@ import (
1616)
1717
1818var configuration = conf .GetConfig ()
19- var connectionsCacheLock sync.Mutex
19+ var ConnectionsCacheLock sync.Mutex
2020
2121const (
22- errorMsgAuthorizationViolation = "authorization violation"
23- errorMsgMissionAccountId = "account id"
22+ ErrorMsgAuthorizationViolation = "authorization violation"
23+ ErrorMsgMissionAccountId = "account id"
2424)
2525
2626type AuthHandler struct {}
@@ -35,9 +35,9 @@ type refreshTokenExpiration struct {
3535 RefreshTokenExpiration int64 `json:"refresh_token_expiration"`
3636}
3737
38- var connectionsCache = map [string ]map [string ]Connection {}
38+ var ConnectionsCache = map [string ]map [string ]Connection {}
3939
40- func connect (password , username , connectionToken string , accountId int ) (* memphis.Conn , error ) {
40+ func Connect (password , username , connectionToken string , accountId int ) (* memphis.Conn , error ) {
4141 if configuration .USER_PASS_BASED_AUTH {
4242 if accountId == 0 {
4343 accountId = 1
@@ -75,10 +75,10 @@ func (ah AuthHandler) Authenticate(c *fiber.Ctx) error {
7575 })
7676 }
7777
78- conn , err := connect (body .Password , body .Username , body .ConnectionToken , int (body .AccountId ))
78+ conn , err := Connect (body .Password , body .Username , body .ConnectionToken , int (body .AccountId ))
7979 if err != nil {
8080 errMsg := strings .ToLower (err .Error ())
81- if strings .Contains (errMsg , errorMsgAuthorizationViolation ) || strings .Contains (errMsg , "token" ) || strings .Contains (errMsg , errorMsgMissionAccountId ) {
81+ if strings .Contains (errMsg , ErrorMsgAuthorizationViolation ) || strings .Contains (errMsg , "token" ) || strings .Contains (errMsg , ErrorMsgMissionAccountId ) {
8282 log .Warnf ("Authentication error" )
8383 return c .Status (401 ).JSON (fiber.Map {
8484 "message" : "Unauthorized" ,
@@ -103,15 +103,15 @@ func (ah AuthHandler) Authenticate(c *fiber.Ctx) error {
103103
104104 username := strings .ToLower (body .Username )
105105 accountId := strconv .Itoa (int (body .AccountId ))
106- if connectionsCache [accountId ] == nil {
107- connectionsCacheLock .Lock ()
108- connectionsCache [accountId ] = make (map [string ]Connection )
109- connectionsCacheLock .Unlock ()
106+ if ConnectionsCache [accountId ] == nil {
107+ ConnectionsCacheLock .Lock ()
108+ ConnectionsCache [accountId ] = make (map [string ]Connection )
109+ ConnectionsCacheLock .Unlock ()
110110 }
111111
112- connectionsCacheLock .Lock ()
113- connectionsCache [accountId ][username ] = Connection {Connection : conn , ExpirationTime : tokenExpiry }
114- connectionsCacheLock .Unlock ()
112+ ConnectionsCacheLock .Lock ()
113+ ConnectionsCache [accountId ][username ] = Connection {Connection : conn , ExpirationTime : tokenExpiry }
114+ ConnectionsCacheLock .Unlock ()
115115 return c .Status (fiber .StatusOK ).JSON (fiber.Map {
116116 "jwt" : token ,
117117 "expires_in" : tokenExpiry * 60 * 1000 ,
@@ -186,10 +186,10 @@ func (ah AuthHandler) RefreshToken(c *fiber.Ctx) error {
186186 password := userData .Password
187187 connectionToken := userData .ConnectionToken
188188
189- conn , err := connect (password , username , connectionToken , accountId )
189+ conn , err := Connect (password , username , connectionToken , accountId )
190190 if err != nil {
191191 errMsg := strings .ToLower (err .Error ())
192- if strings .Contains (errMsg , errorMsgAuthorizationViolation ) || strings .Contains (errMsg , "token" ) || strings .Contains (errMsg , errorMsgMissionAccountId ) {
192+ if strings .Contains (errMsg , ErrorMsgAuthorizationViolation ) || strings .Contains (errMsg , "token" ) || strings .Contains (errMsg , ErrorMsgMissionAccountId ) {
193193 log .Warnf ("RefreshToken: Authentication error" )
194194 return c .Status (401 ).JSON (fiber.Map {
195195 "message" : "Unauthorized" ,
@@ -211,15 +211,15 @@ func (ah AuthHandler) RefreshToken(c *fiber.Ctx) error {
211211 }
212212
213213 accountId = int (accountId )
214- if connectionsCache [strconv .Itoa (int (accountId ))] == nil {
215- connectionsCacheLock .Lock ()
216- connectionsCache [strconv .Itoa (accountId )] = make (map [string ]Connection )
217- connectionsCacheLock .Unlock ()
214+ if ConnectionsCache [strconv .Itoa (int (accountId ))] == nil {
215+ ConnectionsCacheLock .Lock ()
216+ ConnectionsCache [strconv .Itoa (accountId )] = make (map [string ]Connection )
217+ ConnectionsCacheLock .Unlock ()
218218 }
219219
220- connectionsCacheLock .Lock ()
221- connectionsCache [strconv .Itoa (accountId )][username ] = Connection {Connection : conn , ExpirationTime : refreshTokenExpiry }
222- connectionsCacheLock .Unlock ()
220+ ConnectionsCacheLock .Lock ()
221+ ConnectionsCache [strconv .Itoa (accountId )][username ] = Connection {Connection : conn , ExpirationTime : refreshTokenExpiry }
222+ ConnectionsCacheLock .Unlock ()
223223 return c .Status (fiber .StatusOK ).JSON (fiber.Map {
224224 "jwt" : token ,
225225 "expires_in" : tokenExpiry * 60 * 1000 ,
@@ -230,22 +230,22 @@ func (ah AuthHandler) RefreshToken(c *fiber.Ctx) error {
230230
231231func CleanConnectionsCache () {
232232 for range time .Tick (time .Second * 30 ) {
233- for t , tenant := range connectionsCache {
233+ for t , tenant := range ConnectionsCache {
234234 for u , user := range tenant {
235235 currentTime := time .Now ()
236236 unixTimeNow := currentTime .Unix ()
237- conn := connectionsCache [t ][u ].Connection
237+ conn := ConnectionsCache [t ][u ].Connection
238238 if unixTimeNow > int64 (user .ExpirationTime ) {
239239 conn .Close ()
240- connectionsCacheLock .Lock ()
241- delete (connectionsCache [t ], u )
242- connectionsCacheLock .Unlock ()
240+ ConnectionsCacheLock .Lock ()
241+ delete (ConnectionsCache [t ], u )
242+ ConnectionsCacheLock .Unlock ()
243243 }
244244 }
245- if len (connectionsCache [t ]) == 0 {
246- connectionsCacheLock .Lock ()
247- delete (connectionsCache , t )
248- connectionsCacheLock .Unlock ()
245+ if len (ConnectionsCache [t ]) == 0 {
246+ ConnectionsCacheLock .Lock ()
247+ delete (ConnectionsCache , t )
248+ ConnectionsCacheLock .Unlock ()
249249 }
250250 }
251251 }
0 commit comments