-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.hpp
57 lines (47 loc) · 2.08 KB
/
scope.hpp
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
#include <vector>
#include <string>
#include <numeric>
namespace cstream {
enum Scope {
ChatWrite = 1 << 1, // chat:write
BillingPrivateRead = 1 << 2, // billing:private:read
OfflineAccess = 1 << 3, // offline_access
OpenID = 1 << 4, // openid
Profile = 1 << 5, // profile
VideoPrivateRead = 1 << 6, // video:private:read
};
inline std::vector<std::string> scopes_to_strings(int scopeFlags) {
std::vector<std::string> out;
if(scopeFlags & ChatWrite) out.emplace_back("chat:write");
if(scopeFlags & BillingPrivateRead) out.emplace_back("billing:private:read");
if(scopeFlags & OfflineAccess) out.emplace_back("offline_access");
if(scopeFlags & OpenID) out.emplace_back("openid");
if(scopeFlags & Profile) out.emplace_back("profile");
if(scopeFlags & VideoPrivateRead) out.emplace_back("video:private:read");
return out;
}
inline std::string scopes_from_list(std::vector<std::string> scopes) {
if(scopes.empty()) return "";
return std::accumulate(std::next(scopes.begin()), scopes.end(), scopes[0],
[](std::string a, std::string b) {
return a + " " + b;
}
);
}
inline std::string scopes_from_list(int scopeFlags) {
return scopes_from_list(scopes_to_strings(scopeFlags));
}
}
#ifdef CSTREAM_BUILD_TESTS
#include "doctest/doctest.h"
TEST_CASE("scopes_to_strings") {
using namespace cstream;
CHECK(scopes_to_strings(Scope::ChatWrite) == std::vector<std::string>{"chat:write"});
CHECK(scopes_to_strings(Scope::BillingPrivateRead) == std::vector<std::string>{"billing:private:read"});
CHECK(scopes_to_strings(Scope::OfflineAccess) == std::vector<std::string>{"offline_access"});
CHECK(scopes_to_strings(Scope::OpenID) == std::vector<std::string>{"openid"});
CHECK(scopes_to_strings(Scope::Profile) == std::vector<std::string>{"profile"});
CHECK(scopes_to_strings(Scope::VideoPrivateRead) == std::vector<std::string>{"video:private:read"});
CHECK(scopes_to_strings(Scope::ChatWrite | Scope::VideoPrivateRead) == std::vector<std::string>{"chat:write", "video:private:read"});
}
#endif // CSTREAM_BUILD_TESTS