aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/service_tickets.h
blob: 6a24bd56895c23d9c0e71fed347f58a99657f6e8 (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
#pragma once

#include "settings.h"
#include "roles/roles.h"

#include <library/cpp/tvmauth/src/utils.h>

#include <util/datetime/base.h>
#include <util/generic/hash.h>
#include <util/generic/maybe.h>
#include <util/generic/noncopyable.h>
#include <util/generic/ptr.h>

namespace NTvmAuth::NInternal {
    class TClientCaningKnife;
}

namespace NTvmAuth {
    class TServiceTickets: public TAtomicRefCount<TServiceTickets> {
    public:
        using TMapAliasStr = THashMap<TClientSettings::TAlias, TString>;
        using TMapIdStr = THashMap<TTvmId, TString>;
        using TIdSet = THashSet<TTvmId>;
        using TAliasSet = THashSet<TClientSettings::TAlias>;
        using TMapAliasId = THashMap<TClientSettings::TAlias, TTvmId>;

        TServiceTickets(TMapIdStr&& tickets, TMapIdStr&& errors, const TMapAliasId& dstMap)
            : TicketsById(std::move(tickets))
            , ErrorsById(std::move(errors))
        {
            InitAliasesAndUnfetchedIds(dstMap);
            InitInvalidationTime();
        }

        static TInstant GetInvalidationTime(const TMapIdStr& ticketsById) {
            TInstant res;

            for (const auto& pair : ticketsById) {
                TMaybe<TInstant> t = NTvmAuth::NInternal::TCanningKnife::GetExpirationTime(pair.second);
                if (!t) {
                    continue;
                }

                res = res == TInstant() ? *t : std::min(res, *t);
            }

            return res;
        }

    public:
        TMapIdStr TicketsById;
        TMapIdStr ErrorsById;
        TMapAliasStr TicketsByAlias;
        TMapAliasStr ErrorsByAlias;
        TInstant InvalidationTime;
        TIdSet UnfetchedIds;
        TAliasSet UnfetchedAliases;

    private:
        void InitAliasesAndUnfetchedIds(const TMapAliasId& dstMap) {
            for (const auto& pair : dstMap) {
                auto it = TicketsById.find(pair.second);
                auto errIt = ErrorsById.find(pair.second);

                if (it == TicketsById.end()) {
                    if (errIt != ErrorsById.end()) {
                        Y_ENSURE(ErrorsByAlias.insert({pair.first, errIt->second}).second,
                                 "failed to add: " << pair.first);
                    } else {
                        UnfetchedAliases.insert(pair.first);
                        UnfetchedIds.insert(pair.second);
                    }
                } else {
                    Y_ENSURE(TicketsByAlias.insert({pair.first, it->second}).second,
                             "failed to add: " << pair.first);
                }
            }
        }

        void InitInvalidationTime() {
            InvalidationTime = GetInvalidationTime(TicketsById);
        }
    };

    using TServiceTicketsPtr = TIntrusiveConstPtr<TServiceTickets>;
}