mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.0 KiB
127 lines
3.0 KiB
4 years ago
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package auth
|
||
4 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
|
)
|
||
|
|
||
|
// Session represents a session compatible for go-chi session
|
||
|
type Session struct {
|
||
|
Key string `xorm:"pk CHAR(16)"` // has to be Key to match with go-chi/session
|
||
3 years ago
|
Data []byte `xorm:"BLOB"` // on MySQL this has a maximum size of 64Kb - this may need to be increased
|
||
4 years ago
|
Expiry timeutil.TimeStamp // has to be Expiry to match with go-chi/session
|
||
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(Session))
|
||
|
}
|
||
|
|
||
4 years ago
|
// UpdateSession updates the session with provided id
|
||
|
func UpdateSession(key string, data []byte) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).ID(key).Update(&Session{
|
||
4 years ago
|
Data: data,
|
||
|
Expiry: timeutil.TimeStampNow(),
|
||
|
})
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ReadSession reads the data for the provided session
|
||
|
func ReadSession(key string) (*Session, error) {
|
||
|
session := Session{
|
||
|
Key: key,
|
||
|
}
|
||
3 years ago
|
|
||
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
4 years ago
|
return nil, err
|
||
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
4 years ago
|
|
||
3 years ago
|
if has, err := db.GetByBean(ctx, &session); err != nil {
|
||
4 years ago
|
return nil, err
|
||
|
} else if !has {
|
||
|
session.Expiry = timeutil.TimeStampNow()
|
||
3 years ago
|
if err := db.Insert(ctx, &session); err != nil {
|
||
4 years ago
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
return &session, committer.Commit()
|
||
4 years ago
|
}
|
||
|
|
||
|
// ExistSession checks if a session exists
|
||
|
func ExistSession(key string) (bool, error) {
|
||
|
session := Session{
|
||
|
Key: key,
|
||
|
}
|
||
3 years ago
|
return db.GetEngine(db.DefaultContext).Get(&session)
|
||
4 years ago
|
}
|
||
|
|
||
|
// DestroySession destroys a session
|
||
|
func DestroySession(key string) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Delete(&Session{
|
||
4 years ago
|
Key: key,
|
||
|
})
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// RegenerateSession regenerates a session from the old id
|
||
|
func RegenerateSession(oldKey, newKey string) (*Session, error) {
|
||
3 years ago
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
4 years ago
|
return nil, err
|
||
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
4 years ago
|
|
||
3 years ago
|
if has, err := db.GetByBean(ctx, &Session{
|
||
4 years ago
|
Key: newKey,
|
||
|
}); err != nil {
|
||
|
return nil, err
|
||
|
} else if has {
|
||
|
return nil, fmt.Errorf("session Key: %s already exists", newKey)
|
||
|
}
|
||
|
|
||
3 years ago
|
if has, err := db.GetByBean(ctx, &Session{
|
||
4 years ago
|
Key: oldKey,
|
||
|
}); err != nil {
|
||
|
return nil, err
|
||
|
} else if !has {
|
||
3 years ago
|
if err := db.Insert(ctx, &Session{
|
||
4 years ago
|
Key: oldKey,
|
||
|
Expiry: timeutil.TimeStampNow(),
|
||
3 years ago
|
}); err != nil {
|
||
4 years ago
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
if _, err := db.Exec(ctx, "UPDATE "+db.TableName(&Session{})+" SET `key` = ? WHERE `key`=?", newKey, oldKey); err != nil {
|
||
4 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
|
s := Session{
|
||
|
Key: newKey,
|
||
|
}
|
||
3 years ago
|
if _, err := db.GetByBean(ctx, &s); err != nil {
|
||
4 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
return &s, committer.Commit()
|
||
4 years ago
|
}
|
||
|
|
||
|
// CountSessions returns the number of sessions
|
||
|
func CountSessions() (int64, error) {
|
||
3 years ago
|
return db.GetEngine(db.DefaultContext).Count(&Session{})
|
||
4 years ago
|
}
|
||
|
|
||
|
// CleanupSessions cleans up expired sessions
|
||
|
func CleanupSessions(maxLifetime int64) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Where("expiry <= ?", timeutil.TimeStampNow().Add(-maxLifetime)).Delete(&Session{})
|
||
4 years ago
|
return err
|
||
|
}
|