Auto sign off

This commit is contained in:
LittleSheep 2024-02-18 16:26:46 +08:00
parent 00028cfce8
commit e429627ecf
5 changed files with 30 additions and 2 deletions

1
go.mod
View File

@ -58,6 +58,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/philhofer/fwd v1.1.2 // indirect github.com/philhofer/fwd v1.1.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect

2
go.sum
View File

@ -153,6 +153,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=

View File

@ -3,6 +3,8 @@ package main
import ( import (
"code.smartsheep.studio/hydrogen/identity/pkg/external" "code.smartsheep.studio/hydrogen/identity/pkg/external"
"code.smartsheep.studio/hydrogen/identity/pkg/server" "code.smartsheep.studio/hydrogen/identity/pkg/server"
"code.smartsheep.studio/hydrogen/identity/pkg/services"
"github.com/robfig/cron/v3"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -50,12 +52,26 @@ func main() {
server.NewServer() server.NewServer()
go server.Listen() go server.Listen()
// Configure timed tasks
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
quartz.AddFunc("@every 15s", func() {
log.Info().Msg("Running auto sign off...")
if tx := services.PerformAutoSignoff(); tx.Error != nil {
log.Error().Err(tx.Error).Msg("An error occurred when running auto sign off...")
} else {
log.Info().Int64("affected", tx.RowsAffected).Msg("Auto sign off accomplished.")
}
})
quartz.Run()
// Messages // Messages
log.Info().Msgf("Passport v%s is started...", identity.AppVersion) log.Info().Msgf("Identity v%s is started...", identity.AppVersion)
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit <-quit
log.Info().Msgf("Passport v%s is quitting...", identity.AppVersion) log.Info().Msgf("Identity v%s is quitting...", identity.AppVersion)
quartz.Stop()
} }

View File

@ -3,6 +3,9 @@ package services
import ( import (
"code.smartsheep.studio/hydrogen/identity/pkg/database" "code.smartsheep.studio/hydrogen/identity/pkg/database"
"code.smartsheep.studio/hydrogen/identity/pkg/models" "code.smartsheep.studio/hydrogen/identity/pkg/models"
"github.com/spf13/viper"
"gorm.io/gorm"
"time"
) )
func LookupSessionWithToken(tokenId string) (models.AuthSession, error) { func LookupSessionWithToken(tokenId string) (models.AuthSession, error) {
@ -16,3 +19,8 @@ func LookupSessionWithToken(tokenId string) (models.AuthSession, error) {
return session, nil return session, nil
} }
func PerformAutoSignoff() *gorm.DB {
signoffDuration := time.Duration(viper.GetInt64("security.auto_signoff_duration")) * time.Second
return database.C.Where("last_grant_at < ?", time.Now().Add(-signoffDuration)).Delete(&models.AuthSession{})
}

View File

@ -24,6 +24,7 @@ password = "gz937Zxxzfcd9SeH"
[security] [security]
cookie_domain = "localhost" cookie_domain = "localhost"
cookie_samesite = "Lax" cookie_samesite = "Lax"
auto_signoff_duration = 86400
access_token_duration = 300 access_token_duration = 300
refresh_token_duration = 2592000 refresh_token_duration = 2592000