aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/facade.cpp
blob: 6e775694411e69ebd9fc6505b07180baaa83d2ee (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
#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)))
        , Service_(MakeHolder<TServiceTicketChecker>(Updater_))
        , User_(MakeHolder<TUserTicketChecker>(Updater_))
    {
        if (Updater_->GetCachedServiceTickets()) {
            Tickets_ = MakeHolder<TServiceTicketGetter>(Updater_);
        }
    }

    TTvmClient::TTvmClient(const NTvmApi::TClientSettings& settings, TLoggerPtr logger)
        : Updater_(NTvmApi::TThreadedUpdater::Create(settings, std::move(logger)))
    {
        if (settings.IsServiceTicketFetchingRequired()) {
            Tickets_ = MakeHolder<TServiceTicketGetter>(Updater_);
        }
        if (settings.IsServiceTicketCheckingRequired()) {
            Service_ = MakeHolder<TServiceTicketChecker>(Updater_);
        }
        if (settings.IsUserTicketCheckingRequired()) {
            User_ = MakeHolder<TUserTicketChecker>(Updater_);
        }
        if (settings.IsRolesFetchingEnabled() && settings.ShouldCheckSrc) {
            SrcChecker_ = MakeHolder<TSrcChecker>(Updater_);
        }
        if (settings.IsRolesFetchingEnabled() && settings.ShouldCheckDefaultUid) {
            DefaultUidChecker_ = MakeHolder<TDefaultUidChecker>(Updater_);
        }
    }

    TTvmClient::TTvmClient(TAsyncUpdaterPtr updater)
        : Updater_(std::move(updater))
    {
        if (Updater_->GetCachedServiceTickets()) {
            Tickets_ = MakeHolder<TServiceTicketGetter>(Updater_);
        }
        if (Updater_->GetCachedServiceContext()) {
            Service_ = MakeHolder<TServiceTicketChecker>(Updater_);
        }
        if (Updater_->GetCachedUserContext()) {
            User_ = MakeHolder<TUserTicketChecker>(Updater_);
        }

        try {
            if (Updater_->GetRoles()) {
                SrcChecker_ = MakeHolder<TSrcChecker>(Updater_);
                DefaultUidChecker_ = MakeHolder<TDefaultUidChecker>(Updater_);
            }
        } 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(Tickets_, TBrokenTvmClientSettings()
                                  << "Need to enable ServiceTickets fetching");
        return Tickets_->GetTicket(dst);
    }

    TString TTvmClient::GetServiceTicketFor(const TTvmId dst) const {
        Y_ENSURE_EX(Tickets_, TBrokenTvmClientSettings()
                                  << "Need to enable ServiceTickets fetching");
        return Tickets_->GetTicket(dst);
    }

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

        TCheckedServiceTicket res = Service_->Check(ticket);
        if (SrcChecker_ && res) {
            return SrcChecker_->Check(std::move(res));
        }
        return res;
    }

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

        TCheckedUserTicket res = User_->Check(ticket, overrideEnv);
        if (DefaultUidChecker_ && res) {
            return DefaultUidChecker_->Check(std::move(res));
        }
        return User_->Check(ticket, overrideEnv);
    }

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