|
4 | 4 | using System.Security.Cryptography;
|
5 | 5 | using System.Collections;
|
6 | 6 |
|
7 |
| -public class DdpAccount { |
| 7 | +namespace DDP { |
8 | 8 |
|
9 |
| - private DdpConnection connection; |
| 9 | + public class DdpAccount { |
10 | 10 |
|
11 |
| - public bool isLogged; |
12 |
| - public string userId; |
13 |
| - public string token; |
14 |
| - public DateTime tokenExpiration; |
| 11 | + private DdpConnection connection; |
15 | 12 |
|
16 |
| - public DdpError error; |
| 13 | + public bool isLogged; |
| 14 | + public string userId; |
| 15 | + public string token; |
| 16 | + public DateTime tokenExpiration; |
17 | 17 |
|
18 |
| - public DdpAccount(DdpConnection connection) { |
19 |
| - this.connection = connection; |
20 |
| - } |
| 18 | + public DdpError error; |
| 19 | + |
| 20 | + public DdpAccount(DdpConnection connection) { |
| 21 | + this.connection = connection; |
| 22 | + } |
21 | 23 |
|
22 |
| - private JSONObject GetPasswordObj(string password) { |
23 |
| - string digest = BitConverter.ToString( |
24 |
| - new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(password))) |
25 |
| - .Replace("-", "").ToLower(); |
| 24 | + private JSONObject GetPasswordObj(string password) { |
| 25 | + string digest = BitConverter.ToString( |
| 26 | + new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(password))) |
| 27 | + .Replace("-", "").ToLower(); |
26 | 28 |
|
27 |
| - JSONObject passwordObj = JSONObject.Create(); |
28 |
| - passwordObj.AddField("digest", digest); |
29 |
| - passwordObj.AddField("algorithm", "sha-256"); |
| 29 | + JSONObject passwordObj = JSONObject.Create(); |
| 30 | + passwordObj.AddField("digest", digest); |
| 31 | + passwordObj.AddField("algorithm", "sha-256"); |
30 | 32 |
|
31 |
| - return passwordObj; |
32 |
| - } |
| 33 | + return passwordObj; |
| 34 | + } |
33 | 35 |
|
34 |
| - private void HandleLoginResult(MethodCall loginCall) { |
35 |
| - error = loginCall.error; |
| 36 | + private void HandleLoginResult(MethodCall loginCall) { |
| 37 | + error = loginCall.error; |
36 | 38 |
|
37 |
| - if (error == null) { |
38 |
| - JSONObject result = loginCall.result; |
39 |
| - isLogged = true; |
40 |
| - this.userId = result["id"].str; |
41 |
| - this.token = result["token"].str; |
42 |
| - this.tokenExpiration = result["tokenExpires"].GetDateTime(); |
| 39 | + if (error == null) { |
| 40 | + JSONObject result = loginCall.result; |
| 41 | + isLogged = true; |
| 42 | + this.userId = result["id"].str; |
| 43 | + this.token = result["token"].str; |
| 44 | + this.tokenExpiration = result["tokenExpires"].GetDateTime(); |
| 45 | + } |
43 | 46 | }
|
44 |
| - } |
45 | 47 |
|
46 |
| - private void HandleLogoutResult(MethodCall logoutCall) { |
47 |
| - error = logoutCall.error; |
| 48 | + private void HandleLogoutResult(MethodCall logoutCall) { |
| 49 | + error = logoutCall.error; |
48 | 50 |
|
49 |
| - if (error == null) { |
50 |
| - isLogged = false; |
51 |
| - this.userId = null; |
52 |
| - this.token = null; |
53 |
| - this.tokenExpiration = default(DateTime); |
| 51 | + if (error == null) { |
| 52 | + isLogged = false; |
| 53 | + this.userId = null; |
| 54 | + this.token = null; |
| 55 | + this.tokenExpiration = default(DateTime); |
| 56 | + } |
54 | 57 | }
|
55 |
| - } |
56 | 58 |
|
57 |
| - public IEnumerator CreateUserAndLogin(string username, string password) { |
58 |
| - JSONObject loginPasswordObj = JSONObject.Create(); |
59 |
| - loginPasswordObj.AddField("username", username); |
60 |
| - loginPasswordObj.AddField("password", GetPasswordObj(password)); |
| 59 | + public IEnumerator CreateUserAndLogin(string username, string password) { |
| 60 | + JSONObject loginPasswordObj = JSONObject.Create(); |
| 61 | + loginPasswordObj.AddField("username", username); |
| 62 | + loginPasswordObj.AddField("password", GetPasswordObj(password)); |
61 | 63 |
|
62 |
| - MethodCall loginCall = connection.Call("createUser", loginPasswordObj); |
63 |
| - yield return loginCall.WaitForResult(); |
64 |
| - HandleLoginResult(loginCall); |
65 |
| - } |
| 64 | + MethodCall loginCall = connection.Call("createUser", loginPasswordObj); |
| 65 | + yield return loginCall.WaitForResult(); |
| 66 | + HandleLoginResult(loginCall); |
| 67 | + } |
66 | 68 |
|
67 |
| - public IEnumerator Login(string username, string password) { |
68 |
| - JSONObject userObj = JSONObject.Create(); |
69 |
| - userObj.AddField("username", username); |
| 69 | + public IEnumerator Login(string username, string password) { |
| 70 | + JSONObject userObj = JSONObject.Create(); |
| 71 | + userObj.AddField("username", username); |
70 | 72 |
|
71 |
| - JSONObject loginPasswordObj = JSONObject.Create(); |
72 |
| - loginPasswordObj.AddField("user", userObj); |
73 |
| - loginPasswordObj.AddField("password", GetPasswordObj(password)); |
| 73 | + JSONObject loginPasswordObj = JSONObject.Create(); |
| 74 | + loginPasswordObj.AddField("user", userObj); |
| 75 | + loginPasswordObj.AddField("password", GetPasswordObj(password)); |
74 | 76 |
|
75 |
| - MethodCall loginCall = connection.Call("login", loginPasswordObj); |
76 |
| - yield return loginCall.WaitForResult(); |
77 |
| - HandleLoginResult(loginCall); |
78 |
| - } |
| 77 | + MethodCall loginCall = connection.Call("login", loginPasswordObj); |
| 78 | + yield return loginCall.WaitForResult(); |
| 79 | + HandleLoginResult(loginCall); |
| 80 | + } |
79 | 81 |
|
80 |
| - public IEnumerator ResumeSession(string token) { |
81 |
| - JSONObject tokenObj = JSONObject.Create(); |
82 |
| - tokenObj.AddField("resume", token); |
| 82 | + public IEnumerator ResumeSession(string token) { |
| 83 | + JSONObject tokenObj = JSONObject.Create(); |
| 84 | + tokenObj.AddField("resume", token); |
83 | 85 |
|
84 |
| - MethodCall loginCall = connection.Call("login", tokenObj); |
85 |
| - yield return loginCall.WaitForResult(); |
86 |
| - HandleLoginResult(loginCall); |
87 |
| - } |
| 86 | + MethodCall loginCall = connection.Call("login", tokenObj); |
| 87 | + yield return loginCall.WaitForResult(); |
| 88 | + HandleLoginResult(loginCall); |
| 89 | + } |
| 90 | + |
| 91 | + public IEnumerator Logout() { |
| 92 | + MethodCall logoutCall = connection.Call("logout"); |
| 93 | + yield return logoutCall.WaitForResult(); |
| 94 | + HandleLogoutResult(logoutCall); |
| 95 | + } |
88 | 96 |
|
89 |
| - public IEnumerator Logout() { |
90 |
| - MethodCall logoutCall = connection.Call("logout"); |
91 |
| - yield return logoutCall.WaitForResult(); |
92 |
| - HandleLogoutResult(logoutCall); |
93 | 97 | }
|
94 | 98 |
|
95 | 99 | }
|
0 commit comments