text
stringlengths
0
357
else if (error==NULL && jsoneq(json, &tok[i], "error")==0) {
error = jsondup(json, &tok[i+1]);
}
else if (error_description==NULL && jsoneq(json, &tok[i], "error_description")==0) {
error_description = jsondup(json, &tok[i+1]);
}
}
if (error || error_description) {
dc_log_warning(context, 0, "OAuth error: %s: %s",
error? error : "unknown",
error_description? error_description : "no details");
// continue, errors do not imply everything went wrong
}
// update refresh_token if given, typically on the first round, but we update it later as well.
if (refresh_token && refresh_token[0]) {
dc_sqlite3_set_config(context->sql, "oauth2_refresh_token", refresh_token);
dc_sqlite3_set_config(context->sql, "oauth2_refresh_token_for", code);
}
// after that, save the access token.
// if it's unset, we may get it in the next round as we have the refresh_token now.
if (access_token==NULL || access_token[0]==0) {
dc_log_warning(context, 0, "Failed to find OAuth2 access token");
goto cleanup;
}
dc_sqlite3_set_config(context->sql, "oauth2_access_token", access_token);
dc_sqlite3_set_config_int64(context->sql, "oauth2_timestamp_expires",
expires_in? time(NULL)+expires_in-5/*refresh a bet before*/ : 0);
if (update_redirect_uri_on_success) {
dc_sqlite3_set_config(context->sql, "oauth2_redirect_uri", redirect_uri);
}
cleanup:
if (locked) { pthread_mutex_unlock(&context->oauth2_critical); }
free(refresh_token);
free(refresh_token_for);
free(redirect_uri);
free(token_url);
free(json);
free(error);
free(error_description);
free(oauth2);
return access_token? access_token : dc_strdup(NULL);
}
static char* get_oauth2_addr(dc_context_t* context, const oauth2_t* oauth2,
const char* access_token)
{
char* addr_out = NULL;
char* userinfo_url = NULL;
char* json = NULL;
jsmn_parser parser;
jsmntok_t tok[128]; // we do not expect nor read more tokens
int tok_cnt = 0;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC
|| access_token==NULL || access_token[0]==0 || oauth2==NULL) {
goto cleanup;
}
userinfo_url = dc_strdup(oauth2->get_userinfo);
replace_in_uri(&userinfo_url, "$ACCESS_TOKEN", access_token);
// should returns sth. as
// {
// "id": "100000000831024152393",
// "email": "NAME@gmail.com",
// "verified_email": true,
// "picture": "https://lh4.googleusercontent.com/-Gj5jh_9R0BY/AAAAAAAAAAI/AAAAAAAAAAA/IAjtjfjtjNA/photo.jpg"
// }
json = (char*)context->cb(context, DC_EVENT_HTTP_GET, (uintptr_t)userinfo_url, 0);
if (json==NULL) {
dc_log_warning(context, 0, "Error getting userinfo.");
goto cleanup;
}
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 userinfo.");
goto cleanup;
}
for (int i = 1; i < tok_cnt; i++) {
if (addr_out==NULL && jsoneq(json, &tok[i], "email")==0) {
addr_out = jsondup(json, &tok[i+1]);
}
}
if (addr_out==NULL) {
dc_log_warning(context, 0, "E-mail missing in userinfo.");
}
cleanup:
free(userinfo_url);