2014-09-15 20:22:02 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
|
2014-09-17 11:20:23 +02:00
|
|
|
* 2014 Johannes Schauer <j.schauer@email.de>
|
2014-09-15 20:22:02 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License version 2, as published by
|
|
|
|
|
* the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2014-09-20 09:40:59 +02:00
|
|
|
#include <jansson.h>
|
|
|
|
|
#include <stdint.h>
|
2014-09-15 20:00:05 +02:00
|
|
|
#include <stdio.h>
|
2014-09-20 09:40:59 +02:00
|
|
|
#include <stdlib.h>
|
2014-09-15 20:00:05 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
#include "../../utils/http.h"
|
|
|
|
|
#include "../../utils/json.h"
|
2014-09-20 09:40:59 +02:00
|
|
|
#include "../../utils/strings.h"
|
|
|
|
|
#include "../mfconn.h"
|
2014-09-20 10:59:54 +02:00
|
|
|
#include "../apicalls.h" // IWYU pragma: keep
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
static int _decode_get_session_token(mfhttp * conn, void *data);
|
2014-09-18 09:11:00 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
struct user_get_session_token_response {
|
|
|
|
|
uint32_t secret_key;
|
|
|
|
|
char *secret_time;
|
|
|
|
|
char *session_token;
|
2014-09-18 09:11:00 +02:00
|
|
|
};
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
int
|
2014-09-21 15:59:52 +02:00
|
|
|
mfconn_api_user_get_session_token(mfconn * conn, const char *server,
|
|
|
|
|
const char *username, const char *password,
|
|
|
|
|
int app_id, const char *app_key,
|
2014-09-20 10:59:54 +02:00
|
|
|
uint32_t * secret_key,
|
2014-09-24 11:28:35 +02:00
|
|
|
char **secret_time, char **session_token)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
2014-09-20 10:59:54 +02:00
|
|
|
char *login_url;
|
|
|
|
|
char *post_args;
|
2014-09-21 15:59:52 +02:00
|
|
|
const char *user_signature;
|
2014-09-20 10:59:54 +02:00
|
|
|
int retval;
|
2014-09-18 09:11:00 +02:00
|
|
|
struct user_get_session_token_response response;
|
2014-09-20 10:59:54 +02:00
|
|
|
mfhttp *http;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (conn == NULL)
|
|
|
|
|
return -1;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
// configure url for operation
|
|
|
|
|
login_url = strdup_printf("https://%s/api/user/get_session_token.php",
|
2014-09-20 10:59:54 +02:00
|
|
|
server);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
// create user signature
|
2014-09-20 10:59:54 +02:00
|
|
|
user_signature =
|
2014-09-25 19:38:48 +02:00
|
|
|
mfconn_create_user_signature(conn, username, password, app_id,
|
|
|
|
|
app_key);
|
2014-09-20 10:59:54 +02:00
|
|
|
|
|
|
|
|
post_args = strdup_printf("email=%s"
|
|
|
|
|
"&password=%s"
|
2014-09-23 18:24:09 +02:00
|
|
|
"&application_id=%d"
|
2014-09-20 10:59:54 +02:00
|
|
|
"&signature=%s"
|
|
|
|
|
"&token_version=2"
|
|
|
|
|
"&response_format=json",
|
2014-09-23 18:24:09 +02:00
|
|
|
username, password, app_id, user_signature);
|
2014-09-21 15:59:52 +02:00
|
|
|
free((void *)user_signature);
|
2014-09-20 10:59:54 +02:00
|
|
|
|
|
|
|
|
http = http_create();
|
|
|
|
|
retval =
|
|
|
|
|
http_post_buf(http, login_url, post_args,
|
|
|
|
|
_decode_get_session_token, (void *)(&response));
|
2014-09-19 23:13:29 +02:00
|
|
|
http_destroy(http);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-17 11:20:23 +02:00
|
|
|
free(login_url);
|
|
|
|
|
free(post_args);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
*secret_key = response.secret_key;
|
|
|
|
|
*secret_time = response.secret_time;
|
|
|
|
|
*session_token = response.session_token;
|
|
|
|
|
|
2014-09-15 20:00:05 +02:00
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
static int _decode_get_session_token(mfhttp * conn, void *user_ptr)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
|
|
|
|
json_error_t error;
|
2014-09-20 10:59:54 +02:00
|
|
|
json_t *root = NULL;
|
|
|
|
|
json_t *data;
|
|
|
|
|
json_t *session_token;
|
|
|
|
|
json_t *secret_key;
|
|
|
|
|
json_t *secret_time;
|
2014-09-18 09:11:00 +02:00
|
|
|
struct user_get_session_token_response *response;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
if (user_ptr == NULL)
|
|
|
|
|
return -1;
|
2014-09-17 11:20:23 +02:00
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
response = (struct user_get_session_token_response *)user_ptr;
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
root = http_parse_buf_json(conn, 0, &error);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
data = json_object_by_path(root, "response");
|
2014-09-23 20:52:30 +02:00
|
|
|
if (data == NULL) {
|
|
|
|
|
fprintf(stderr, "json: no /response content\n");
|
2014-09-20 10:59:54 +02:00
|
|
|
return -1;
|
2014-09-23 20:52:30 +02:00
|
|
|
}
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
session_token = json_object_get(data, "session_token");
|
|
|
|
|
if (session_token == NULL) {
|
2014-09-15 20:00:05 +02:00
|
|
|
json_decref(root);
|
2014-09-23 20:52:30 +02:00
|
|
|
fprintf(stderr, "json: no /session_token content\n");
|
2014-09-15 20:00:05 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 09:11:00 +02:00
|
|
|
response->session_token = strdup(json_string_value(session_token));
|
2014-09-15 20:00:05 +02:00
|
|
|
|
2014-09-20 10:59:54 +02:00
|
|
|
secret_key = json_object_get(data, "secret_key");
|
|
|
|
|
if (secret_key != NULL)
|
2014-09-18 09:11:00 +02:00
|
|
|
response->secret_key = atoll(json_string_value(secret_key));
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
/*
|
2014-09-20 10:59:54 +02:00
|
|
|
time looks like a float but we must store it as a string to
|
|
|
|
|
remain congruent with the server on decimal place presentation.
|
|
|
|
|
*/
|
|
|
|
|
secret_time = json_object_get(data, "time");
|
|
|
|
|
if (secret_time != NULL)
|
|
|
|
|
response->secret_time = strdup(json_string_value(secret_time));
|
|
|
|
|
|
|
|
|
|
if (root != NULL)
|
|
|
|
|
json_decref(root);
|
2014-09-15 20:00:05 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sample user callback
|
|
|
|
|
/*
|
|
|
|
|
static void
|
2014-09-19 23:13:29 +02:00
|
|
|
_mycallback(char *data,size_t sz,cmffile *cfile)
|
2014-09-15 20:00:05 +02:00
|
|
|
{
|
|
|
|
|
double bytes_read;
|
|
|
|
|
double bytes_total;
|
|
|
|
|
|
|
|
|
|
bytes_read = cfile_get_rx_count(cfile);
|
|
|
|
|
bytes_total = cfile_get_rx_length(cfile);
|
|
|
|
|
|
|
|
|
|
printf("bytes read: %.0f\n\r",bytes_read);
|
|
|
|
|
|
|
|
|
|
if(bytes_read == bytes_total)
|
|
|
|
|
{
|
|
|
|
|
printf("transfer complete!\n\r");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
*/
|