aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/facade.cpp
blob: 59a682678f097dc1724daddcb70654439253be96 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "facade.h"

#include "misc/checker.h"
#include "misc/default_uid_checker.h"
#include "misc/getter.h"
#include "misc/src_checker.h"
#include "misc/api/threaded_updater.h"
#include "misc/tool/threaded_updater.h"

namespace NTvmAuth {
    TTvmClient::TTvmClient(const NTvmTool::TClientSettings& settings, TLoggerPtr logger)
        : Updater_(NTvmTool::TThreadedUpdater::Create(settings, std::move(logger)))
        , NeedService_(true)
        , NeedUser_(true)
    {
        Y_ENSURE_EX(Updater_->GetCachedServiceContext(), TInternalException() << "Unable to get cached service context");
        Y_ENSURE_EX(Updater_->GetCachedUserContext(), TInternalException() << "Unable to get cached user context");

        if (Updater_->GetCachedServiceTickets()) {
            NeedTickets_ = true;
        }

        try {
            if (settings.ShouldCheckSrc && Updater_->GetRoles()) {
                NeedSrcChecker_ = true;
            }

            if (settings.ShouldCheckDefaultUid && Updater_->GetRoles()) {
                NeedDefaultUidChecker_ = true;
            }
        } catch (const TBrokenTvmClientSettings&) {
            // Roles are not configured
        }
    }

    TTvmClient::TTvmClient(const NTvmApi::TClientSettings& settings, TLoggerPtr logger)
        : Updater_(NTvmApi::TThreadedUpdater::Create(settings, std::move(logger)))
        , NeedService_(settings.CheckServiceTickets)
        , NeedUser_(settings.CheckUserTicketsWithBbEnv)
        , NeedTickets_(settings.IsServiceTicketFetchingRequired())
        , NeedSrcChecker_(settings.FetchRolesForIdmSystemSlug && settings.ShouldCheckSrc)
        , NeedDefaultUidChecker_(settings.FetchRolesForIdmSystemSlug && settings.ShouldCheckDefaultUid)
    {
        ServiceTicketCheckFlags_.NeedDstCheck = settings.ShouldCheckDst;
        if (NeedService_) {
            Y_ENSURE_EX(Updater_->GetCachedServiceContext(), TInternalException() << "Unable to get cached service context");
        }
        if (NeedUser_) {
            Y_ENSURE_EX(Updater_->GetCachedUserContext(), TInternalException() << "Unable to get cached user context");
        }
        if (NeedTickets_) {
            Y_ENSURE_EX(Updater_->GetCachedServiceTickets(), TInternalException() << "Unable to get cached service tickets");
        }
        if (NeedSrcChecker_) {
            GetRoles();
        }
        if (NeedDefaultUidChecker_) {
            GetRoles();
        }
    }

    TTvmClient::TTvmClient(
        TAsyncUpdaterPtr updater,
        const TServiceContext::TCheckFlags& serviceTicketCheckFlags)
        : Updater_(std::move(updater))
        , ServiceTicketCheckFlags_(serviceTicketCheckFlags)
        , NeedService_(Updater_->GetCachedServiceContext())
        , NeedUser_(Updater_->GetCachedUserContext())
        , NeedTickets_(Updater_->GetCachedServiceTickets())
    {
        try {
            if (Updater_->GetRoles()) {
                NeedSrcChecker_ = true;
                NeedDefaultUidChecker_ = true;
            }
        } catch (const TIllegalUsage&) {
            // it is a test probably
        }
    }

    TTvmClient::TTvmClient(TTvmClient&& o) = default;
    TTvmClient::~TTvmClient() = default;
    TTvmClient& TTvmClient::operator=(TTvmClient&& o) = default;

    TClientStatus TTvmClient::GetStatus() const {
        Y_ENSURE(Updater_);
        return Updater_->GetStatus();
    }

    TInstant TTvmClient::GetUpdateTimeOfPublicKeys() const {
        Y_ENSURE(Updater_);
        return Updater_->GetUpdateTimeOfPublicKeys();
    }

    TInstant TTvmClient::GetUpdateTimeOfServiceTickets() const {
        Y_ENSURE(Updater_);
        return Updater_->GetUpdateTimeOfServiceTickets();
    }

    TInstant TTvmClient::GetInvalidationTimeOfPublicKeys() const {
        Y_ENSURE(Updater_);
        return Updater_->GetInvalidationTimeOfPublicKeys();
    }

    TInstant TTvmClient::GetInvalidationTimeOfServiceTickets() const {
        Y_ENSURE(Updater_);
        return Updater_->GetInvalidationTimeOfServiceTickets();
    }

    TString TTvmClient::GetServiceTicketFor(const TClientSettings::TAlias& dst) const {
        Y_ENSURE_EX(NeedTickets_, TBrokenTvmClientSettings()
                                      << "Need to enable ServiceTickets fetching");

        TServiceTicketsPtr c = Updater_->GetCachedServiceTickets();
        return TServiceTicketGetter::GetTicket(dst, c);
    }

    TString TTvmClient::GetServiceTicketFor(const TTvmId dst) const {
        Y_ENSURE_EX(NeedTickets_, TBrokenTvmClientSettings()
                                      << "Need to enable ServiceTickets fetching");
        TServiceTicketsPtr c = Updater_->GetCachedServiceTickets();
        return TServiceTicketGetter::GetTicket(dst, c);
    }

    TCheckedServiceTicket TTvmClient::CheckServiceTicket(TStringBuf ticket) const {
        Y_ENSURE_EX(NeedService_, TBrokenTvmClientSettings()
                                      << "Need to use TClientSettings::EnableServiceTicketChecking()");

        TServiceContextPtr c = Updater_->GetCachedServiceContext();
        TCheckedServiceTicket res = TServiceTicketChecker::Check(ticket, c, ServiceTicketCheckFlags_);
        if (NeedSrcChecker_ && res) {
            NRoles::TRolesPtr roles = Updater_->GetRoles();
            return TSrcChecker::Check(std::move(res), roles);
        }
        return res;
    }

    TCheckedUserTicket TTvmClient::CheckUserTicket(TStringBuf ticket, TMaybe<EBlackboxEnv> overridenEnv) const {
        Y_ENSURE_EX(NeedUser_, TBrokenTvmClientSettings()
                                   << "Need to use TClientSettings::EnableUserTicketChecking()");

        auto c = Updater_->GetCachedUserContext(overridenEnv);
        TCheckedUserTicket res = TUserTicketChecker::Check(ticket, c);
        if (NeedDefaultUidChecker_ && res) {
            NRoles::TRolesPtr roles = Updater_->GetRoles();
            return TDefaultUidChecker::Check(std::move(res), roles);
        }
        return res;
    }

    NRoles::TRolesPtr TTvmClient::GetRoles() const {
        Y_ENSURE(Updater_);
        return Updater_->GetRoles();
    }
}