2023-04-19 11:46:02 +03:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-12-08 16:30:55 +02:00
|
|
|
"errors"
|
2023-04-19 11:46:02 +03:00
|
|
|
|
|
|
|
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"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-04-19 11:46:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-12-20 14:21:08 +02:00
|
|
|
deviceAuthRequestTable = table{
|
|
|
|
name: projection.DeviceAuthRequestProjectionTable,
|
|
|
|
instanceIDCol: projection.DeviceAuthRequestColumnInstanceID,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnClientID = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnClientID,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnDeviceCode = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnDeviceCode,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnUserCode = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnUserCode,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnScopes = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnScopes,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2024-04-03 09:06:21 +03:00
|
|
|
DeviceAuthRequestColumnAudience = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnAudience,
|
|
|
|
table: deviceAuthRequestTable,
|
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnCreationDate = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnCreationDate,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnChangeDate = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnChangeDate,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnSequence = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnSequence,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnInstanceID = Column{
|
|
|
|
name: projection.DeviceAuthRequestColumnInstanceID,
|
|
|
|
table: deviceAuthRequestTable,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
// 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) {
|
2023-04-19 11:46:02 +03:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
stmt, scan := prepareDeviceAuthQuery(ctx, q.client)
|
|
|
|
eq := sq.Eq{
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
|
|
|
DeviceAuthRequestColumnUserCode.identifier(): userCode,
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
query, args, err := stmt.Where(eq).ToSql()
|
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-Axu7l", "Errors.Query.SQLStatement")
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 14:49:02 +02:00
|
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
2023-12-20 14:21:08 +02:00
|
|
|
authReq, err = scan(row)
|
2023-08-22 14:49:02 +02:00
|
|
|
return err
|
|
|
|
}, query, args...)
|
2023-12-20 14:21:08 +02:00
|
|
|
return authReq, err
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var deviceAuthSelectColumns = []string{
|
2023-12-20 14:21:08 +02:00
|
|
|
DeviceAuthRequestColumnClientID.identifier(),
|
|
|
|
DeviceAuthRequestColumnDeviceCode.identifier(),
|
|
|
|
DeviceAuthRequestColumnUserCode.identifier(),
|
|
|
|
DeviceAuthRequestColumnScopes.identifier(),
|
2024-04-03 09:06:21 +03:00
|
|
|
DeviceAuthRequestColumnAudience.identifier(),
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
|
2023-12-20 14:21:08 +02:00
|
|
|
func prepareDeviceAuthQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*domain.AuthRequestDevice, error)) {
|
|
|
|
return sq.Select(deviceAuthSelectColumns...).From(deviceAuthRequestTable.identifier()).PlaceholderFormat(sq.Dollar),
|
|
|
|
func(row *sql.Row) (*domain.AuthRequestDevice, error) {
|
|
|
|
dst := new(domain.AuthRequestDevice)
|
|
|
|
var (
|
2024-04-03 09:06:21 +03:00
|
|
|
scopes database.TextArray[string]
|
|
|
|
audience database.TextArray[string]
|
2023-12-20 14:21:08 +02:00
|
|
|
)
|
2023-04-19 11:46:02 +03:00
|
|
|
|
|
|
|
err := row.Scan(
|
|
|
|
&dst.ClientID,
|
2023-12-20 14:21:08 +02:00
|
|
|
&dst.DeviceCode,
|
|
|
|
&dst.UserCode,
|
2023-04-19 11:46:02 +03:00
|
|
|
&scopes,
|
2024-04-03 09:06:21 +03:00
|
|
|
&audience,
|
2023-04-19 11:46:02 +03:00
|
|
|
)
|
2023-12-08 16:30:55 +02:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, zerrors.ThrowNotFound(err, "QUERY-Sah9a", "Errors.DeviceAuth.NotExisting")
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-Voo3o", "Errors.Internal")
|
2023-04-19 11:46:02 +03:00
|
|
|
}
|
|
|
|
dst.Scopes = scopes
|
2024-04-03 09:06:21 +03:00
|
|
|
dst.Audience = audience
|
2023-04-19 11:46:02 +03:00
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
}
|