aboutsummaryrefslogtreecommitdiffstats
path: root/library/c/tvmauth/deprecated_wrapper.h
blob: e554a01bad05efa18c26458fb551667af0ef8ede (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
#pragma once
// DO_NOT_STYLE

#ifndef _TVM_AUTH_DEPRECATED_WRAPPER_H_
#define _TVM_AUTH_DEPRECATED_WRAPPER_H_

#include "deprecated.h"
#include "tvmauth_wrapper.h"

#include <memory>
#include <string>
#include <vector>

namespace NTvmAuthWrapper {
    /*!
     * Please do not use thees types: use TvmClient instead
     */

    class TServiceContext {
    public:
        TServiceContext(TTvmId tvmId, const std::string& secretBase64, const std::string& tvmKeysResponse)
            : Ptr(nullptr, TA_DeleteServiceContext) {
            TA_TServiceContext* rawPtr;
            ThrowIfFatal(TA_CreateServiceContext(tvmId, secretBase64.c_str(), secretBase64.size(), tvmKeysResponse.c_str(), tvmKeysResponse.size(), &rawPtr));
            Ptr.reset(rawPtr);
        }

        static TServiceContext CheckingFactory(TTvmId tvmId, const std::string& tvmKeysResponse) {
            return TServiceContext(tvmId, tvmKeysResponse);
        }

        static TServiceContext SigningFactory(const std::string& secretBase64, TTvmId = 0) {
            TServiceContext ins;
            TA_TServiceContext* rawPtr;
            ThrowIfFatal(TA_CreateServiceContext(0, secretBase64.c_str(), secretBase64.size(), nullptr, 0, &rawPtr));
            ins.Ptr.reset(rawPtr);
            return ins;
        }

        TServiceContext(TServiceContext&& o) = default;
        TServiceContext& operator=(TServiceContext&& o) = default;

        TCheckedServiceTicket Check(const std::string& ticketBody) const {
            TA_TCheckedServiceTicket* ticketPtr = nullptr;
            TA_EErrorCode resultCode = TA_CheckServiceTicket(Ptr.get(), ticketBody.c_str(), ticketBody.size(), &ticketPtr);
            return TCheckedServiceTicket(ticketPtr, resultCode);
        }

        std::string SignCgiParamsForTvm(const std::string& ts, const std::string& dst, const std::string& scopes) const {
            char buffer[1024];
            size_t realSize;
            ThrowIfFatal(TA_SignCgiParamsForTvm(Ptr.get(), ts.c_str(), ts.size(), dst.c_str(), dst.size(), scopes.c_str(), scopes.size(), buffer, &realSize, 1024));
            return std::string(buffer, realSize);
        }

    public:
        // Use CheckingFactory()
        TServiceContext(TTvmId tvmId, const std::string& tvmKeysResponse)
            : Ptr(nullptr, TA_DeleteServiceContext) {
            TA_TServiceContext* rawPtr;
            ThrowIfFatal(TA_CreateServiceContext(tvmId, nullptr, 0, tvmKeysResponse.c_str(), tvmKeysResponse.size(), &rawPtr));
            Ptr.reset(rawPtr);
        }

    private:
        TServiceContext()
            : Ptr(nullptr, TA_DeleteServiceContext)
        {
        }

    private:
        std::unique_ptr<TA_TServiceContext, decltype(&TA_DeleteServiceContext)> Ptr;
    };

    class TUserContext {
    public:
        TUserContext(TA_EBlackboxEnv env, const std::string& tvmKeysResponse)
            : Ptr(nullptr, TA_DeleteUserContext) {
            TA_TUserContext* rawPtr;
            ThrowIfFatal(TA_CreateUserContext(env, tvmKeysResponse.c_str(), tvmKeysResponse.size(), &rawPtr));
            Ptr.reset(rawPtr);
        }

        TUserContext(TUserContext&& o) = default;
        TUserContext& operator=(TUserContext&& o) = default;

        TCheckedUserTicket Check(const std::string& ticketBody) const {
            TA_TCheckedUserTicket* ticketPtr = nullptr;
            TA_EErrorCode resultCode = TA_CheckUserTicket(Ptr.get(), ticketBody.c_str(), ticketBody.size(), &ticketPtr);
            return TCheckedUserTicket(ticketPtr, resultCode);
        }

    private:
        std::unique_ptr<TA_TUserContext, decltype(&TA_DeleteUserContext)> Ptr;
    };
}

#endif