text stringlengths 0 357 |
|---|
} |
char* dc_get_oauth2_access_token(dc_context_t* context, const char* addr, |
const char* code, int flags) |
{ |
oauth2_t* oauth2 = NULL; |
char* access_token = NULL; |
char* refresh_token = NULL; |
char* refresh_token_for = NULL; |
char* redirect_uri = NULL; |
int update_redirect_uri_on_success = 0; |
char* token_url = NULL; |
time_t expires_in = 0; |
char* error = NULL; |
char* error_description = NULL; |
char* json = NULL; |
jsmn_parser parser; |
jsmntok_t tok[128]; // we do not expect nor read more tokens |
int tok_cnt = 0; |
int locked = 0; |
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC |
|| code==NULL || code[0]==0) { |
dc_log_warning(context, 0, "Internal OAuth2 error"); |
goto cleanup; |
} |
if ((oauth2=get_info(addr))==NULL) { |
dc_log_warning(context, 0, "Internal OAuth2 error: 2"); |
goto cleanup; |
} |
pthread_mutex_lock(&context->oauth2_critical); |
locked = 1; |
// read generated token |
if ( !(flags&DC_REGENERATE) && !is_expired(context) ) { |
access_token = dc_sqlite3_get_config(context->sql, "oauth2_access_token", NULL); |
if (access_token!=NULL) { |
goto cleanup; // success |
} |
} |
// generate new token: build & call auth url |
refresh_token = dc_sqlite3_get_config(context->sql, "oauth2_refresh_token", NULL); |
refresh_token_for = dc_sqlite3_get_config(context->sql, "oauth2_refresh_token_for", "unset"); |
if (refresh_token==NULL || strcmp(refresh_token_for, code)!=0) |
{ |
dc_log_info(context, 0, "Generate OAuth2 refresh_token and access_token..."); |
redirect_uri = dc_sqlite3_get_config(context->sql, "oauth2_pending_redirect_uri", "unset"); |
update_redirect_uri_on_success = 1; |
token_url = dc_strdup(oauth2->init_token); |
} |
else |
{ |
dc_log_info(context, 0, "Regenerate OAuth2 access_token by refresh_token..."); |
redirect_uri = dc_sqlite3_get_config(context->sql, "oauth2_redirect_uri", "unset"); |
token_url = dc_strdup(oauth2->refresh_token); |
} |
replace_in_uri(&token_url, "$CLIENT_ID", oauth2->client_id); |
replace_in_uri(&token_url, "$REDIRECT_URI", redirect_uri); |
replace_in_uri(&token_url, "$CODE", code); |
replace_in_uri(&token_url, "$REFRESH_TOKEN", refresh_token); |
json = (char*)context->cb(context, DC_EVENT_HTTP_POST, (uintptr_t)token_url, 0); |
if (json==NULL) { |
dc_log_warning(context, 0, "Error calling OAuth2 at %s", token_url); |
goto cleanup; |
} |
// generate new token: parse returned json |
jsmn_init(&parser); |
tok_cnt = jsmn_parse(&parser, json, strlen(json), tok, sizeof(tok)/sizeof(tok[0])); |
if (tok_cnt<2 || tok[0].type!=JSMN_OBJECT) { |
dc_log_warning(context, 0, "Failed to parse OAuth2 json from %s", token_url); |
goto cleanup; |
} |
for (int i = 1; i < tok_cnt; i++) { |
if (access_token==NULL && jsoneq(json, &tok[i], "access_token")==0) { |
access_token = jsondup(json, &tok[i+1]); |
} |
else if (refresh_token==NULL && jsoneq(json, &tok[i], "refresh_token")==0) { |
refresh_token = jsondup(json, &tok[i+1]); |
} |
else if (jsoneq(json, &tok[i], "expires_in")==0) { |
char* expires_in_str = jsondup(json, &tok[i+1]); |
if (expires_in_str) { |
time_t val = atol(expires_in_str); |
// val should be reasonable, maybe between 20 seconds and 5 years. |
// if out of range, we re-create when the token gets invalid, |
// which may create some additional load and requests wrt threads. |
if (val>20 && val<(60*60*24*365*5)) { |
expires_in = val; |
} |
free(expires_in_str); |
} |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.