mirror of
https://github.com/weloe/token-go.git
synced 2025-10-06 16:07:18 +08:00
feat: add device manager api
This commit is contained in:
21
enforcer.go
21
enforcer.go
@@ -642,3 +642,24 @@ func (e *Enforcer) RefreshTokenByModel(refreshToken string, refreshModel *model.
|
||||
RefreshToken: refreshToken,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (e *Enforcer) GetLoginDevices(id string) []string {
|
||||
session := e.GetSession(id)
|
||||
if session == nil {
|
||||
return nil
|
||||
}
|
||||
return session.GetAllDevice()
|
||||
}
|
||||
|
||||
func (e *Enforcer) GetDeviceByToken(token string) string {
|
||||
id := e.getIdByToken(token)
|
||||
session := e.GetSession(id)
|
||||
if session == nil {
|
||||
return ""
|
||||
}
|
||||
tokenSign := session.GetTokenSign(token)
|
||||
if tokenSign == nil {
|
||||
return ""
|
||||
}
|
||||
return tokenSign.Device
|
||||
}
|
||||
|
@@ -45,6 +45,10 @@ type IEnforcer interface {
|
||||
GetIdByToken(token string) string
|
||||
GetLoginCount(id string, device ...string) int
|
||||
|
||||
// device manager api
|
||||
GetLoginDevices(id string) []string
|
||||
GetDeviceByToken(token string) string
|
||||
|
||||
// refresh api
|
||||
GetRefreshToken(tokenValue string) string
|
||||
RefreshToken(refreshToken string, refreshModel ...*model.Refresh) (*model.RefreshRes, error)
|
||||
|
@@ -621,3 +621,19 @@ func TestEnforcer_RefreshToken(t *testing.T) {
|
||||
t.Fatalf("RefreshToken() failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforcer_GetLoginDevices(t *testing.T) {
|
||||
enforcer, _ := NewTestEnforcer(t)
|
||||
t1, err := enforcer.LoginById("1", "test")
|
||||
if err != nil {
|
||||
t.Fatalf("LoginById failed: %v", err)
|
||||
}
|
||||
devices := enforcer.GetLoginDevices("1")
|
||||
if len(devices) != 1 || devices[0] != "test" {
|
||||
t.Fatalf("GetLoginDevices failed, want is 'test'")
|
||||
}
|
||||
device := enforcer.GetDeviceByToken(t1)
|
||||
if device != "test" {
|
||||
t.Fatalf("GetLoginDevices failed, want is 'test'")
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"container/list"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/weloe/token-go/util"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -65,6 +66,16 @@ func (s *Session) GetFilterTokenSignSlice(device string) []*TokenSign {
|
||||
return l
|
||||
}
|
||||
|
||||
func (s *Session) GetAllDevice() []string {
|
||||
arr := make([]string, 0)
|
||||
for _, sign := range s.TokenSignList {
|
||||
if !util.HasStr(arr, sign.Device) {
|
||||
arr = append(arr, sign.Device)
|
||||
}
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// GetFilterTokenSign filter by TokenSign.Device from all TokenSign
|
||||
func (s *Session) GetFilterTokenSign(device string) *list.List {
|
||||
copyList := list.New()
|
||||
|
Reference in New Issue
Block a user