Skip to content

Commit 3e49fba

Browse files
authored
feat: return time of last usage for public keys and access tokens in the api (#34323)
In the Gitea GUI, the user can see the time that _AccessTokens_ and _PublicKeys_ were last used. This information is not returned by the _/users/{username}/tokens_ and _/user/keys_ endpoints in the API. This PR adds the missing data. The time of last usage for for _tokens_ & _keys_ seem to be stored in the _Updated_ field of the structs internally. For consistency, I have used the name _updated_at_ for the new field returned by the _API_. However, for the _API_ user, I don't think that name reflects the data returned, as I believe it is the time of last usage. I propose that we use the name _last_used_at_ instead. Let's hear reviewers opinion on that. * PublicKey 1. _last_used_at_: string($date-time) * AccessToken 1. _created_at_: string($date-time) (for parity with public keys) 2. _last_used_at_: string($date-time) Fix #34313
1 parent e67f74e commit 3e49fba

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

modules/structs/user_app.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ import (
1111
// AccessToken represents an API access token.
1212
// swagger:response AccessToken
1313
type AccessToken struct {
14-
ID int64 `json:"id"`
15-
Name string `json:"name"`
16-
Token string `json:"sha1"`
17-
TokenLastEight string `json:"token_last_eight"`
18-
Scopes []string `json:"scopes"`
14+
ID int64 `json:"id"`
15+
Name string `json:"name"`
16+
Token string `json:"sha1"`
17+
TokenLastEight string `json:"token_last_eight"`
18+
Scopes []string `json:"scopes"`
19+
Created time.Time `json:"created_at"`
20+
Updated time.Time `json:"last_used_at"`
1921
}
2022

2123
// AccessTokenList represents a list of API access token.

modules/structs/user_key.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type PublicKey struct {
1616
Fingerprint string `json:"fingerprint,omitempty"`
1717
// swagger:strfmt date-time
1818
Created time.Time `json:"created_at,omitempty"`
19+
Updated time.Time `json:"last_used_at,omitempty"`
1920
Owner *User `json:"user,omitempty"`
2021
ReadOnly bool `json:"read_only,omitempty"`
2122
KeyType string `json:"key_type,omitempty"`

routers/api/v1/user/app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func ListAccessTokens(ctx *context.APIContext) {
6262
Name: tokens[i].Name,
6363
TokenLastEight: tokens[i].TokenLastEight,
6464
Scopes: tokens[i].Scope.StringSlice(),
65+
Created: tokens[i].CreatedUnix.AsTime(),
66+
Updated: tokens[i].UpdatedUnix.AsTime(),
6567
}
6668
}
6769

services/convert/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ func ToPublicKey(apiLink string, key *asymkey_model.PublicKey) *api.PublicKey {
316316
Title: key.Name,
317317
Fingerprint: key.Fingerprint,
318318
Created: key.CreatedUnix.AsTime(),
319+
Updated: key.UpdatedUnix.AsTime(),
319320
}
320321
}
321322

templates/swagger/v1_json.tmpl

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)