/
SasUserAccount.cs
311 lines (253 loc) · 8.61 KB
/
SasUserAccount.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Newtonsoft.Json.Linq;
using Sas;
using Sas.Data;
using UniRx;
using UnityEngine;
namespace Sas
{
namespace Component
{
public class AutoLoginFile
{
public uint version { get; set; }
public string token { get; set; }
public string password { get; set; }
public string signature { get; set; }
public AutoLoginFile ()
{
version = 1;
}
}
public class AccessDelReqParam
{
public IList<ulong> targets { get; set; }
}
public class AccountLoginReqParam
{
public int sns { get; set; }
public long sid { get; set; }
public byte[] email { get; set; }
public byte[] token { get; set; }
public int platform { get; set; }
public string equip { get; set; }
public string app { get; set; }
public string ver { get; set; }
public string os { get; set; }
public string model { get; set; }
public AccountLoginReqParam ()
{
sns = SNS.SAS.GetHashCode ();
sid = 0;
platform = Application.platform.GetHashCode ();
equip = SystemInfo.deviceUniqueIdentifier;
app = Application.identifier;
ver = Application.version;
os = SystemInfo.operatingSystem;
model = SystemInfo.deviceModel;
}
}
public class AccountSignUpReqParam
{
public int sns { get; set; }
public long sid { get; set; }
public byte[] email { get; set; }
public byte[] token { get; set; }
public byte[] public_key { get; set; }
public byte[] private_key { get; set; }
public byte[] authentication { get; set; }
public byte[] test { get; set; }
public AccountSignUpReqParam ()
{
sns = SNS.SAS.GetHashCode ();
sid = 0;
}
}
public class Account
{
Context context { get; set; }
byte[] authentication { get; set; }
public Account (Context ctx)
{
Debug.Assert (ctx != null);
context = ctx;
context.handler.Add ("/Account/Login", token => context.login_result = token.ToObject<AccountLoginResult> ());
context.handler.Add ("/Account/AccessOpen", token => {
var equip = token.ToObject<AccountAccessEquipResult> ();
context.login_result.equips = context.login_result.equips
.Where (e => e.id == equip.id)
.DefaultIfEmpty (null)
.Select (e => e != null ? e : equip)
.ToArray ();
});
context.handler.Add ("/Account/AccessDel", token => {
var targets = token.ToObject<ulong[]> ();
context.login_result.equips = context.login_result.equips
.Where (e => Array.Find (targets, t => e.id == t) == null)
.ToArray ();
});
}
~Account ()
{
context.handler.Del (new string[] {
"/Account/Login",
"/Account/Login",
"/Account/AccessDel"
});
}
static byte[] MakeAuthentication (JToken token)
{
var cipher = token.ToObject<byte[]> ();
using (var aes = new System.Security.Cryptography.AesManaged ()) {
using (var key = Sas.SecureHelper.APP_KEY)
aes.Key = key.GetByte (Convert.FromBase64String);
using (var iv = Sas.SecureHelper.APP_IV)
aes.IV = iv.GetByte (Convert.FromBase64String);
using (var question = aes.Decrypt (cipher.ToArray ())) {
var val = SasUtil.Evaluate (question.GetByte (b => b));
question.AppendChar ('=');
foreach (var ch in val.ToString ()) {
question.AppendChar (ch);
}
return question.GetByte (aes.Encrypt);
}
}
}
public UniRx.IObservable<string> Authentication ()
{
// 추후 런쳐 사
return context.Get ("/Account/Authentication")
.Select (result => {
authentication = MakeAuthentication (result.protocol.payload ["/Account/Authentication"]);
return result.protocol.command;
});
}
public UniRx.IObservable<Sas.Net.HTTPResult> SignUp (AccountSignupParam param)
{
return Observable.Range (0, 1)
.SelectMany (_ => {
var req = new AccountSignUpReqParam ();
using (var aes = new System.Security.Cryptography.AesManaged ()) {
using (var iv = SecureHelper.APP_IV) {
aes.IV = iv.GetByte (Convert.FromBase64String);
}
using (var key = SecureHelper.APP_KEY) {
aes.Key = key.GetByte (Convert.FromBase64String);
}
req.authentication = authentication;
req.token = param.password.GetByte (aes.Encrypt);
req.email = param.email.GetByte (aes.Encrypt);
using (var rsa = new System.Security.Cryptography.RSACryptoServiceProvider ()) {
req.public_key = aes.Encrypt (rsa.ToXmlString (false));
req.private_key = rsa.ExportPrivateKey ().GetByte (aes.Encrypt);
req.test = param.email.GetByte (rsa.Encrypt);
}
}
var json = JObject.FromObject (req);
return context.Post ("/Account/SignUp", json);
});
}
UniRx.IObservable<Net.HTTPResult> LoginAsSas (AccountLoginParam param)
{
return Observable.Range (0, 1)
.SelectMany (_ => {
var req = new AccountLoginReqParam ();
using (var aes = new System.Security.Cryptography.AesManaged ()) {
using (var iv = SecureHelper.APP_IV) {
aes.IV = iv.GetByte (Convert.FromBase64String);
}
using (var key = SecureHelper.APP_KEY) {
aes.Key = key.GetByte (Convert.FromBase64String);
}
req.email = param.email.GetByte (aes.Encrypt);
req.token = param.password.GetByte (aes.Encrypt);
}
var json = JObject.FromObject (req);
return context.Post ("/Account/Login", json);
});
}
UniRx.IObservable<Net.HTTPResult> LoginAsAuto (AccountLoginParam argc)
{
return Observable.Range (0, 1)
.SelectMany (_ => {
JsonDB db = new JsonDB ("account");
var obj = db.Get (argc.extension);
if (obj.Type == JTokenType.Null)
throw new Sas.Exception (Sas.ERRNO.LOCAL_HOST_NOT_FIND_EXIST.ToErrCode ());
AutoLoginFile body = obj.ToObject<AutoLoginFile> ();
;
var hashed = body.signature;
body.signature = null;
var json = Newtonsoft.Json.JsonConvert.SerializeObject (body);
var pbkdf = new Rfc2898DeriveBytes (json, SecureHelper.APP_SALT.GetByte (Convert.FromBase64String), 3483);
var signature = Convert.ToBase64String (pbkdf.GetBytes (256));
if (!string.Equals (signature, hashed))
throw new Sas.Exception (Sas.ERRNO.LOCAL_HOST_SIGNATURE_ERR.ToErrCode ());
var param = new AccountLoginReqParam ();
param.sns = SNS.AUTO.GetHashCode ();
param.token = Convert.FromBase64String (body.password);
var inst = context.requester.Post (context.server + "/Account/LoginAuto");
inst.mReq.Headers.Add ("sas-accesstoken", body.token);
return inst.Invoke (JObject.FromObject (param));
});
}
public UniRx.IObservable<Net.HTTPResult> Login (AccountLoginParam param)
{
return Observable.Range (0, 1)
.SelectMany (_ => {
switch (param.sns) {
case Data.SNS.AUTO:
return LoginAsAuto (param);
default:
break;
}
return LoginAsSas (param);
});
}
public UniRx.IObservable<Net.HTTPResult> AccessDel (IList<ulong> targets)
{
return Observable.Range (0, 1)
.SelectMany (_ => {
if (context.token == null)
throw new System.MissingFieldException ("context", "otp");
var param = new AccessDelReqParam { targets = targets };
return context.Post ("/Account/AccessDel", JObject.FromObject (param));
});
}
public UniRx.IObservable<Net.HTTPResult> AccessOpen ()
{
return Observable.Range (0, 1)
.SelectMany (_ => {
if (context.token == null)
throw new System.MissingFieldException ("context", "otp");
return context.Post ("/Account/AccessOpen", null);
});
}
public UniRx.IObservable<bool> DumpAutoLogin (string filename)
{
return Observable.Range (0, 1)
.Select (_ => {
if (context.accept_equip == null)
throw new System.MissingFieldException ("context", "accept_equip");
var body = new AutoLoginFile ();
body.token = context.token;
using (var rsa = new System.Security.Cryptography.RSACryptoServiceProvider ()) {
rsa.FromXmlString (context.public_key);
var encrypted = rsa.Encrypt (context.token_time.ToString ());
body.password = Convert.ToBase64String (encrypted);
}
var json = Newtonsoft.Json.JsonConvert.SerializeObject (body);
var pbkdf = new Rfc2898DeriveBytes (json, SecureHelper.APP_SALT.GetByte (Convert.FromBase64String), 3483);
body.signature = Convert.ToBase64String (pbkdf.GetBytes (256));
JsonDB db = new JsonDB ("account");
var obj = JToken.FromObject (body);
db.Put (filename, obj);
return true;
});
}
}
}
}