2023-05-05 15:34:53 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-12-08 14:30:55 +00:00
|
|
|
"errors"
|
2023-10-19 10:34:00 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-05-05 15:34:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2023-05-05 15:34:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-05-05 15:34:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Sessions struct {
|
|
|
|
SearchResponse
|
|
|
|
Sessions []*Session
|
|
|
|
}
|
|
|
|
|
|
|
|
type Session struct {
|
|
|
|
ID string
|
|
|
|
CreationDate time.Time
|
|
|
|
ChangeDate time.Time
|
|
|
|
Sequence uint64
|
|
|
|
State domain.SessionState
|
|
|
|
ResourceOwner string
|
|
|
|
Creator string
|
|
|
|
UserFactor SessionUserFactor
|
|
|
|
PasswordFactor SessionPasswordFactor
|
2023-06-21 14:06:18 +00:00
|
|
|
IntentFactor SessionIntentFactor
|
2023-08-11 15:36:18 +00:00
|
|
|
WebAuthNFactor SessionWebAuthNFactor
|
2023-08-15 09:50:42 +00:00
|
|
|
TOTPFactor SessionTOTPFactor
|
2023-08-24 09:41:52 +00:00
|
|
|
OTPSMSFactor SessionOTPFactor
|
|
|
|
OTPEmailFactor SessionOTPFactor
|
2023-05-05 15:34:53 +00:00
|
|
|
Metadata map[string][]byte
|
2023-10-19 10:34:00 +00:00
|
|
|
UserAgent domain.UserAgent
|
2023-11-06 09:48:28 +00:00
|
|
|
Expiration time.Time
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SessionUserFactor struct {
|
|
|
|
UserID string
|
2023-07-14 11:16:16 +00:00
|
|
|
ResourceOwner string
|
2023-05-05 15:34:53 +00:00
|
|
|
UserCheckedAt time.Time
|
|
|
|
LoginName string
|
|
|
|
DisplayName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type SessionPasswordFactor struct {
|
|
|
|
PasswordCheckedAt time.Time
|
|
|
|
}
|
|
|
|
|
2023-06-21 14:06:18 +00:00
|
|
|
type SessionIntentFactor struct {
|
|
|
|
IntentCheckedAt time.Time
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:36:18 +00:00
|
|
|
type SessionWebAuthNFactor struct {
|
|
|
|
WebAuthNCheckedAt time.Time
|
|
|
|
UserVerified bool
|
2023-06-07 15:28:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 09:50:42 +00:00
|
|
|
type SessionTOTPFactor struct {
|
|
|
|
TOTPCheckedAt time.Time
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:41:52 +00:00
|
|
|
type SessionOTPFactor struct {
|
|
|
|
OTPCheckedAt time.Time
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
type SessionsSearchQueries struct {
|
|
|
|
SearchRequest
|
|
|
|
Queries []SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *SessionsSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
|
|
|
query = q.SearchRequest.toQuery(query)
|
|
|
|
for _, q := range q.Queries {
|
|
|
|
query = q.toQuery(query)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
sessionsTable = table{
|
|
|
|
name: projection.SessionsProjectionTable,
|
|
|
|
instanceIDCol: projection.SessionColumnInstanceID,
|
|
|
|
}
|
|
|
|
SessionColumnID = Column{
|
|
|
|
name: projection.SessionColumnID,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnCreationDate = Column{
|
|
|
|
name: projection.SessionColumnCreationDate,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnChangeDate = Column{
|
|
|
|
name: projection.SessionColumnChangeDate,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnSequence = Column{
|
|
|
|
name: projection.SessionColumnSequence,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnState = Column{
|
|
|
|
name: projection.SessionColumnState,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnResourceOwner = Column{
|
|
|
|
name: projection.SessionColumnResourceOwner,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnInstanceID = Column{
|
|
|
|
name: projection.SessionColumnInstanceID,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnCreator = Column{
|
|
|
|
name: projection.SessionColumnCreator,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnUserID = Column{
|
|
|
|
name: projection.SessionColumnUserID,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-11-16 06:35:50 +00:00
|
|
|
SessionColumnUserResourceOwner = Column{
|
|
|
|
name: projection.SessionColumnUserResourceOwner,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
SessionColumnUserCheckedAt = Column{
|
|
|
|
name: projection.SessionColumnUserCheckedAt,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnPasswordCheckedAt = Column{
|
|
|
|
name: projection.SessionColumnPasswordCheckedAt,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-06-21 14:06:18 +00:00
|
|
|
SessionColumnIntentCheckedAt = Column{
|
|
|
|
name: projection.SessionColumnIntentCheckedAt,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-08-11 15:36:18 +00:00
|
|
|
SessionColumnWebAuthNCheckedAt = Column{
|
|
|
|
name: projection.SessionColumnWebAuthNCheckedAt,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnWebAuthNUserVerified = Column{
|
|
|
|
name: projection.SessionColumnWebAuthNUserVerified,
|
2023-06-07 15:28:42 +00:00
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-08-15 09:50:42 +00:00
|
|
|
SessionColumnTOTPCheckedAt = Column{
|
|
|
|
name: projection.SessionColumnTOTPCheckedAt,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-08-24 09:41:52 +00:00
|
|
|
SessionColumnOTPSMSCheckedAt = Column{
|
|
|
|
name: projection.SessionColumnOTPSMSCheckedAt,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnOTPEmailCheckedAt = Column{
|
|
|
|
name: projection.SessionColumnOTPEmailCheckedAt,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
SessionColumnMetadata = Column{
|
|
|
|
name: projection.SessionColumnMetadata,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnToken = Column{
|
|
|
|
name: projection.SessionColumnTokenID,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-10-19 10:34:00 +00:00
|
|
|
SessionColumnUserAgentFingerprintID = Column{
|
|
|
|
name: projection.SessionColumnUserAgentFingerprintID,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnUserAgentIP = Column{
|
|
|
|
name: projection.SessionColumnUserAgentIP,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnUserAgentDescription = Column{
|
|
|
|
name: projection.SessionColumnUserAgentDescription,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
|
|
|
SessionColumnUserAgentHeader = Column{
|
|
|
|
name: projection.SessionColumnUserAgentHeader,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-11-06 09:48:28 +00:00
|
|
|
SessionColumnExpiration = Column{
|
|
|
|
name: projection.SessionColumnExpiration,
|
|
|
|
table: sessionsTable,
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
)
|
|
|
|
|
2023-08-22 10:49:22 +00:00
|
|
|
func (q *Queries) SessionByID(ctx context.Context, shouldTriggerBulk bool, id, sessionToken string) (session *Session, err error) {
|
2023-05-05 15:34:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-07-07 08:15:05 +00:00
|
|
|
if shouldTriggerBulk {
|
2023-10-26 15:07:56 +00:00
|
|
|
_, traceSpan := tracing.NewNamedSpan(ctx, "TriggerSessionProjection")
|
2023-10-19 10:19:10 +00:00
|
|
|
ctx, err = projection.SessionProjection.Trigger(ctx, handler.WithAwaitRunning())
|
|
|
|
logging.OnError(err).Debug("unable to trigger")
|
2023-10-26 15:07:56 +00:00
|
|
|
traceSpan.EndWithError(err)
|
2023-07-07 08:15:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
query, scan := prepareSessionQuery(ctx, q.client)
|
|
|
|
stmt, args, err := query.Where(
|
|
|
|
sq.Eq{
|
|
|
|
SessionColumnID.identifier(): id,
|
|
|
|
SessionColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
|
|
|
},
|
|
|
|
).ToSql()
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-dn9JW", "Errors.Query.SQLStatement")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 10:49:22 +00:00
|
|
|
var tokenID string
|
|
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
|
|
session, tokenID, err = scan(row)
|
|
|
|
return err
|
|
|
|
}, stmt, args...)
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sessionToken == "" {
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
if err := q.sessionTokenVerifier(ctx, sessionToken, session.ID, tokenID); err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPermissionDenied(nil, "QUERY-dsfr3", "Errors.PermissionDenied")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2023-08-22 10:49:22 +00:00
|
|
|
func (q *Queries) SearchSessions(ctx context.Context, queries *SessionsSearchQueries) (sessions *Sessions, err error) {
|
2023-05-05 15:34:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
query, scan := prepareSessionsQuery(ctx, q.client)
|
|
|
|
stmt, args, err := queries.toQuery(query).
|
|
|
|
Where(sq.Eq{
|
|
|
|
SessionColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
2023-11-08 10:32:13 +00:00
|
|
|
}).
|
|
|
|
ToSql()
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-sn9Jf", "Errors.Query.InvalidRequest")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 10:49:22 +00:00
|
|
|
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
|
|
|
sessions, err = scan(rows)
|
|
|
|
return err
|
|
|
|
}, stmt, args...)
|
2023-05-05 15:34:53 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-Sfg42", "Errors.Internal")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
2023-08-22 10:49:22 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
sessions.State, err = q.latestState(ctx, sessionsTable)
|
2023-05-05 15:34:53 +00:00
|
|
|
return sessions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionIDsSearchQuery(ids []string) (SearchQuery, error) {
|
|
|
|
list := make([]interface{}, len(ids))
|
|
|
|
for i, value := range ids {
|
|
|
|
list[i] = value
|
|
|
|
}
|
|
|
|
return NewListQuery(SessionColumnID, list, ListIn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionCreatorSearchQuery(creator string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(SessionColumnCreator, creator, TextEquals)
|
|
|
|
}
|
|
|
|
|
2023-11-08 10:32:13 +00:00
|
|
|
func NewUserIDSearchQuery(id string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(SessionColumnUserID, id, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCreationDateQuery(datetime time.Time, compare TimestampComparison) (SearchQuery, error) {
|
|
|
|
return NewTimestampQuery(SessionColumnCreationDate, datetime, compare)
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
func prepareSessionQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*Session, string, error)) {
|
|
|
|
return sq.Select(
|
|
|
|
SessionColumnID.identifier(),
|
|
|
|
SessionColumnCreationDate.identifier(),
|
|
|
|
SessionColumnChangeDate.identifier(),
|
|
|
|
SessionColumnSequence.identifier(),
|
|
|
|
SessionColumnState.identifier(),
|
|
|
|
SessionColumnResourceOwner.identifier(),
|
|
|
|
SessionColumnCreator.identifier(),
|
|
|
|
SessionColumnUserID.identifier(),
|
2023-11-16 06:35:50 +00:00
|
|
|
SessionColumnUserResourceOwner.identifier(),
|
2023-05-05 15:34:53 +00:00
|
|
|
SessionColumnUserCheckedAt.identifier(),
|
|
|
|
LoginNameNameCol.identifier(),
|
|
|
|
HumanDisplayNameCol.identifier(),
|
|
|
|
SessionColumnPasswordCheckedAt.identifier(),
|
2023-06-21 14:06:18 +00:00
|
|
|
SessionColumnIntentCheckedAt.identifier(),
|
2023-08-11 15:36:18 +00:00
|
|
|
SessionColumnWebAuthNCheckedAt.identifier(),
|
|
|
|
SessionColumnWebAuthNUserVerified.identifier(),
|
2023-08-15 09:50:42 +00:00
|
|
|
SessionColumnTOTPCheckedAt.identifier(),
|
2023-08-24 09:41:52 +00:00
|
|
|
SessionColumnOTPSMSCheckedAt.identifier(),
|
|
|
|
SessionColumnOTPEmailCheckedAt.identifier(),
|
2023-05-05 15:34:53 +00:00
|
|
|
SessionColumnMetadata.identifier(),
|
|
|
|
SessionColumnToken.identifier(),
|
2023-10-19 10:34:00 +00:00
|
|
|
SessionColumnUserAgentFingerprintID.identifier(),
|
|
|
|
SessionColumnUserAgentIP.identifier(),
|
|
|
|
SessionColumnUserAgentDescription.identifier(),
|
|
|
|
SessionColumnUserAgentHeader.identifier(),
|
2023-11-06 09:48:28 +00:00
|
|
|
SessionColumnExpiration.identifier(),
|
2023-05-05 15:34:53 +00:00
|
|
|
).From(sessionsTable.identifier()).
|
|
|
|
LeftJoin(join(LoginNameUserIDCol, SessionColumnUserID)).
|
2023-07-14 11:16:16 +00:00
|
|
|
LeftJoin(join(HumanUserIDCol, SessionColumnUserID)).
|
|
|
|
LeftJoin(join(UserIDCol, SessionColumnUserID) + db.Timetravel(call.Took(ctx))).
|
2023-05-05 15:34:53 +00:00
|
|
|
PlaceholderFormat(sq.Dollar), func(row *sql.Row) (*Session, string, error) {
|
|
|
|
session := new(Session)
|
|
|
|
|
|
|
|
var (
|
2023-08-11 15:36:18 +00:00
|
|
|
userID sql.NullString
|
2023-11-16 06:35:50 +00:00
|
|
|
userResourceOwner sql.NullString
|
2023-08-11 15:36:18 +00:00
|
|
|
userCheckedAt sql.NullTime
|
|
|
|
loginName sql.NullString
|
|
|
|
displayName sql.NullString
|
|
|
|
passwordCheckedAt sql.NullTime
|
|
|
|
intentCheckedAt sql.NullTime
|
|
|
|
webAuthNCheckedAt sql.NullTime
|
|
|
|
webAuthNUserPresent sql.NullBool
|
2023-08-15 09:50:42 +00:00
|
|
|
totpCheckedAt sql.NullTime
|
2023-08-24 09:41:52 +00:00
|
|
|
otpSMSCheckedAt sql.NullTime
|
|
|
|
otpEmailCheckedAt sql.NullTime
|
2023-08-11 15:36:18 +00:00
|
|
|
metadata database.Map[[]byte]
|
|
|
|
token sql.NullString
|
2023-10-19 10:34:00 +00:00
|
|
|
userAgentIP sql.NullString
|
|
|
|
userAgentHeader database.Map[[]string]
|
2023-11-06 09:48:28 +00:00
|
|
|
expiration sql.NullTime
|
2023-05-05 15:34:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
err := row.Scan(
|
|
|
|
&session.ID,
|
|
|
|
&session.CreationDate,
|
|
|
|
&session.ChangeDate,
|
|
|
|
&session.Sequence,
|
|
|
|
&session.State,
|
|
|
|
&session.ResourceOwner,
|
|
|
|
&session.Creator,
|
|
|
|
&userID,
|
2023-11-16 06:35:50 +00:00
|
|
|
&userResourceOwner,
|
2023-05-05 15:34:53 +00:00
|
|
|
&userCheckedAt,
|
|
|
|
&loginName,
|
|
|
|
&displayName,
|
|
|
|
&passwordCheckedAt,
|
2023-06-21 14:06:18 +00:00
|
|
|
&intentCheckedAt,
|
2023-08-11 15:36:18 +00:00
|
|
|
&webAuthNCheckedAt,
|
|
|
|
&webAuthNUserPresent,
|
2023-08-15 09:50:42 +00:00
|
|
|
&totpCheckedAt,
|
2023-08-24 09:41:52 +00:00
|
|
|
&otpSMSCheckedAt,
|
|
|
|
&otpEmailCheckedAt,
|
2023-05-05 15:34:53 +00:00
|
|
|
&metadata,
|
|
|
|
&token,
|
2023-10-19 10:34:00 +00:00
|
|
|
&session.UserAgent.FingerprintID,
|
|
|
|
&userAgentIP,
|
|
|
|
&session.UserAgent.Description,
|
|
|
|
&userAgentHeader,
|
2023-11-06 09:48:28 +00:00
|
|
|
&expiration,
|
2023-05-05 15:34:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, "", zerrors.ThrowNotFound(err, "QUERY-SFeaa", "Errors.Session.NotExisting")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, "", zerrors.ThrowInternal(err, "QUERY-SAder", "Errors.Internal")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
session.UserFactor.UserID = userID.String
|
2023-11-16 06:35:50 +00:00
|
|
|
session.UserFactor.ResourceOwner = userResourceOwner.String
|
2023-05-05 15:34:53 +00:00
|
|
|
session.UserFactor.UserCheckedAt = userCheckedAt.Time
|
|
|
|
session.UserFactor.LoginName = loginName.String
|
|
|
|
session.UserFactor.DisplayName = displayName.String
|
|
|
|
session.PasswordFactor.PasswordCheckedAt = passwordCheckedAt.Time
|
2023-06-21 14:06:18 +00:00
|
|
|
session.IntentFactor.IntentCheckedAt = intentCheckedAt.Time
|
2023-08-11 15:36:18 +00:00
|
|
|
session.WebAuthNFactor.WebAuthNCheckedAt = webAuthNCheckedAt.Time
|
|
|
|
session.WebAuthNFactor.UserVerified = webAuthNUserPresent.Bool
|
2023-08-15 09:50:42 +00:00
|
|
|
session.TOTPFactor.TOTPCheckedAt = totpCheckedAt.Time
|
2023-08-24 09:41:52 +00:00
|
|
|
session.OTPSMSFactor.OTPCheckedAt = otpSMSCheckedAt.Time
|
|
|
|
session.OTPEmailFactor.OTPCheckedAt = otpEmailCheckedAt.Time
|
2023-05-05 15:34:53 +00:00
|
|
|
session.Metadata = metadata
|
2023-10-19 10:34:00 +00:00
|
|
|
session.UserAgent.Header = http.Header(userAgentHeader)
|
|
|
|
if userAgentIP.Valid {
|
|
|
|
session.UserAgent.IP = net.ParseIP(userAgentIP.String)
|
|
|
|
}
|
2023-11-06 09:48:28 +00:00
|
|
|
session.Expiration = expiration.Time
|
2023-05-05 15:34:53 +00:00
|
|
|
return session, token.String, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareSessionsQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*Sessions, error)) {
|
|
|
|
return sq.Select(
|
|
|
|
SessionColumnID.identifier(),
|
|
|
|
SessionColumnCreationDate.identifier(),
|
|
|
|
SessionColumnChangeDate.identifier(),
|
|
|
|
SessionColumnSequence.identifier(),
|
|
|
|
SessionColumnState.identifier(),
|
|
|
|
SessionColumnResourceOwner.identifier(),
|
|
|
|
SessionColumnCreator.identifier(),
|
|
|
|
SessionColumnUserID.identifier(),
|
2023-11-16 06:35:50 +00:00
|
|
|
SessionColumnUserResourceOwner.identifier(),
|
2023-05-05 15:34:53 +00:00
|
|
|
SessionColumnUserCheckedAt.identifier(),
|
|
|
|
LoginNameNameCol.identifier(),
|
|
|
|
HumanDisplayNameCol.identifier(),
|
|
|
|
SessionColumnPasswordCheckedAt.identifier(),
|
2023-06-21 14:06:18 +00:00
|
|
|
SessionColumnIntentCheckedAt.identifier(),
|
2023-08-11 15:36:18 +00:00
|
|
|
SessionColumnWebAuthNCheckedAt.identifier(),
|
|
|
|
SessionColumnWebAuthNUserVerified.identifier(),
|
2023-08-15 09:50:42 +00:00
|
|
|
SessionColumnTOTPCheckedAt.identifier(),
|
2023-08-24 09:41:52 +00:00
|
|
|
SessionColumnOTPSMSCheckedAt.identifier(),
|
|
|
|
SessionColumnOTPEmailCheckedAt.identifier(),
|
2023-05-05 15:34:53 +00:00
|
|
|
SessionColumnMetadata.identifier(),
|
2023-11-06 09:48:28 +00:00
|
|
|
SessionColumnExpiration.identifier(),
|
2023-05-05 15:34:53 +00:00
|
|
|
countColumn.identifier(),
|
|
|
|
).From(sessionsTable.identifier()).
|
|
|
|
LeftJoin(join(LoginNameUserIDCol, SessionColumnUserID)).
|
2023-07-14 11:16:16 +00:00
|
|
|
LeftJoin(join(HumanUserIDCol, SessionColumnUserID)).
|
|
|
|
LeftJoin(join(UserIDCol, SessionColumnUserID) + db.Timetravel(call.Took(ctx))).
|
2023-05-05 15:34:53 +00:00
|
|
|
PlaceholderFormat(sq.Dollar), func(rows *sql.Rows) (*Sessions, error) {
|
|
|
|
sessions := &Sessions{Sessions: []*Session{}}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
session := new(Session)
|
|
|
|
|
|
|
|
var (
|
2023-08-11 15:36:18 +00:00
|
|
|
userID sql.NullString
|
2023-11-16 06:35:50 +00:00
|
|
|
userResourceOwner sql.NullString
|
2023-08-11 15:36:18 +00:00
|
|
|
userCheckedAt sql.NullTime
|
|
|
|
loginName sql.NullString
|
|
|
|
displayName sql.NullString
|
|
|
|
passwordCheckedAt sql.NullTime
|
|
|
|
intentCheckedAt sql.NullTime
|
|
|
|
webAuthNCheckedAt sql.NullTime
|
|
|
|
webAuthNUserPresent sql.NullBool
|
2023-08-15 09:50:42 +00:00
|
|
|
totpCheckedAt sql.NullTime
|
2023-08-24 09:41:52 +00:00
|
|
|
otpSMSCheckedAt sql.NullTime
|
|
|
|
otpEmailCheckedAt sql.NullTime
|
2023-08-11 15:36:18 +00:00
|
|
|
metadata database.Map[[]byte]
|
2023-11-06 09:48:28 +00:00
|
|
|
expiration sql.NullTime
|
2023-05-05 15:34:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
err := rows.Scan(
|
|
|
|
&session.ID,
|
|
|
|
&session.CreationDate,
|
|
|
|
&session.ChangeDate,
|
|
|
|
&session.Sequence,
|
|
|
|
&session.State,
|
|
|
|
&session.ResourceOwner,
|
|
|
|
&session.Creator,
|
|
|
|
&userID,
|
2023-11-16 06:35:50 +00:00
|
|
|
&userResourceOwner,
|
2023-05-05 15:34:53 +00:00
|
|
|
&userCheckedAt,
|
|
|
|
&loginName,
|
|
|
|
&displayName,
|
|
|
|
&passwordCheckedAt,
|
2023-06-21 14:06:18 +00:00
|
|
|
&intentCheckedAt,
|
2023-08-11 15:36:18 +00:00
|
|
|
&webAuthNCheckedAt,
|
|
|
|
&webAuthNUserPresent,
|
2023-08-15 09:50:42 +00:00
|
|
|
&totpCheckedAt,
|
2023-08-24 09:41:52 +00:00
|
|
|
&otpSMSCheckedAt,
|
|
|
|
&otpEmailCheckedAt,
|
2023-05-05 15:34:53 +00:00
|
|
|
&metadata,
|
2023-11-06 09:48:28 +00:00
|
|
|
&expiration,
|
2023-05-05 15:34:53 +00:00
|
|
|
&sessions.Count,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-SAfeg", "Errors.Internal")
|
2023-05-05 15:34:53 +00:00
|
|
|
}
|
|
|
|
session.UserFactor.UserID = userID.String
|
2023-11-16 06:35:50 +00:00
|
|
|
session.UserFactor.ResourceOwner = userResourceOwner.String
|
2023-05-05 15:34:53 +00:00
|
|
|
session.UserFactor.UserCheckedAt = userCheckedAt.Time
|
|
|
|
session.UserFactor.LoginName = loginName.String
|
|
|
|
session.UserFactor.DisplayName = displayName.String
|
|
|
|
session.PasswordFactor.PasswordCheckedAt = passwordCheckedAt.Time
|
2023-06-21 14:06:18 +00:00
|
|
|
session.IntentFactor.IntentCheckedAt = intentCheckedAt.Time
|
2023-08-11 15:36:18 +00:00
|
|
|
session.WebAuthNFactor.WebAuthNCheckedAt = webAuthNCheckedAt.Time
|
|
|
|
session.WebAuthNFactor.UserVerified = webAuthNUserPresent.Bool
|
2023-08-15 09:50:42 +00:00
|
|
|
session.TOTPFactor.TOTPCheckedAt = totpCheckedAt.Time
|
2023-08-24 09:41:52 +00:00
|
|
|
session.OTPSMSFactor.OTPCheckedAt = otpSMSCheckedAt.Time
|
|
|
|
session.OTPEmailFactor.OTPCheckedAt = otpEmailCheckedAt.Time
|
2023-05-05 15:34:53 +00:00
|
|
|
session.Metadata = metadata
|
2023-11-06 09:48:28 +00:00
|
|
|
session.Expiration = expiration.Time
|
2023-05-05 15:34:53 +00:00
|
|
|
|
|
|
|
sessions.Sessions = append(sessions.Sessions, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessions, nil
|
|
|
|
}
|
|
|
|
}
|