mirror of
https://github.com/zitadel/zitadel.git
synced 2026-07-25 18:28:00 +00:00
* fix: add `Scopes` to `Request` interface so that scopes can be validated on all requests * feat: assert org from scope exists when authorizing requests * fix: check all scopes * comments * check org on callback creation * fix tests * clarifications * fix scope marshaling * fix: enfore organization for authrequest in initiation * fix: filter sessions on the accounts page by organization scope * add integration tests --------- Co-authored-by: Livio Spring <livio@zitadel.com> Co-authored-by: Max Peintner <peintnerm@gmail.com>
129 lines
4.1 KiB
Go
129 lines
4.1 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
var (
|
|
deviceAuthRequestTable = table{
|
|
name: projection.DeviceAuthRequestProjectionTable,
|
|
instanceIDCol: projection.DeviceAuthRequestColumnInstanceID,
|
|
}
|
|
DeviceAuthRequestColumnClientID = Column{
|
|
name: projection.DeviceAuthRequestColumnClientID,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnDeviceCode = Column{
|
|
name: projection.DeviceAuthRequestColumnDeviceCode,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnUserCode = Column{
|
|
name: projection.DeviceAuthRequestColumnUserCode,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnScopes = Column{
|
|
name: projection.DeviceAuthRequestColumnScopes,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnAudience = Column{
|
|
name: projection.DeviceAuthRequestColumnAudience,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnCreationDate = Column{
|
|
name: projection.DeviceAuthRequestColumnCreationDate,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnChangeDate = Column{
|
|
name: projection.DeviceAuthRequestColumnChangeDate,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnSequence = Column{
|
|
name: projection.DeviceAuthRequestColumnSequence,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
DeviceAuthRequestColumnInstanceID = Column{
|
|
name: projection.DeviceAuthRequestColumnInstanceID,
|
|
table: deviceAuthRequestTable,
|
|
}
|
|
)
|
|
|
|
// DeviceAuthRequestByUserCode finds a Device Authorization request by User-Code from the `device_auth_requests` projection.
|
|
func (q *Queries) DeviceAuthRequestByUserCode(ctx context.Context, userCode string) (authReq *domain.AuthRequestDevice, err error) {
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
stmt, scan := prepareDeviceAuthQuery()
|
|
eq := sq.Eq{
|
|
DeviceAuthRequestColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
|
DeviceAuthRequestColumnUserCode.identifier(): userCode,
|
|
}
|
|
query, args, err := stmt.Where(eq).ToSql()
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-Axu7l", "Errors.Query.SQLStatement")
|
|
}
|
|
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
authReq, err = scan(row)
|
|
return err
|
|
}, query, args...)
|
|
return authReq, err
|
|
}
|
|
|
|
var deviceAuthSelectColumns = []string{
|
|
DeviceAuthRequestColumnClientID.identifier(),
|
|
DeviceAuthRequestColumnDeviceCode.identifier(),
|
|
DeviceAuthRequestColumnUserCode.identifier(),
|
|
DeviceAuthRequestColumnScopes.identifier(),
|
|
DeviceAuthRequestColumnAudience.identifier(),
|
|
AppColumnName.identifier(),
|
|
ProjectColumnName.identifier(),
|
|
}
|
|
|
|
func prepareDeviceAuthQuery() (sq.SelectBuilder, func(*sql.Row) (*domain.AuthRequestDevice, error)) {
|
|
return sq.Select(deviceAuthSelectColumns...).
|
|
From(deviceAuthRequestTable.identifier()).
|
|
LeftJoin(join(AppOIDCConfigColumnClientID, DeviceAuthRequestColumnClientID)).
|
|
LeftJoin(join(AppColumnID, AppOIDCConfigColumnAppID)).
|
|
LeftJoin(join(ProjectColumnID, AppColumnProjectID)).
|
|
PlaceholderFormat(sq.Dollar),
|
|
func(row *sql.Row) (*domain.AuthRequestDevice, error) {
|
|
var (
|
|
clientID string
|
|
deviceCode string
|
|
userCode string
|
|
scopes database.TextArray[string]
|
|
audience database.TextArray[string]
|
|
appName sql.NullString
|
|
projectName sql.NullString
|
|
)
|
|
|
|
err := row.Scan(
|
|
&clientID,
|
|
&deviceCode,
|
|
&userCode,
|
|
&scopes,
|
|
&audience,
|
|
&appName,
|
|
&projectName,
|
|
)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, zerrors.ThrowNotFound(err, "QUERY-Sah9a", "Errors.DeviceAuth.NotFound")
|
|
}
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-Voo3o", "Errors.Internal")
|
|
}
|
|
return domain.NewAuthRequestDevice(clientID, deviceCode, userCode, scopes, audience, appName.String, projectName.String), nil
|
|
}
|
|
}
|