diff --git a/go.mod b/go.mod --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ github.com/golang-jwt/jwt/v4 v4.5.2 github.com/google/uuid v1.4.0 github.com/gorilla/sessions v1.4.0 github.com/gorilla/websocket v1.5.1 + github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/ipfs/go-block-format v0.2.0 github.com/ipfs/go-cid v0.4.1 diff --git a/go.sum b/go.sum --- a/go.sum +++ b/go.sum @@ -91,6 +91,8 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= +github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b h1:wDUNC2eKiL35DbLvsDhiblTUXHxcOPwQSCzi7xpQUN4= +github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b/go.mod h1:VzxiSdG6j1pi7rwGm/xYI5RbtpBgM8sARDXlvEvxlu0= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI= diff --git a/server/handle_account.go b/server/handle_account.go --- a/server/handle_account.go +++ b/server/handle_account.go @@ -6,10 +6,12 @@ "github.com/haileyok/cocoon/oauth" "github.com/haileyok/cocoon/oauth/constants" "github.com/haileyok/cocoon/oauth/provider" + "github.com/hako/durafmt" "github.com/labstack/echo/v4" ) func (s *Server) handleAccount(e echo.Context) error { + ctx := e.Request().Context() repo, sess, err := s.getSessionRepoOrErr(e) if err != nil { return e.Redirect(303, "/account/signin") @@ -36,15 +38,31 @@ } filtered = append(filtered, t) } + now := time.Now() + tokenInfo := []map[string]string{} for _, t := range tokens { + ageRes := oauth.GetSessionAgeFromToken(t) + maxTime := constants.PublicClientSessionLifetime + if t.ClientAuth.Method != "none" { + maxTime = constants.ConfidentialClientSessionLifetime + } + + var clientName string + metadata, err := s.oauthProvider.ClientManager.GetClient(ctx, t.ClientId) + if err != nil { + clientName = t.ClientId + } else { + clientName = metadata.Metadata.ClientName + } + tokenInfo = append(tokenInfo, map[string]string{ - "ClientId": t.ClientId, - "CreatedAt": t.CreatedAt.Format("02 Jan 06 15:04 MST"), - "UpdatedAt": t.CreatedAt.Format("02 Jan 06 15:04 MST"), - "ExpiresAt": t.CreatedAt.Format("02 Jan 06 15:04 MST"), - "Token": t.Token, - "Ip": t.Ip, + "ClientName": clientName, + "Age": durafmt.Parse(ageRes.SessionAge).LimitFirstN(2).String(), + "LastUpdated": durafmt.Parse(now.Sub(t.UpdatedAt)).LimitFirstN(2).String(), + "ExpiresIn": durafmt.Parse(now.Add(maxTime).Sub(now)).LimitFirstN(2).String(), + "Token": t.Token, + "Ip": t.Ip, }) } diff --git a/server/templates/account.html b/server/templates/account.html --- a/server/templates/account.html +++ b/server/templates/account.html @@ -24,10 +24,10 @@

You do not have any active OAuth sessions!

{{ else }} {{ range .Tokens }}
-

{{ .ClientId }}

-

Created: {{ .CreatedAt }}

-

Updated: {{ .UpdatedAt }}

-

Expires: {{ .ExpiresAt }}

+

{{ .ClientName }}

+

Session Age: {{ .Age}}

+

Last Updated: {{ .LastUpdated }} ago

+

Expires In: {{ .ExpiresIn }}

IP Address: {{ .Ip }}