text
stringlengths
0
357
char* value_urlencoded = dc_urlencode(value);
dc_str_replace(uri, key, value_urlencoded);
free(value_urlencoded);
}
}
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
// from the jsmn parser example
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
static char* jsondup(const char *json, jsmntok_t *tok) {
if (tok->type == JSMN_STRING || tok->type == JSMN_PRIMITIVE) {
return strndup(json+tok->start, tok->end - tok->start);
}
return strdup("");
}
static int is_expired(dc_context_t* context)
{
time_t expire_timestamp = dc_sqlite3_get_config_int64(context->sql,
"oauth2_timestamp_expires", 0);
if (expire_timestamp<=0) {
return 0; // timestamp does never expire
}
if (expire_timestamp>time(NULL)) {
return 0; // expire timestamp is in the future and not yet expired
}
return 1; // expired
}
/**
* Get url that can be used to initiate an OAuth2 authorisation.
*
* If an OAuth2 authorization is possible for a given e-mail-address,
* this function returns the URL that should be opened in a browser.
*
* If the user authorizes access,
* the given redirect_uri is called by the provider.
* It's up to the UI to handle this call.
*
* The provider will attach some parameters to the url,
* most important the parameter `code` that should be set as the `mail_pw`.
* With `server_flags` set to #DC_LP_AUTH_OAUTH2,
* dc_configure() can be called as usual afterwards.
*
* Note: OAuth2 depends on #DC_EVENT_HTTP_POST;
* if you have not implemented #DC_EVENT_HTTP_POST in the ui,
* OAuth2 **won't work**.
*
* @memberof dc_context_t
* @param context The context object as created by dc_context_new().
* @param addr E-mail address the user has entered.
* In case the user selects a different e-mail-address during
* authorization, this is corrected in dc_configure()
* @param redirect_uri URL that will get `code` that is used as `mail_pw` then.
* Not all URLs are allowed here, however, the following should work:
* `chat.delta:/PATH`, `http://localhost:PORT/PATH`,
* `https://localhost:PORT/PATH`, `urn:ietf:wg:oauth:2.0:oob`
* (the latter just displays the code the user can copy+paste then)
* @return URL that can be opened in the browser to start OAuth2.
* If OAuth2 is not possible for the given e-mail-address, NULL is returned.
*/
char* dc_get_oauth2_url(dc_context_t* context, const char* addr,
const char* redirect_uri)
{
#define CLIENT_ID "959970109878-4mvtgf6feshskf7695nfln6002mom908.apps.googleusercontent.com"
oauth2_t* oauth2 = NULL;
char* oauth2_url = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC
|| redirect_uri==NULL || redirect_uri[0]==0) {
goto cleanup;
}
oauth2 = get_info(addr);
if (oauth2==NULL) {
goto cleanup;
}
dc_sqlite3_set_config(context->sql, "oauth2_pending_redirect_uri", redirect_uri);
oauth2_url = dc_strdup(oauth2->get_code);
replace_in_uri(&oauth2_url, "$CLIENT_ID", oauth2->client_id);
replace_in_uri(&oauth2_url, "$REDIRECT_URI", redirect_uri);
cleanup:
free(oauth2);
return oauth2_url;