mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-12 22:22:27 +08:00
init commit
This commit is contained in:
109
water-api/auth/water_auth.go
Normal file
109
water-api/auth/water_auth.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
providerApiEntity "flswld.com/annie-user-api/entity"
|
||||
"flswld.com/light"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var tokenUserMap map[string]*providerApiEntity.User
|
||||
var tokenUserMapLock sync.RWMutex
|
||||
var tokenValidMap map[string]bool
|
||||
var tokenValidMapLock sync.RWMutex
|
||||
var tokenTimeoutMap map[string]int64
|
||||
var tokenTimeoutMapLock sync.RWMutex
|
||||
|
||||
func init() {
|
||||
tokenUserMap = make(map[string]*providerApiEntity.User)
|
||||
tokenValidMap = make(map[string]bool)
|
||||
tokenTimeoutMap = make(map[string]int64)
|
||||
go CleanTimeoutToken()
|
||||
}
|
||||
|
||||
func CleanTimeoutToken() {
|
||||
ticker := time.NewTicker(time.Second * 300)
|
||||
for {
|
||||
now := time.Now().Unix()
|
||||
deleteTokenList := make([]string, 0)
|
||||
tokenTimeoutMapLock.RLock()
|
||||
for accessToken, createTime := range tokenTimeoutMap {
|
||||
if now-createTime > 3600*24 {
|
||||
deleteTokenList = append(deleteTokenList, accessToken)
|
||||
}
|
||||
}
|
||||
tokenTimeoutMapLock.RUnlock()
|
||||
tokenUserMapLock.Lock()
|
||||
tokenValidMapLock.Lock()
|
||||
tokenTimeoutMapLock.Lock()
|
||||
for _, accessToken := range deleteTokenList {
|
||||
delete(tokenUserMap, accessToken)
|
||||
delete(tokenValidMap, accessToken)
|
||||
delete(tokenTimeoutMap, accessToken)
|
||||
}
|
||||
tokenUserMapLock.Unlock()
|
||||
tokenValidMapLock.Unlock()
|
||||
tokenTimeoutMapLock.Unlock()
|
||||
<-ticker.C
|
||||
}
|
||||
}
|
||||
|
||||
func WaterQueryUserByAccessToken(consumer *light.Consumer, accessToken string) (*providerApiEntity.User, error) {
|
||||
tokenUserMapLock.RLock()
|
||||
value, ok := tokenUserMap[accessToken]
|
||||
tokenUserMapLock.RUnlock()
|
||||
if ok {
|
||||
return value, nil
|
||||
}
|
||||
user := new(providerApiEntity.User)
|
||||
err := consumer.CallFunction("RpcService", "RpcQueryUserByAccessToken", accessToken, user)
|
||||
if err == false {
|
||||
return nil, errors.New("rpc call fail")
|
||||
}
|
||||
tokenUserMapLock.Lock()
|
||||
tokenUserMap[accessToken] = user
|
||||
tokenUserMapLock.Unlock()
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func WaterVerifyAccessToken(consumer *light.Consumer, accessToken string) (bool, error) {
|
||||
tokenValidMapLock.RLock()
|
||||
_, ok := tokenValidMap[accessToken]
|
||||
tokenValidMapLock.RUnlock()
|
||||
if ok {
|
||||
tokenTimeoutMapLock.RLock()
|
||||
tokenCreateTime := tokenTimeoutMap[accessToken]
|
||||
tokenTimeoutMapLock.RUnlock()
|
||||
if time.Now().Unix()-tokenCreateTime <= 3600*24 {
|
||||
return true, nil
|
||||
} else {
|
||||
tokenUserMapLock.Lock()
|
||||
delete(tokenUserMap, accessToken)
|
||||
tokenUserMapLock.Unlock()
|
||||
tokenValidMapLock.Lock()
|
||||
delete(tokenValidMap, accessToken)
|
||||
tokenValidMapLock.Unlock()
|
||||
tokenTimeoutMapLock.Lock()
|
||||
delete(tokenTimeoutMap, accessToken)
|
||||
tokenTimeoutMapLock.Unlock()
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
var valid bool
|
||||
err := consumer.CallFunction("RpcService", "RpcVerifyAccessToken", accessToken, &valid)
|
||||
if err == false {
|
||||
return false, errors.New("rpc call fail")
|
||||
}
|
||||
if valid {
|
||||
tokenValidMapLock.Lock()
|
||||
tokenValidMap[accessToken] = true
|
||||
tokenValidMapLock.Unlock()
|
||||
tokenTimeoutMapLock.Lock()
|
||||
tokenTimeoutMap[accessToken] = time.Now().Unix()
|
||||
tokenTimeoutMapLock.Unlock()
|
||||
return true, nil
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
25
water-api/go.mod
Normal file
25
water-api/go.mod
Normal file
@@ -0,0 +1,25 @@
|
||||
module water-api
|
||||
|
||||
go 1.19
|
||||
|
||||
require flswld.com/common v0.0.0-incompatible // indirect
|
||||
|
||||
replace flswld.com/common => ../common
|
||||
|
||||
require flswld.com/logger v0.0.0-incompatible // indirect
|
||||
|
||||
replace flswld.com/logger => ../logger
|
||||
|
||||
require flswld.com/air-api v0.0.0-incompatible // indirect
|
||||
|
||||
replace flswld.com/air-api => ../air-api
|
||||
|
||||
require flswld.com/light v0.0.0-incompatible
|
||||
|
||||
replace flswld.com/light => ../light
|
||||
|
||||
require flswld.com/annie-user-api v0.0.0-incompatible
|
||||
|
||||
require github.com/BurntSushi/toml v0.3.1 // indirect
|
||||
|
||||
replace flswld.com/annie-user-api => ../service/annie-user-api
|
||||
2
water-api/go.sum
Normal file
2
water-api/go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
Reference in New Issue
Block a user