aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/checker.h
blob: e8ed2f55036ec4dc75bb1b58dcea41c982ff7699 (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
#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/checked_user_ticket.h>

namespace NTvmAuth {
    class TServiceTicketChecker {
    public:
        TServiceTicketChecker(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(TStringBuf ticket) const {
            return GetCache()->Check(ticket);
        }

    private:
        TServiceContextPtr GetCache() const {
            TServiceContextPtr c = Updater_->GetCachedServiceContext();
            Y_ENSURE_EX(c, TBrokenTvmClientSettings() << "Need to use TClientSettings::EnableServiceTicketChecking()");
            return c;
        }

    private:
        TAsyncUpdaterPtr Updater_;
    };

    class TUserTicketChecker {
    public:
        TUserTicketChecker(TAsyncUpdaterPtr updater)
            : Updater_(std::move(updater))
        {
            Y_ENSURE(Updater_);
            GetCache({});
        }

        /*!
         * Blackbox enviroment must be cofingured in TClientSettings
         * Can throw exception if cache is out of date or wrong config
         */
        TCheckedUserTicket Check(TStringBuf ticket, TMaybe<EBlackboxEnv> overridenEnv) const {
            return GetCache(overridenEnv)->Check(ticket);
        }

    private:
        TUserContextPtr GetCache(TMaybe<EBlackboxEnv> overridenEnv) const {
            TUserContextPtr c = Updater_->GetCachedUserContext(overridenEnv);
            Y_ENSURE_EX(c, TBrokenTvmClientSettings() << "Need to use TClientSettings::EnableUserTicketChecking()");
            return c;
        }

    private:
        TAsyncUpdaterPtr Updater_;
    };
}