Skip to content

Commit

Permalink
add aw_sign for api sign
Browse files Browse the repository at this point in the history
  • Loading branch information
haipome committed Nov 2, 2017
1 parent 03ee624 commit ab06742
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 2 deletions.
4 changes: 2 additions & 2 deletions accessws/aw_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ int send_auth_request(nw_ses *ses, uint64_t id, struct clt_info *info, json_t *p
const char *source = json_string_value(json_array_get(params, 1));
if (source == NULL || strlen(source) >= SOURCE_MAX_LEN)
return send_error_invalid_argument(ses, id);
info->source = strdup(source);

nw_state_entry *entry = nw_state_add(state_context, settings.backend_timeout, 0);
struct state_data *state = entry->data;
Expand All @@ -155,7 +154,8 @@ int send_auth_request(nw_ses *ses, uint64_t id, struct clt_info *info, json_t *p
state->request_id = id;
state->info = info;

log_info("send auth request, token: %s", token);
log_info("send auth request, token: %s, source: %s", token, source);
info->source = strdup(source);
nw_job_add(job_context, entry->id, sdsnew(token));

return 0;
Expand Down
1 change: 1 addition & 0 deletions accessws/aw_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static int read_config_from_json(json_t *root)

ERR_RET(read_cfg_int(root, "worker_num", &settings.worker_num, false, 1));
ERR_RET(read_cfg_str(root, "auth_url", &settings.auth_url, NULL));
ERR_RET(read_cfg_str(root, "sign_url", &settings.sign_url, NULL));
ERR_RET(read_cfg_real(root, "backend_timeout", &settings.backend_timeout, false, 1.0));
ERR_RET(read_cfg_real(root, "cache_timeout", &settings.cache_timeout, false, 0.5));

Expand Down
1 change: 1 addition & 0 deletions accessws/aw_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct settings {

int worker_num;
char *auth_url;
char *sign_url;
double backend_timeout;
double cache_timeout;

Expand Down
5 changes: 5 additions & 0 deletions accessws/aw_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# include "aw_config.h"
# include "aw_server.h"
# include "aw_auth.h"
# include "aw_sign.h"
# include "aw_kline.h"
# include "aw_depth.h"
# include "aw_price.h"
Expand Down Expand Up @@ -123,6 +124,10 @@ int main(int argc, char *argv[])
if (ret < 0) {
error(EXIT_FAILURE, errno, "init auth fail: %d", ret);
}
ret = init_sign();
if (ret < 0) {
error(EXIT_FAILURE, errno, "init sing fail: %d", ret);
}
ret = init_kline();
if (ret < 0) {
error(EXIT_FAILURE, errno, "init kline fail: %d", ret);
Expand Down
7 changes: 7 additions & 0 deletions accessws/aw_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# include "aw_config.h"
# include "aw_server.h"
# include "aw_auth.h"
# include "aw_sign.h"
# include "aw_kline.h"
# include "aw_depth.h"
# include "aw_price.h"
Expand Down Expand Up @@ -156,6 +157,11 @@ static int on_method_server_auth(nw_ses *ses, uint64_t id, struct clt_info *info
return send_auth_request(ses, id, info, params);
}

static int on_method_server_sign(nw_ses *ses, uint64_t id, struct clt_info *info, json_t *params)
{
return send_sign_request(ses, id, info, params);
}

static int process_cache(nw_ses *ses, uint64_t id, sds key)
{
dict_entry *entry = dict_find(backend_cache, key);
Expand Down Expand Up @@ -863,6 +869,7 @@ static int init_svr(void)
ERR_RET_LN(add_handler("server.ping", on_method_server_ping));
ERR_RET_LN(add_handler("server.time", on_method_server_time));
ERR_RET_LN(add_handler("server.auth", on_method_server_auth));
ERR_RET_LN(add_handler("server.sign", on_method_server_sign));

ERR_RET_LN(add_handler("kline.query", on_method_kline_query));
ERR_RET_LN(add_handler("kline.subscribe", on_method_kline_subscribe));
Expand Down
214 changes: 214 additions & 0 deletions accessws/aw_sign.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* Description:
* History: [email protected], 2017/11/02, create
*/

# include <curl/curl.h>

# include "aw_config.h"
# include "aw_server.h"
# include "aw_asset.h"
# include "aw_order.h"
# include "aw_sign.h"

static nw_job *job_context;
static nw_state *state_context;

struct sign_request {
sds access_id;
sds authorisation;
uint64_t tonce;
};

struct state_data {
nw_ses *ses;
uint64_t ses_id;
uint64_t request_id;
struct clt_info *info;
};

static size_t post_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
sds *reply = userdata;
*reply = sdscatlen(*reply, ptr, size * nmemb);
return size * nmemb;
}

static void on_job(nw_job_entry *entry, void *privdata)
{
CURL *curl = curl_easy_init();
struct sign_request *request = entry->request;

sds reply = sdsempty();
sds token = sdsempty();
sds url = sdsempty();
struct curl_slist *chunk = NULL;

char *access_id = curl_easy_escape(curl, request->access_id, 0);
url = sdscatprintf(url, "%s?access_id=%s&tonce=%"PRIu64, settings.sign_url, access_id, request->tonce);
free(access_id);

token = sdscatprintf(token, "Authorization: %s", request->authorisation);
chunk = curl_slist_append(chunk, token);
chunk = curl_slist_append(chunk, "Accept-Language: en_US");
chunk = curl_slist_append(chunk, "Content-Type: application/json");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, post_write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &reply);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(settings.backend_timeout * 1000));

CURLcode ret = curl_easy_perform(curl);
if (ret != CURLE_OK) {
log_fatal("curl_easy_perform fail: %s", curl_easy_strerror(ret));
goto cleanup;
}

json_t *result = json_loads(reply, 0, NULL);
if (result == NULL)
goto cleanup;
entry->reply = result;

cleanup:
curl_easy_cleanup(curl);
sdsfree(reply);
sdsfree(token);
sdsfree(url);
curl_slist_free_all(chunk);
}

static void on_result(struct state_data *state, struct sign_request *request, json_t *result)
{
if (state->ses->id != state->ses_id)
return;
if (result == NULL)
goto error;

json_t *code = json_object_get(result, "code");
if (code == NULL)
goto error;
int error_code = json_integer_value(code);
if (error_code != 0) {
const char *message = json_string_value(json_object_get(result, "message"));
if (message == NULL)
goto error;
log_error("sign fail, access_id: %s, authorisation: %s, token: %"PRIu64" code: %d, message: %s",
request->access_id, request->authorisation, request->tonce, error_code, message);
send_error(state->ses, state->request_id, 11, message);
return;
}

json_t *data = json_object_get(result, "data");
if (data == NULL)
goto error;
struct clt_info *info = state->info;
uint32_t user_id = json_integer_value(json_object_get(data, "user_id"));
if (user_id == 0)
goto error;

if (info->auth && info->user_id != user_id) {
asset_unsubscribe(info->user_id, state->ses);
order_unsubscribe(info->user_id, state->ses);
}

info->auth = true;
info->user_id = user_id;
log_error("sign success, access_id: %s, user_id: %u", request->access_id, user_id);
send_success(state->ses, state->request_id);

return;

error:
if (result) {
char *reply = json_dumps(result, 0);
log_fatal("invalid reply: %s", reply);
free(reply);
}
send_error_internal_error(state->ses, state->request_id);
}

static void on_finish(nw_job_entry *entry)
{
nw_state_entry *state = nw_state_get(state_context, entry->id);
if (state == NULL)
return;
on_result(state->data, entry->request, entry->reply);
nw_state_del(state_context, entry->id);
}

static void on_cleanup(nw_job_entry *entry)
{
struct sign_request *request = entry->request;
sdsfree(request->access_id);
sdsfree(request->authorisation);
free(request);
if (entry->reply)
json_decref(entry->reply);
}

static void on_timeout(nw_state_entry *entry)
{
struct state_data *state = entry->data;
if (state->ses->id == state->ses_id) {
send_error_service_timeout(state->ses, state->request_id);
}
}

int send_sign_request(nw_ses *ses, uint64_t id, struct clt_info *info, json_t *params)
{
if (json_array_size(params) != 3)
return send_error_invalid_argument(ses, id);
const char *access_id = json_string_value(json_array_get(params, 0));
if (access_id == NULL)
return send_error_invalid_argument(ses, id);
const char *authorisation = json_string_value(json_array_get(params, 1));
if (authorisation == NULL)
return send_error_invalid_argument(ses, id);
uint64_t tonce = json_integer_value(json_array_get(params, 2));
if (tonce == 0)
return send_error_invalid_argument(ses, id);

nw_state_entry *entry = nw_state_add(state_context, settings.backend_timeout, 0);
struct state_data *state = entry->data;
state->ses = ses;
state->ses_id = ses->id;
state->request_id = id;
state->info = info;

log_info("send sign request, access_id: %s, authorisation: %s, tonce: %"PRIu64, access_id, authorisation, tonce);
info->source = strdup("api");

struct sign_request *request = malloc(sizeof(struct sign_request));
request->access_id = sdsnew(access_id);
request->authorisation = sdsnew(authorisation);
request->tonce = tonce;
nw_job_add(job_context, entry->id, request);

return 0;
}

int init_sign(void)
{
nw_job_type jt;
memset(&jt, 0, sizeof(jt));
jt.on_job = on_job;
jt.on_finish = on_finish;
jt.on_cleanup = on_cleanup;

job_context = nw_job_create(&jt, 10);
if (job_context == NULL)
return -__LINE__;

nw_state_type st;
memset(&st, 0, sizeof(st));
st.on_timeout = on_timeout;

state_context = nw_state_create(&st, sizeof(struct state_data));
if (state_context == NULL)
return -__LINE__;

return 0;
}

14 changes: 14 additions & 0 deletions accessws/aw_sign.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Description:
* History: [email protected], 2017/11/02, create
*/

# ifndef _AW_SIGN_H_
# define _AW_SIGN_H_

int init_sign(void);

int send_sign_request(nw_ses *ses, uint64_t id, struct clt_info *info, json_t *params);

# endif

1 change: 1 addition & 0 deletions accessws/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"backend_timeout": 1.0,
"cache_timeout": 10.0,
"auth_url": "http:https://192.168.1.6:8000/internal/exchange/user/auth",
"sign_url": "http:https://192.168.1.6:8000/internal/exchange/user/api/auth",
"depth_limit": [1, 5, 10, 20, 30, 50, 100],
"depth_merge": ["0", "0.0001", "0.001", "0.01", "0.1", "0.5", "1", "5", "10"]
}

0 comments on commit ab06742

Please sign in to comment.