mirror of
https://github.com/weloe/token-go.git
synced 2025-10-05 23:46:52 +08:00
feat: add DeviceMaxLoginCount
This commit is contained in:
@@ -25,6 +25,8 @@ type TokenConfig struct {
|
|||||||
// If (IsConcurrent == true && IsShare == false), support MaxLoginCount
|
// If (IsConcurrent == true && IsShare == false), support MaxLoginCount
|
||||||
// If IsConcurrent == -1, do not need to check loginCount
|
// If IsConcurrent == -1, do not need to check loginCount
|
||||||
MaxLoginCount int16
|
MaxLoginCount int16
|
||||||
|
// Maximum number of logins per device
|
||||||
|
DeviceMaxLoginCount int16
|
||||||
|
|
||||||
// Read token method
|
// Read token method
|
||||||
// Set to true to read token from these method before login.
|
// Set to true to read token from these method before login.
|
||||||
@@ -62,6 +64,7 @@ func DefaultTokenConfig() *TokenConfig {
|
|||||||
IsConcurrent: true,
|
IsConcurrent: true,
|
||||||
IsShare: true,
|
IsShare: true,
|
||||||
MaxLoginCount: 12,
|
MaxLoginCount: 12,
|
||||||
|
DeviceMaxLoginCount: 12,
|
||||||
IsReadBody: true,
|
IsReadBody: true,
|
||||||
IsReadHeader: true,
|
IsReadHeader: true,
|
||||||
IsReadCookie: true,
|
IsReadCookie: true,
|
||||||
|
81
enforcer.go
81
enforcer.go
@@ -202,7 +202,7 @@ func (e *Enforcer) LoginByModel(id string, loginModel *model.Login, ctx ctx.Cont
|
|||||||
|
|
||||||
// allocate token
|
// allocate token
|
||||||
tokenValue, err = e.createLoginToken(id, loginModel)
|
tokenValue, err = e.createLoginToken(id, loginModel)
|
||||||
|
device := loginModel.Device
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -213,7 +213,7 @@ func (e *Enforcer) LoginByModel(id string, loginModel *model.Login, ctx ctx.Cont
|
|||||||
}
|
}
|
||||||
session.AddTokenSign(&model.TokenSign{
|
session.AddTokenSign(&model.TokenSign{
|
||||||
Value: tokenValue,
|
Value: tokenValue,
|
||||||
Device: loginModel.Device,
|
Device: device,
|
||||||
})
|
})
|
||||||
|
|
||||||
timeout := loginModel.Timeout
|
timeout := loginModel.Timeout
|
||||||
@@ -237,7 +237,7 @@ func (e *Enforcer) LoginByModel(id string, loginModel *model.Login, ctx ctx.Cont
|
|||||||
|
|
||||||
// called watcher
|
// called watcher
|
||||||
m := &model.Login{
|
m := &model.Login{
|
||||||
Device: loginModel.Device,
|
Device: device,
|
||||||
IsLastingCookie: loginModel.IsLastingCookie,
|
IsLastingCookie: loginModel.IsLastingCookie,
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
JwtData: loginModel.JwtData,
|
JwtData: loginModel.JwtData,
|
||||||
@@ -252,14 +252,17 @@ func (e *Enforcer) LoginByModel(id string, loginModel *model.Login, ctx ctx.Cont
|
|||||||
e.watcher.Login(e.loginType, id, tokenValue, m)
|
e.watcher.Login(e.loginType, id, tokenValue, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if login success check it
|
if device != "" && tokenConfig.DeviceMaxLoginCount != -1 {
|
||||||
if tokenConfig.IsConcurrent && !tokenConfig.IsShare {
|
if session = e.GetSession(id); session != nil {
|
||||||
// check if the number of sessions for this account exceeds the maximum limit.
|
// get by login device
|
||||||
if tokenConfig.MaxLoginCount != -1 {
|
tokenSignList := session.GetFilterTokenSign(device)
|
||||||
if session = e.GetSession(id); session != nil {
|
if tokenSignList.Len() <= int(tokenConfig.DeviceMaxLoginCount) {
|
||||||
// logout account until loginCount == maxLoginCount if loginCount > maxLoginCount
|
return tokenValue, nil
|
||||||
for _, tokenSign := range session.TokenSignList {
|
}
|
||||||
if session.TokenSignSize() > int(tokenConfig.MaxLoginCount) {
|
// if loginCount > maxLoginCount, logout account until single device Login count is equal to DeviceMaxLoginCount
|
||||||
|
for element := tokenSignList.Front(); element != nil; element = element.Next() {
|
||||||
|
if tokenSign, ok := element.Value.(*model.TokenSign); ok {
|
||||||
|
if session.TokenSignSize() > int(tokenConfig.DeviceMaxLoginCount) {
|
||||||
// delete tokenSign
|
// delete tokenSign
|
||||||
tokenSignValue := tokenSign.Value
|
tokenSignValue := tokenSign.Value
|
||||||
session.RemoveTokenSign(tokenSignValue)
|
session.RemoveTokenSign(tokenSignValue)
|
||||||
@@ -272,19 +275,61 @@ func (e *Enforcer) LoginByModel(id string, loginModel *model.Login, ctx ctx.Cont
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
e.logger.Logout(e.loginType, id, tokenSignValue)
|
||||||
}
|
|
||||||
|
|
||||||
// check TokenSignList length, if length == 0, delete this session
|
if e.watcher != nil {
|
||||||
if session != nil && session.TokenSignSize() == 0 {
|
e.watcher.Logout(e.loginType, id, tokenSignValue)
|
||||||
err = e.DeleteSession(id)
|
}
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// check TokenSignList length, if length == 0, delete this session
|
||||||
|
if session != nil && session.TokenSignSize() == 0 {
|
||||||
|
err = e.DeleteSession(id)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the number of sessions for this account exceeds the maximum limit.
|
||||||
|
if tokenConfig.MaxLoginCount != -1 {
|
||||||
|
if session = e.GetSession(id); session != nil {
|
||||||
|
if session.TokenSignSize() <= int(tokenConfig.MaxLoginCount) {
|
||||||
|
return tokenValue, nil
|
||||||
|
}
|
||||||
|
// logout account until loginCount == maxLoginCount if loginCount > maxLoginCount
|
||||||
|
for _, tokenSign := range session.TokenSignList {
|
||||||
|
if session.TokenSignSize() > int(tokenConfig.MaxLoginCount) {
|
||||||
|
// delete tokenSign
|
||||||
|
tokenSignValue := tokenSign.Value
|
||||||
|
session.RemoveTokenSign(tokenSignValue)
|
||||||
|
err = e.UpdateSession(id, session)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
// delete token-id
|
||||||
|
err = e.deleteIdByToken(tokenSignValue)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
e.logger.Logout(e.loginType, id, tokenSignValue)
|
||||||
|
|
||||||
|
if e.watcher != nil {
|
||||||
|
e.watcher.Logout(e.loginType, id, tokenSignValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check TokenSignList length, if length == 0, delete this session
|
||||||
|
if session != nil && session.TokenSignSize() == 0 {
|
||||||
|
err = e.DeleteSession(id)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return tokenValue, nil
|
return tokenValue, nil
|
||||||
|
Reference in New Issue
Block a user