aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/src_checker.h
blob: b2de25dd949dbe73aa56e2ada8bf7747f0554045 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once

#include "async_updater.h"

#include <library/cpp/tvmauth/client/exception.h>

#include <library/cpp/tvmauth/checked_service_ticket.h>
#include <library/cpp/tvmauth/src/service_impl.h>

namespace NTvmAuth {
    class TSrcChecker {
    public:
        TSrcChecker(TAsyncUpdaterPtr updater)
            : Updater_(std::move(updater))
        {
            Y_ENSURE(Updater_);
            GetCache();
        }

        /*!
         * Checking must be enabled in TClientSettings
         * Can throw exception if cache is out of date or wrong config
         * @param ticket
         */
        TCheckedServiceTicket Check(TCheckedServiceTicket ticket) const {
            NRoles::TConsumerRolesPtr roles = GetCache()->GetRolesForService(ticket);
            if (roles) {
                return ticket;
            }

            TServiceTicketImplPtr impl = THolder(NInternal::TCanningKnife::GetS(ticket)); 
            impl->SetStatus(ETicketStatus::NoRoles);
            return TCheckedServiceTicket(std::move(impl));
        }

    private:
        NRoles::TRolesPtr GetCache() const {
            NRoles::TRolesPtr c = Updater_->GetRoles();
            Y_ENSURE_EX(c, TBrokenTvmClientSettings() << "Need to use TClientSettings::EnableRolesFetching()");
            return c;
        }

    private:
        TAsyncUpdaterPtr Updater_;
    };
}