TrinityCore
SecretMgr.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __TRINITY_SECRETMGR_H__
19#define __TRINITY_SECRETMGR_H__
20
21#include "BigNumber.h"
22#include "Common.h"
23#include "LogCommon.h"
24#include "Optional.h"
25#include <array>
26#include <mutex>
27#include <string>
28
30{
32
33 // only add new indices right above this line
35};
36
38{
41
43};
44
46{
47 private:
50
51 public:
53
54 SecretMgr(SecretMgr const&) = delete;
55 static SecretMgr* instance();
56
57 struct Secret
58 {
59 public:
60 explicit operator bool() const { return (state == PRESENT); }
61 BigNumber const& operator*() const { return value; }
62 BigNumber const* operator->() const { return &value; }
63 bool IsAvailable() const { return (state != NOT_LOADED_YET) && (state != LOAD_FAILED); }
64
65 private:
66 std::mutex lock;
67 enum { NOT_LOADED_YET, LOAD_FAILED, NOT_PRESENT, PRESENT } state = NOT_LOADED_YET;
69
70 friend class SecretMgr;
71 };
72
73 void Initialize(SecretOwner owner);
74 Secret const& GetSecret(Secrets i);
75
76 private:
77 void AttemptLoad(Secrets i, LogLevel errorLevel, std::unique_lock<std::mutex> const&);
78 Optional<std::string> AttemptTransition(Secrets i, Optional<BigNumber> const& newSecret, Optional<BigNumber> const& oldSecret, bool hadOldSecret) const;
79
80 std::array<Secret, NUM_SECRETS> _secrets;
81};
82
83#define sSecretMgr SecretMgr::instance()
84
85#endif
#define TC_SHARED_API
Definition: Define.h:117
uint32_t uint32
Definition: Define.h:142
LogLevel
Definition: LogCommon.h:25
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
SecretOwner
Definition: SecretMgr.h:38
@ SECRET_OWNER_BNETSERVER
Definition: SecretMgr.h:39
@ NUM_SECRET_OWNERS
Definition: SecretMgr.h:42
@ SECRET_OWNER_WORLDSERVER
Definition: SecretMgr.h:40
Secrets
Definition: SecretMgr.h:30
@ NUM_SECRETS
Definition: SecretMgr.h:34
@ SECRET_TOTP_MASTER_KEY
Definition: SecretMgr.h:31
std::array< Secret, NUM_SECRETS > _secrets
Definition: SecretMgr.h:80
Optional< std::string > AttemptTransition(Secrets i, Optional< BigNumber > const &newSecret, Optional< BigNumber > const &oldSecret, bool hadOldSecret) const
Definition: SecretMgr.cpp:171
void AttemptLoad(Secrets i, LogLevel errorLevel, std::unique_lock< std::mutex > const &)
Definition: SecretMgr.cpp:109
~SecretMgr()
Definition: SecretMgr.h:49
SecretMgr()
Definition: SecretMgr.h:48
SecretMgr(SecretMgr const &)=delete
static SecretOwner OWNER
Definition: SecretMgr.h:52
Secret const & GetSecret(Secrets i)
Definition: SecretMgr.cpp:100
BigNumber value
Definition: SecretMgr.h:68
BigNumber const & operator*() const
Definition: SecretMgr.h:61
BigNumber const * operator->() const
Definition: SecretMgr.h:62
bool IsAvailable() const
Definition: SecretMgr.h:63
std::mutex lock
Definition: SecretMgr.h:66