2020-05-18 10:06:36 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-12-21 17:42:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-10-15 11:52:41 +00:00
|
|
|
usr_view "github.com/caos/zitadel/internal/user/repository/view"
|
|
|
|
"github.com/caos/zitadel/internal/user/repository/view/model"
|
2020-06-25 06:01:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tokenTable = "auth.tokens"
|
|
|
|
)
|
|
|
|
|
2020-10-15 11:52:41 +00:00
|
|
|
func (v *View) TokenByID(tokenID string) (*model.TokenView, error) {
|
|
|
|
return usr_view.TokenByID(v.Db, tokenTable, tokenID)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 11:52:41 +00:00
|
|
|
func (v *View) TokensByUserID(userID string) ([]*model.TokenView, error) {
|
|
|
|
return usr_view.TokensByUserID(v.Db, tokenTable, userID)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) PutToken(token *model.TokenView, event *models.Event) error {
|
2020-10-15 11:52:41 +00:00
|
|
|
err := usr_view.PutToken(v.Db, tokenTable, token)
|
2020-05-18 10:06:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedTokenSequence(event)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) PutTokens(token []*model.TokenView, event *models.Event) error {
|
2020-10-15 11:52:41 +00:00
|
|
|
err := usr_view.PutTokens(v.Db, tokenTable, token...)
|
2020-08-28 07:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedTokenSequence(event)
|
2020-08-28 07:44:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) DeleteToken(tokenID string, event *models.Event) error {
|
2020-10-15 11:52:41 +00:00
|
|
|
err := usr_view.DeleteToken(v.Db, tokenTable, tokenID)
|
2020-12-21 17:42:34 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedTokenSequence(event)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) DeleteSessionTokens(agentID, userID string, event *models.Event) error {
|
2020-10-15 11:52:41 +00:00
|
|
|
err := usr_view.DeleteSessionTokens(v.Db, tokenTable, agentID, userID)
|
2020-12-21 17:42:34 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
2020-06-19 12:52:04 +00:00
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedTokenSequence(event)
|
2020-06-19 12:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) DeleteUserTokens(userID string, event *models.Event) error {
|
2020-10-15 11:52:41 +00:00
|
|
|
err := usr_view.DeleteUserTokens(v.Db, tokenTable, userID)
|
2020-12-21 17:42:34 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
2020-06-19 12:52:04 +00:00
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedTokenSequence(event)
|
2020-06-19 12:52:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) DeleteApplicationTokens(event *models.Event, ids ...string) error {
|
2020-10-15 11:52:41 +00:00
|
|
|
err := usr_view.DeleteApplicationTokens(v.Db, tokenTable, ids)
|
2020-12-21 17:42:34 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedTokenSequence(event)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) GetLatestTokenSequence(aggregateType string) (*repository.CurrentSequence, error) {
|
|
|
|
return v.latestSequence(tokenTable, aggregateType)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) ProcessedTokenSequence(event *models.Event) error {
|
|
|
|
return v.saveCurrentSequence(tokenTable, event)
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) UpdateTokenSpoolerRunTimestamp() error {
|
|
|
|
return v.updateSpoolerRunSequence(tokenTable)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
func (v *View) GetLatestTokenFailedEvent(sequence uint64) (*repository.FailedEvent, error) {
|
2020-05-18 10:06:36 +00:00
|
|
|
return v.latestFailedEvent(tokenTable, sequence)
|
|
|
|
}
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
func (v *View) ProcessedTokenFailedEvent(failedEvent *repository.FailedEvent) error {
|
2020-05-18 10:06:36 +00:00
|
|
|
return v.saveFailedEvent(failedEvent)
|
|
|
|
}
|