Feature: Parse user agent string in user auth token api response (#16… (#17504)
* Feature: Parse user agent string in user auth token api response (#16222) * Adding UA Parser Go modules attempt (#16222) * Bring user agent vals up per req * fix tests * doc update * update to flatten, no maps * update doc
This commit is contained in:
@@ -3,10 +3,14 @@ package dtos
|
||||
import "time"
|
||||
|
||||
type UserToken struct {
|
||||
Id int64 `json:"id"`
|
||||
IsActive bool `json:"isActive"`
|
||||
ClientIp string `json:"clientIp"`
|
||||
UserAgent string `json:"userAgent"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
SeenAt time.Time `json:"seenAt"`
|
||||
Id int64 `json:"id"`
|
||||
IsActive bool `json:"isActive"`
|
||||
ClientIp string `json:"clientIp"`
|
||||
Device string `json:"device"`
|
||||
OperatingSystem string `json:"os"`
|
||||
OperatingSystemVersion string `json:"osVersion"`
|
||||
Browser string `json:"browser"`
|
||||
BrowserVersion string `json:"browserVersion"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
SeenAt time.Time `json:"seenAt"`
|
||||
}
|
||||
|
||||
+32
-6
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/ua-parser/uap-go/uaparser"
|
||||
)
|
||||
|
||||
// GET /api/user/auth-tokens
|
||||
@@ -62,13 +63,38 @@ func (server *HTTPServer) getUserAuthTokensInternal(c *models.ReqContext, userID
|
||||
isActive = true
|
||||
}
|
||||
|
||||
parser := uaparser.NewFromSaved()
|
||||
client := parser.Parse(token.UserAgent)
|
||||
|
||||
osVersion := ""
|
||||
if client.Os.Major != "" {
|
||||
osVersion = client.Os.Major
|
||||
|
||||
if client.Os.Minor != "" {
|
||||
osVersion = osVersion + "." + client.Os.Minor
|
||||
}
|
||||
}
|
||||
|
||||
browserVersion := ""
|
||||
if client.UserAgent.Major != "" {
|
||||
browserVersion = client.UserAgent.Major
|
||||
|
||||
if client.UserAgent.Minor != "" {
|
||||
browserVersion = browserVersion + "." + client.UserAgent.Minor
|
||||
}
|
||||
}
|
||||
|
||||
result = append(result, &dtos.UserToken{
|
||||
Id: token.Id,
|
||||
IsActive: isActive,
|
||||
ClientIp: token.ClientIp,
|
||||
UserAgent: token.UserAgent,
|
||||
CreatedAt: time.Unix(token.CreatedAt, 0),
|
||||
SeenAt: time.Unix(token.SeenAt, 0),
|
||||
Id: token.Id,
|
||||
IsActive: isActive,
|
||||
ClientIp: token.ClientIp,
|
||||
Device: client.Device.ToString(),
|
||||
OperatingSystem: client.Os.Family,
|
||||
OperatingSystemVersion: osVersion,
|
||||
Browser: client.UserAgent.Family,
|
||||
BrowserVersion: browserVersion,
|
||||
CreatedAt: time.Unix(token.CreatedAt, 0),
|
||||
SeenAt: time.Unix(token.SeenAt, 0),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -140,17 +140,27 @@ func TestUserTokenApiEndpoint(t *testing.T) {
|
||||
So(resultOne.Get("id").MustInt64(), ShouldEqual, tokens[0].Id)
|
||||
So(resultOne.Get("isActive").MustBool(), ShouldBeTrue)
|
||||
So(resultOne.Get("clientIp").MustString(), ShouldEqual, "127.0.0.1")
|
||||
So(resultOne.Get("userAgent").MustString(), ShouldEqual, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36")
|
||||
So(resultOne.Get("createdAt").MustString(), ShouldEqual, time.Unix(tokens[0].CreatedAt, 0).Format(time.RFC3339))
|
||||
So(resultOne.Get("seenAt").MustString(), ShouldEqual, time.Unix(tokens[0].SeenAt, 0).Format(time.RFC3339))
|
||||
|
||||
So(resultOne.Get("device").MustString(), ShouldEqual, "Other")
|
||||
So(resultOne.Get("browser").MustString(), ShouldEqual, "Chrome")
|
||||
So(resultOne.Get("browserVersion").MustString(), ShouldEqual, "72.0")
|
||||
So(resultOne.Get("os").MustString(), ShouldEqual, "Linux")
|
||||
So(resultOne.Get("osVersion").MustString(), ShouldEqual, "")
|
||||
|
||||
resultTwo := result.GetIndex(1)
|
||||
So(resultTwo.Get("id").MustInt64(), ShouldEqual, tokens[1].Id)
|
||||
So(resultTwo.Get("isActive").MustBool(), ShouldBeFalse)
|
||||
So(resultTwo.Get("clientIp").MustString(), ShouldEqual, "127.0.0.2")
|
||||
So(resultTwo.Get("userAgent").MustString(), ShouldEqual, "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
|
||||
So(resultTwo.Get("createdAt").MustString(), ShouldEqual, time.Unix(tokens[1].CreatedAt, 0).Format(time.RFC3339))
|
||||
So(resultTwo.Get("seenAt").MustString(), ShouldEqual, time.Unix(tokens[1].SeenAt, 0).Format(time.RFC3339))
|
||||
|
||||
So(resultTwo.Get("device").MustString(), ShouldEqual, "iPhone")
|
||||
So(resultTwo.Get("browser").MustString(), ShouldEqual, "Mobile Safari")
|
||||
So(resultTwo.Get("browserVersion").MustString(), ShouldEqual, "11.0")
|
||||
So(resultTwo.Get("os").MustString(), ShouldEqual, "iOS")
|
||||
So(resultTwo.Get("osVersion").MustString(), ShouldEqual, "11.0")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user