2022-02-08 08:37:28 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-12-08 14:30:55 +00:00
|
|
|
"errors"
|
2022-02-08 08:37:28 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-02-27 21:36:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
2022-08-31 07:52:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-12-01 08:18: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"
|
2022-02-08 08:37:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
personalAccessTokensTable = table{
|
2022-10-27 06:08:36 +00:00
|
|
|
name: projection.PersonalAccessTokenProjectionTable,
|
|
|
|
instanceIDCol: projection.PersonalAccessTokenColumnInstanceID,
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
PersonalAccessTokenColumnID = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnID,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
|
|
|
PersonalAccessTokenColumnUserID = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnUserID,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
|
|
|
PersonalAccessTokenColumnExpiration = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnExpiration,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
|
|
|
PersonalAccessTokenColumnScopes = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnScopes,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
|
|
|
PersonalAccessTokenColumnCreationDate = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnCreationDate,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
|
|
|
PersonalAccessTokenColumnChangeDate = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnChangeDate,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
|
|
|
PersonalAccessTokenColumnResourceOwner = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnResourceOwner,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
PersonalAccessTokenColumnInstanceID = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnInstanceID,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
2022-02-08 08:37:28 +00:00
|
|
|
PersonalAccessTokenColumnSequence = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnSequence,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
PersonalAccessTokenColumnOwnerRemoved = Column{
|
|
|
|
name: projection.PersonalAccessTokenColumnOwnerRemoved,
|
|
|
|
table: personalAccessTokensTable,
|
|
|
|
}
|
2022-02-08 08:37:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PersonalAccessTokens struct {
|
|
|
|
SearchResponse
|
|
|
|
PersonalAccessTokens []*PersonalAccessToken
|
|
|
|
}
|
|
|
|
|
|
|
|
type PersonalAccessToken struct {
|
|
|
|
ID string
|
|
|
|
CreationDate time.Time
|
|
|
|
ChangeDate time.Time
|
|
|
|
ResourceOwner string
|
|
|
|
Sequence uint64
|
|
|
|
|
|
|
|
UserID string
|
|
|
|
Expiration time.Time
|
2023-10-19 10:19:10 +00:00
|
|
|
Scopes database.TextArray[string]
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PersonalAccessTokenSearchQueries struct {
|
|
|
|
SearchRequest
|
|
|
|
Queries []SearchQuery
|
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
func (q *Queries) PersonalAccessTokenByID(ctx context.Context, shouldTriggerBulk bool, id string, withOwnerRemoved bool, queries ...SearchQuery) (pat *PersonalAccessToken, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
if shouldTriggerBulk {
|
2023-10-26 15:07:56 +00:00
|
|
|
_, traceSpan := tracing.NewNamedSpan(ctx, "TriggerPersonalAccessTokenProjection")
|
2023-10-19 10:19:10 +00:00
|
|
|
ctx, err = projection.PersonalAccessTokenProjection.Trigger(ctx, handler.WithAwaitRunning())
|
|
|
|
logging.OnError(err).Debug("trigger failed")
|
2023-10-26 15:07:56 +00:00
|
|
|
traceSpan.EndWithError(err)
|
2022-06-14 05:51:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
query, scan := preparePersonalAccessTokenQuery(ctx, q.client)
|
2022-02-08 08:37:28 +00:00
|
|
|
for _, q := range queries {
|
|
|
|
query = q.toQuery(query)
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
eq := sq.Eq{
|
2022-03-23 08:02:39 +00:00
|
|
|
PersonalAccessTokenColumnID.identifier(): id,
|
2022-03-29 09:53:19 +00:00
|
|
|
PersonalAccessTokenColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
|
|
|
if !withOwnerRemoved {
|
|
|
|
eq[PersonalAccessTokenColumnOwnerRemoved.identifier()] = false
|
|
|
|
}
|
|
|
|
stmt, args, err := query.Where(eq).ToSql()
|
2022-02-08 08:37:28 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-Dgfb4", "Errors.Query.SQLStatment")
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
|
|
pat, err = scan(row)
|
|
|
|
return err
|
|
|
|
}, stmt, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pat, nil
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 16:01:17 +00:00
|
|
|
func (q *Queries) SearchPersonalAccessTokens(ctx context.Context, queries *PersonalAccessTokenSearchQueries, withOwnerRemoved bool) (personalAccessTokens *PersonalAccessTokens, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
query, scan := preparePersonalAccessTokensQuery(ctx, q.client)
|
2022-11-30 16:01:17 +00:00
|
|
|
eq := sq.Eq{
|
|
|
|
PersonalAccessTokenColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
|
|
|
}
|
|
|
|
if !withOwnerRemoved {
|
|
|
|
eq[PersonalAccessTokenColumnOwnerRemoved.identifier()] = false
|
|
|
|
}
|
|
|
|
stmt, args, err := queries.toQuery(query).Where(eq).ToSql()
|
2022-02-08 08:37:28 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-Hjw2w", "Errors.Query.InvalidRequest")
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
|
|
|
personalAccessTokens, err = scan(rows)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}, stmt, args...)
|
2022-02-08 08:37:28 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-Bmz63", "Errors.Internal")
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
2023-08-22 12:49:02 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
personalAccessTokens.State, err = q.latestState(ctx, personalAccessTokensTable)
|
2022-02-08 08:37:28 +00:00
|
|
|
return personalAccessTokens, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPersonalAccessTokenResourceOwnerSearchQuery(value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(PersonalAccessTokenColumnResourceOwner, value, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPersonalAccessTokenUserIDSearchQuery(value string) (SearchQuery, error) {
|
|
|
|
return NewTextQuery(PersonalAccessTokenColumnUserID, value, TextEquals)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PersonalAccessTokenSearchQueries) AppendMyResourceOwnerQuery(orgID string) error {
|
|
|
|
query, err := NewPersonalAccessTokenResourceOwnerSearchQuery(orgID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.Queries = append(r.Queries, query)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *PersonalAccessTokenSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
|
|
|
query = q.SearchRequest.toQuery(query)
|
|
|
|
for _, q := range q.Queries {
|
|
|
|
query = q.toQuery(query)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func preparePersonalAccessTokenQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*PersonalAccessToken, error)) {
|
2022-02-08 08:37:28 +00:00
|
|
|
return sq.Select(
|
|
|
|
PersonalAccessTokenColumnID.identifier(),
|
|
|
|
PersonalAccessTokenColumnCreationDate.identifier(),
|
|
|
|
PersonalAccessTokenColumnChangeDate.identifier(),
|
|
|
|
PersonalAccessTokenColumnResourceOwner.identifier(),
|
|
|
|
PersonalAccessTokenColumnSequence.identifier(),
|
|
|
|
PersonalAccessTokenColumnUserID.identifier(),
|
|
|
|
PersonalAccessTokenColumnExpiration.identifier(),
|
|
|
|
PersonalAccessTokenColumnScopes.identifier()).
|
2023-02-27 21:36:43 +00:00
|
|
|
From(personalAccessTokensTable.identifier() + db.Timetravel(call.Took(ctx))).
|
|
|
|
PlaceholderFormat(sq.Dollar),
|
2022-02-08 08:37:28 +00:00
|
|
|
func(row *sql.Row) (*PersonalAccessToken, error) {
|
|
|
|
p := new(PersonalAccessToken)
|
|
|
|
err := row.Scan(
|
|
|
|
&p.ID,
|
|
|
|
&p.CreationDate,
|
|
|
|
&p.ChangeDate,
|
|
|
|
&p.ResourceOwner,
|
|
|
|
&p.Sequence,
|
|
|
|
&p.UserID,
|
|
|
|
&p.Expiration,
|
2022-08-31 07:52:43 +00:00
|
|
|
&p.Scopes,
|
2022-02-08 08:37:28 +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-fRunu", "Errors.PersonalAccessToken.NotFound")
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-dj2FF", "Errors.Internal")
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func preparePersonalAccessTokensQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*PersonalAccessTokens, error)) {
|
2022-02-08 08:37:28 +00:00
|
|
|
return sq.Select(
|
|
|
|
PersonalAccessTokenColumnID.identifier(),
|
|
|
|
PersonalAccessTokenColumnCreationDate.identifier(),
|
|
|
|
PersonalAccessTokenColumnChangeDate.identifier(),
|
|
|
|
PersonalAccessTokenColumnResourceOwner.identifier(),
|
|
|
|
PersonalAccessTokenColumnSequence.identifier(),
|
|
|
|
PersonalAccessTokenColumnUserID.identifier(),
|
|
|
|
PersonalAccessTokenColumnExpiration.identifier(),
|
|
|
|
PersonalAccessTokenColumnScopes.identifier(),
|
|
|
|
countColumn.identifier()).
|
2023-02-27 21:36:43 +00:00
|
|
|
From(personalAccessTokensTable.identifier() + db.Timetravel(call.Took(ctx))).
|
|
|
|
PlaceholderFormat(sq.Dollar),
|
2022-02-08 08:37:28 +00:00
|
|
|
func(rows *sql.Rows) (*PersonalAccessTokens, error) {
|
|
|
|
personalAccessTokens := make([]*PersonalAccessToken, 0)
|
|
|
|
var count uint64
|
|
|
|
for rows.Next() {
|
|
|
|
token := new(PersonalAccessToken)
|
|
|
|
err := rows.Scan(
|
|
|
|
&token.ID,
|
|
|
|
&token.CreationDate,
|
|
|
|
&token.ChangeDate,
|
|
|
|
&token.ResourceOwner,
|
|
|
|
&token.Sequence,
|
|
|
|
&token.UserID,
|
|
|
|
&token.Expiration,
|
2022-08-31 07:52:43 +00:00
|
|
|
&token.Scopes,
|
2022-02-08 08:37:28 +00:00
|
|
|
&count,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
personalAccessTokens = append(personalAccessTokens, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Close(); err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-QMXJv", "Errors.Query.CloseRows")
|
2022-02-08 08:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &PersonalAccessTokens{
|
|
|
|
PersonalAccessTokens: personalAccessTokens,
|
|
|
|
SearchResponse: SearchResponse{
|
|
|
|
Count: count,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|