zitadel/internal/repository/deviceauth/device_auth.go
Livio Spring 9ec9ad4314
feat(oidc): sid claim for id_tokens issued through login V1 (#8525)
# Which Problems Are Solved

id_tokens issued for auth requests created through the login UI
currently do not provide a sid claim.
This is due to the fact that (SSO) sessions for the login UI do not have
one and are only computed by the userAgent(ID), the user(ID) and the
authentication checks of the latter.

This prevents client to track sessions and terminate specific session on
the end_session_endpoint.

# How the Problems Are Solved

- An `id` column is added to the `auth.user_sessions` table.
- The `id` (prefixed with `V1_`) is set whenever a session is added or
updated to active (from terminated)
- The id is passed to the `oidc session` (as v2 sessionIDs), to expose
it as `sid` claim

# Additional Changes

- refactored `getUpdateCols` to handle different column value types and
add arguments for query

# Additional Context

- closes #8499 
- relates to #8501
2024-09-03 13:19:00 +00:00

156 lines
3.6 KiB
Go

package deviceauth
import (
"context"
"time"
"golang.org/x/text/language"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
)
const (
eventTypePrefix eventstore.EventType = "device.authorization."
AddedEventType = eventTypePrefix + "added"
ApprovedEventType = eventTypePrefix + "approved"
CanceledEventType = eventTypePrefix + "canceled"
DoneEventType = eventTypePrefix + "done"
)
type AddedEvent struct {
*eventstore.BaseEvent `json:"-"`
ClientID string
DeviceCode string
UserCode string
Expires time.Time
Scopes []string
Audience []string
State domain.DeviceAuthState
NeedRefreshToken bool
}
func (e *AddedEvent) SetBaseEvent(b *eventstore.BaseEvent) {
e.BaseEvent = b
}
func (e *AddedEvent) Payload() any {
return e
}
func (e *AddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return NewAddUniqueConstraints(e.DeviceCode, e.UserCode)
}
func NewAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
clientID string,
deviceCode string,
userCode string,
expires time.Time,
scopes []string,
audience []string,
needRefreshToken bool,
) *AddedEvent {
return &AddedEvent{
eventstore.NewBaseEventForPush(
ctx, aggregate, AddedEventType,
),
clientID, deviceCode, userCode, expires, scopes, audience,
domain.DeviceAuthStateInitiated, needRefreshToken,
}
}
type ApprovedEvent struct {
*eventstore.BaseEvent `json:"-"`
UserID string
UserOrgID string
UserAuthMethods []domain.UserAuthMethodType
AuthTime time.Time
PreferredLanguage *language.Tag
UserAgent *domain.UserAgent
SessionID string
}
func (e *ApprovedEvent) SetBaseEvent(b *eventstore.BaseEvent) {
e.BaseEvent = b
}
func (e *ApprovedEvent) Payload() any {
return e
}
func (e *ApprovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewApprovedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
userID,
userOrgID string,
userAuthMethods []domain.UserAuthMethodType,
authTime time.Time,
preferredLanguage *language.Tag,
userAgent *domain.UserAgent,
sessionID string,
) *ApprovedEvent {
return &ApprovedEvent{
BaseEvent: eventstore.NewBaseEventForPush(
ctx, aggregate, ApprovedEventType,
),
UserID: userID,
UserOrgID: userOrgID,
UserAuthMethods: userAuthMethods,
AuthTime: authTime,
PreferredLanguage: preferredLanguage,
UserAgent: userAgent,
SessionID: sessionID,
}
}
type CanceledEvent struct {
*eventstore.BaseEvent `json:"-"`
Reason domain.DeviceAuthCanceled
}
func (e *CanceledEvent) SetBaseEvent(b *eventstore.BaseEvent) {
e.BaseEvent = b
}
func (e *CanceledEvent) Payload() any {
return e
}
func (e *CanceledEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewCanceledEvent(ctx context.Context, aggregate *eventstore.Aggregate, reason domain.DeviceAuthCanceled) *CanceledEvent {
return &CanceledEvent{eventstore.NewBaseEventForPush(ctx, aggregate, CanceledEventType), reason}
}
type DoneEvent struct {
*eventstore.BaseEvent `json:"-"`
}
func (e *DoneEvent) SetBaseEvent(b *eventstore.BaseEvent) {
e.BaseEvent = b
}
func (e *DoneEvent) Payload() any {
return e
}
func (e *DoneEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewDoneEvent(ctx context.Context, aggregate *eventstore.Aggregate) *DoneEvent {
return &DoneEvent{eventstore.NewBaseEventForPush(ctx, aggregate, DoneEventType)}
}