From e429627ecff161e93dfb5644e2804cac1669726c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 18 Feb 2024 16:26:46 +0800 Subject: [PATCH] :sparkles: Auto sign off --- go.mod | 1 + go.sum | 2 ++ pkg/cmd/main.go | 20 ++++++++++++++++++-- pkg/services/sessions.go | 8 ++++++++ settings.toml | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 518989a..a26692c 100644 --- a/go.mod +++ b/go.mod @@ -58,6 +58,7 @@ require ( github.com/pelletier/go-toml/v2 v2.1.1 // indirect github.com/philhofer/fwd v1.1.2 // 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/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect diff --git a/go.sum b/go.sum index 6b6b1f9..ff7dace 100644 --- a/go.sum +++ b/go.sum @@ -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/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= 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/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= diff --git a/pkg/cmd/main.go b/pkg/cmd/main.go index 9697a7c..81b93ca 100644 --- a/pkg/cmd/main.go +++ b/pkg/cmd/main.go @@ -3,6 +3,8 @@ package main import ( "code.smartsheep.studio/hydrogen/identity/pkg/external" "code.smartsheep.studio/hydrogen/identity/pkg/server" + "code.smartsheep.studio/hydrogen/identity/pkg/services" + "github.com/robfig/cron/v3" "os" "os/signal" "syscall" @@ -50,12 +52,26 @@ func main() { server.NewServer() 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 - 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) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) <-quit - log.Info().Msgf("Passport v%s is quitting...", identity.AppVersion) + log.Info().Msgf("Identity v%s is quitting...", identity.AppVersion) + + quartz.Stop() } diff --git a/pkg/services/sessions.go b/pkg/services/sessions.go index 1e015e2..8cecee3 100644 --- a/pkg/services/sessions.go +++ b/pkg/services/sessions.go @@ -3,6 +3,9 @@ package services import ( "code.smartsheep.studio/hydrogen/identity/pkg/database" "code.smartsheep.studio/hydrogen/identity/pkg/models" + "github.com/spf13/viper" + "gorm.io/gorm" + "time" ) func LookupSessionWithToken(tokenId string) (models.AuthSession, error) { @@ -16,3 +19,8 @@ func LookupSessionWithToken(tokenId string) (models.AuthSession, error) { 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{}) +} diff --git a/settings.toml b/settings.toml index dc327d7..18582ba 100644 --- a/settings.toml +++ b/settings.toml @@ -24,6 +24,7 @@ password = "gz937Zxxzfcd9SeH" [security] cookie_domain = "localhost" cookie_samesite = "Lax" +auto_signoff_duration = 86400 access_token_duration = 300 refresh_token_duration = 2592000