mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:07:31 +00:00
feat: add possibility to set an expiration to a session (#6851)
* add lifetime to session api * extend session with lifetime * check session token expiration * fix typo * integration test to check session token expiration * integration test to check session token expiration * i18n * cleanup * improve tests * prevent negative lifetime * fix error message * fix lifetime check
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
SessionsProjectionTable = "projections.sessions6"
|
||||
SessionsProjectionTable = "projections.sessions7"
|
||||
|
||||
SessionColumnID = "id"
|
||||
SessionColumnCreationDate = "creation_date"
|
||||
@@ -39,6 +39,7 @@ const (
|
||||
SessionColumnUserAgentIP = "user_agent_ip"
|
||||
SessionColumnUserAgentDescription = "user_agent_description"
|
||||
SessionColumnUserAgentHeader = "user_agent_header"
|
||||
SessionColumnExpiration = "expiration"
|
||||
)
|
||||
|
||||
type sessionProjection struct{}
|
||||
@@ -77,6 +78,7 @@ func (*sessionProjection) Init() *old_handler.Check {
|
||||
handler.NewColumn(SessionColumnUserAgentIP, handler.ColumnTypeText, handler.Nullable()),
|
||||
handler.NewColumn(SessionColumnUserAgentDescription, handler.ColumnTypeText, handler.Nullable()),
|
||||
handler.NewColumn(SessionColumnUserAgentHeader, handler.ColumnTypeJSONB, handler.Nullable()),
|
||||
handler.NewColumn(SessionColumnExpiration, handler.ColumnTypeTimestamp, handler.Nullable()),
|
||||
},
|
||||
handler.NewPrimaryKey(SessionColumnInstanceID, SessionColumnID),
|
||||
handler.WithIndex(handler.NewIndex(
|
||||
@@ -132,6 +134,10 @@ func (p *sessionProjection) Reducers() []handler.AggregateReducer {
|
||||
Event: session.MetadataSetType,
|
||||
Reduce: p.reduceMetadataSet,
|
||||
},
|
||||
{
|
||||
Event: session.LifetimeSetType,
|
||||
Reduce: p.reduceLifetimeSet,
|
||||
},
|
||||
{
|
||||
Event: session.TerminateType,
|
||||
Reduce: p.reduceSessionTerminated,
|
||||
@@ -376,6 +382,26 @@ func (p *sessionProjection) reduceMetadataSet(event eventstore.Event) (*handler.
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *sessionProjection) reduceLifetimeSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*session.LifetimeSetEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handler.NewUpdateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(SessionColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(SessionColumnSequence, e.Sequence()),
|
||||
handler.NewCol(SessionColumnExpiration, e.CreationDate().Add(e.Lifetime)),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(SessionColumnID, e.Aggregate().ID),
|
||||
handler.NewCond(SessionColumnInstanceID, e.Aggregate().InstanceID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *sessionProjection) reduceSessionTerminated(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*session.TerminateEvent)
|
||||
if !ok {
|
||||
|
Reference in New Issue
Block a user