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"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/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"
|
|
|
|
)
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) TokenByID(tokenID, instanceID string) (*model.TokenView, error) {
|
|
|
|
return usr_view.TokenByID(v.Db, tokenTable, tokenID, instanceID)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) TokensByUserID(userID, instanceID string) ([]*model.TokenView, error) {
|
|
|
|
return usr_view.TokensByUserID(v.Db, tokenTable, userID, instanceID)
|
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
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) DeleteToken(tokenID, instanceID string, event *models.Event) error {
|
|
|
|
err := usr_view.DeleteToken(v.Db, tokenTable, tokenID, instanceID)
|
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
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) DeleteSessionTokens(agentID, userID, instanceID string, event *models.Event) error {
|
|
|
|
err := usr_view.DeleteSessionTokens(v.Db, tokenTable, agentID, userID, instanceID)
|
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
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) DeleteUserTokens(userID, instanceID string, event *models.Event) error {
|
|
|
|
err := usr_view.DeleteUserTokens(v.Db, tokenTable, userID, instanceID)
|
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 {
|
2022-04-19 06:26:12 +00:00
|
|
|
err := usr_view.DeleteApplicationTokens(v.Db, tokenTable, event.InstanceID, 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
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) DeleteTokensFromRefreshToken(refreshTokenID, instanceID string, event *models.Event) error {
|
|
|
|
err := usr_view.DeleteTokensFromRefreshToken(v.Db, tokenTable, refreshTokenID, instanceID)
|
2021-11-03 07:35:24 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v.ProcessedTokenSequence(event)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) GetLatestTokenSequence(instanceID string) (*repository.CurrentSequence, error) {
|
|
|
|
return v.latestSequence(tokenTable, instanceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) GetLatestTokenSequences() ([]*repository.CurrentSequence, error) {
|
|
|
|
return v.latestSequences(tokenTable)
|
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
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) GetLatestTokenFailedEvent(sequence uint64, instanceID string) (*repository.FailedEvent, error) {
|
|
|
|
return v.latestFailedEvent(tokenTable, instanceID, sequence)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|