summaryrefslogtreecommitdiff
path: root/src/modules/mega/mega_login.c
blob: 7cce9aabf97edefa321b4b772101df4536539ce0 (plain)
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
#include "mega.h"

#define URL "https://staging.api.mega.co.nz/"
#define APIKEY "SDKSAMPLE"

Eina_Bool
mega_login_send(Mega *mega, const char *json)
{
   Mega_Action *ma;
   char *data;
   size_t len;

   len = strlen(json);
   data = calloc(1, len + 3);
   data[0] = '[';
   strcpy(data+1, json);
   data[len+1] = ']';

   printf("sending %s\n", data);

   ma = calloc(1, sizeof(Mega_Action));
   ma->type = MEGA_LOGIN;
   ma->mega = mega;
   ma->u.al.buf = eina_strbuf_new();

   mega->action = ma;

   mega->ecu = ecore_con_url_new(URL"cs?id=fxbkjndgmz&ak="APIKEY);
   ecore_con_url_data_set(mega->ecu, mega);
   ecore_con_url_post(mega->ecu, data, len+2, NULL);
   free(data);
   return EINA_TRUE;
}

const char *
mega_login_json(const char *email, const char *hash)
{
   cJSON *json;
   char *s;

   json = cJSON_CreateObject();

   cJSON_AddItemToObject(json, "a", cJSON_CreateString("us"));
   cJSON_AddItemToObject(json, "user", cJSON_CreateString(email));
   cJSON_AddItemToObject(json, "uh", cJSON_CreateString(hash));

   s = cJSON_PrintUnformatted(json);
   cJSON_Delete(json);
   return s;
}

const char *
mega_login_encode(Mega *mega, const char *auth)
{
   char **s;
   const char *password_enc,
              *hash,
              *hashb64,
              *json;
   unsigned int nb;
   size_t len;

   s = eina_str_split_full(auth, ":", 2, &nb);
   password_enc = mega_utils_key_encode(s[1]);
   hash = mega_utils_login_hash(s[0], password_enc);
   hashb64 = mega_utils_base64_encode(hash, 8, &len);
   json = mega_login_json(s[0], hashb64);
   mega->info.email = strdup(s[0]);
   mega->info.password = strdup(s[1]);
   strncpy(mega->session.password, password_enc, 16);

   free((char *)password_enc);
   free(s[0]);
   free(s);
   return json;
}