-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauthentication.cpp
More file actions
84 lines (73 loc) · 2.1 KB
/
authentication.cpp
File metadata and controls
84 lines (73 loc) · 2.1 KB
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
#include <mage.h>
#include <iostream>
using namespace mage;
using namespace std;
int main() {
//
// Here, you specify the MAGE app to connect to, and where
// you can find it.
//
mage::RPC client("game", "localhost:8080");
//
// MAGE works with JSON-RPC: so
// we create variables for the request's
// parameters as well as for the response
//
Json::Value auth;
Json::Value user;
//
// When logging in, you will need to submit:
//
// 1. An engine name, provided to you by the server-side developer
// 2. A username
// 3. A password
//
auth["engineName"] = "cms";
auth["credentials"]["username"] = "username";
auth["credentials"]["password"] = "password";
//
// Login will return you a user and a session. We take the
// session and set it as our own.
//
// Note: It is possible that you need to call a different login
// method. If it is the case, you should still expect to receive
// the same data structure as with the standard ident module.
//
try {
user = client.Call("ident.login", auth);
cout << "Login succeeded, you are: ";
cout << user["user"]["displayName"];
cout << " (id: " << user["user"]["userId"];
cout << ")" << endl;
//
// We set the session
//
client.SetSession(user["session"]["key"].asString());
} catch (mage::MageRPCError e) {
cerr << "Could not login, an RPC error has occured: " << e.what() << " (code " << e.code() << ")" << endl;
return 1;
} catch (mage::MageErrorMessage e) {
cerr << "Login failed: " << e.code() << endl;
return 1;
}
//
// From here, all the calls you will be doing
// are authenticated.
//
Json::Value params;
Json::Value res;
params["somethings"] = "test";
params["one"]["two"]["three"] = 4;
try {
res = client.Call("mymodule.mycommand", params);
cout << "mymodule.mycommand (authenticated): " << res << endl;
} catch (mage::MageRPCError e) {
cerr << "An RPC error has occured: " << e.what() << " (code " << e.code() << ")" << endl;
} catch (mage::MageErrorMessage e) {
cerr << "mymodule.mycommand responded with an error: " << e.code() << endl;
}
//
// To clear the session:
//
client.ClearSession();
}