Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions models/auth/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ func (opts ListAccessTokensOptions) ToOrders() string {
return "created_unix DESC"
}

// AccessTokenUseInterval is how often access_token.updated_unix ("last used") is
// persisted after a successful token authentication. Must stay well below the
// 7-day HasRecentActivity window, which is the only thing the column feeds
// besides its own display.
const AccessTokenUseInterval = 30 * time.Second

// ShouldPersistTokenUse reports whether a token's updated_unix is stale enough
// to be worth writing back. Avoids a DB write on every authenticated request.
func ShouldPersistTokenUse(last timeutil.TimeStamp, now time.Time) bool {
return now.Sub(last.AsTime()) >= AccessTokenUseInterval
}

// UpdateAccessToken updates information of access token.
func UpdateAccessToken(ctx context.Context, t *AccessToken) error {
_, err := db.GetEngine(ctx).ID(t.ID).AllCols().Update(t)
Expand Down
37 changes: 37 additions & 0 deletions models/auth/access_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,52 @@ package auth_test

import (
"testing"
"time"

auth_model "gitea.dev/models/auth"
"gitea.dev/models/db"
"gitea.dev/models/unittest"
"gitea.dev/modules/timeutil"
"gitea.dev/modules/util"

"github.com/stretchr/testify/assert"
)

func TestShouldPersistTokenUse(t *testing.T) {
now := time.Now()
tests := []struct {
name string
last timeutil.TimeStamp
want bool
}{
{
name: "fresh, skip write",
last: timeutil.TimeStamp(now.Add(-5 * time.Second).Unix()),
want: false,
},
{
name: "exactly at interval, write",
last: timeutil.TimeStamp(now.Add(-auth_model.AccessTokenUseInterval).Unix()),
want: true,
},
{
name: "stale, write",
last: timeutil.TimeStamp(now.Add(-2 * auth_model.AccessTokenUseInterval).Unix()),
want: true,
},
{
name: "zero (never used), write",
last: 0,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, auth_model.ShouldPersistTokenUse(tt.last, now))
})
}
}

func TestNewAccessToken(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
token := &auth_model.AccessToken{
Expand Down
9 changes: 6 additions & 3 deletions services/auth/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package auth
import (
"errors"
"net/http"
"time"

actions_model "gitea.dev/models/actions"
auth_model "gitea.dev/models/auth"
Expand Down Expand Up @@ -96,9 +97,11 @@ func (b *Basic) VerifyAuthToken(req *http.Request, w http.ResponseWriter, store
return nil, err
}

token.UpdatedUnix = timeutil.TimeStampNow()
if err = auth_model.UpdateAccessToken(req.Context(), token); err != nil {
log.Error("UpdateAccessToken: %v", err)
if auth_model.ShouldPersistTokenUse(token.UpdatedUnix, time.Now()) {
token.UpdatedUnix = timeutil.TimeStampNow()
if err = auth_model.UpdateAccessToken(req.Context(), token); err != nil {
log.Error("UpdateAccessToken: %v", err)
}
}

store.GetData()["LoginMethod"] = AccessTokenMethodName
Expand Down
8 changes: 5 additions & 3 deletions services/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ func (o *OAuth2) userFromToken(ctx context.Context, tokenSHA string, store DataS
return nil, err
}

t.UpdatedUnix = timeutil.TimeStampNow()
if err = auth_model.UpdateAccessToken(ctx, t); err != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it seems only the updated time should be updated, the function UpdateAccessToken could be rewrite to not update other columns.

log.Error("UpdateAccessToken: %v", err)
if auth_model.ShouldPersistTokenUse(t.UpdatedUnix, time.Now()) {
t.UpdatedUnix = timeutil.TimeStampNow()
if err = auth_model.UpdateAccessToken(ctx, t); err != nil {
log.Error("UpdateAccessToken: %v", err)
}
}
store.GetData()["ApiTokenScope"] = t.Scope
return user_model.GetUserByID(ctx, t.UID)
Expand Down
Loading