Skip to content
Snippets Groups Projects
list.go 2.53 KiB
Newer Older
package tokeninfo

import (
	"database/sql"

	"github.com/jmoiron/sqlx"
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	"github.com/oidc-mytoken/api/v0"
	"github.com/oidc-mytoken/server/internal/db"
	"github.com/oidc-mytoken/server/internal/db/dbrepo/mytokenrepo/tree"
	response "github.com/oidc-mytoken/server/internal/endpoints/token/mytoken/pkg"
	"github.com/oidc-mytoken/server/internal/endpoints/tokeninfo/pkg"
	"github.com/oidc-mytoken/server/internal/model"
	"github.com/oidc-mytoken/server/internal/utils/auth"
	"github.com/oidc-mytoken/server/internal/utils/errorfmt"
	eventService "github.com/oidc-mytoken/server/shared/mytoken/event"
	event "github.com/oidc-mytoken/server/shared/mytoken/event/pkg"
	mytoken "github.com/oidc-mytoken/server/shared/mytoken/pkg"
	"github.com/oidc-mytoken/server/shared/mytoken/restrictions"
	"github.com/oidc-mytoken/server/shared/mytoken/rotation"
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func doTokenInfoList(
	rlog log.Ext1FieldLogger, req pkg.TokenInfoRequest, mt *mytoken.Mytoken, clientMetadata *api.ClientMetaData,
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	usedRestriction *restrictions.Restriction,
) (tokenList []tree.MytokenEntryTree, tokenUpdate *response.MytokenResponse, err error) {
	err = db.Transact(
		rlog, func(tx *sqlx.Tx) error {
			tokenList, err = tree.AllTokens(rlog, tx, mt.ID)
Gabriel Zachmann's avatar
Gabriel Zachmann committed
			if err != nil && !errors.Is(err, sql.ErrNoRows) {
				return err
			}
			if usedRestriction == nil {
				return nil
			}
			if err = usedRestriction.UsedOther(rlog, tx, mt.ID); err != nil {
Gabriel Zachmann's avatar
Gabriel Zachmann committed
				return err
			}
			tokenUpdate, err = rotation.RotateMytokenAfterOtherForResponse(
				rlog, tx, req.Mytoken.JWT, mt, *clientMetadata, req.Mytoken.OriginalTokenType,
Gabriel Zachmann's avatar
Gabriel Zachmann committed
			)
			if err != nil {
				return err
			}
			return eventService.LogEvent(
				rlog, tx, eventService.MTEvent{
Gabriel Zachmann's avatar
Gabriel Zachmann committed
					Event: event.FromNumber(event.TokenInfoListMTs, ""),
					MTID:  mt.ID,
				}, *clientMetadata,
			)
		},
	)
Gabriel Zachmann's avatar
Gabriel Zachmann committed
func handleTokenInfoList(
	rlog log.Ext1FieldLogger, req pkg.TokenInfoRequest, mt *mytoken.Mytoken, clientMetadata *api.ClientMetaData,
Gabriel Zachmann's avatar
Gabriel Zachmann committed
) model.Response {
	// If we call this function it means the token is valid.
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	usedRestriction, errRes := auth.CheckCapabilityAndRestriction(
		rlog, nil, mt, clientMetadata.IP, nil, nil, api.CapabilityListMT,
Gabriel Zachmann's avatar
Gabriel Zachmann committed
	)
	if errRes != nil {
		return *errRes
	}
	tokenList, tokenUpdate, err := doTokenInfoList(rlog, req, mt, clientMetadata, usedRestriction)
	if err != nil {
		rlog.Errorf("%s", errorfmt.Full(err))
		return *model.ErrorToInternalServerErrorResponse(err)
	}
	rsp := pkg.NewTokeninfoListResponse(tokenList, tokenUpdate)
	return makeTokenInfoResponse(rsp, tokenUpdate)