commit ddc62a40ede4cff5abc2605214ea6042038a4ad5
parent 10ab811986af9d83cc476e2a1015838dc851f395
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Sun, 16 May 2021 23:32:12 +0200
write api tokens to ~/.diary-token
Diffstat:
4 files changed, 52 insertions(+), 13 deletions(-)
diff --git a/caldav.c b/caldav.c
@@ -41,6 +41,11 @@ static size_t curl_write_mem_callback(void * contents, size_t size, size_t nmemb
return realsize;
}
+static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
+ size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
+ return written;
+}
+
// todo
// https://beej.us/guide/bgnet/html
void* get_in_addr(struct sockaddr *sa) {
@@ -159,17 +164,30 @@ void set_access_token(char* code, char* verifier) {
curl = curl_easy_init();
// https://curl.se/libcurl/c/getinmemory.html
- struct curl_mem_chunk token_result;
- token_result.memory = malloc(1);
- token_result.size = 0;
+ //struct curl_mem_chunk token_result;
+ //token_result.memory = malloc(1);
+ //token_result.size = 0;
+
+ FILE *tokenfile;
+ char* tokenfile_path;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, GOOGLE_OAUTH_TOKEN_URL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfields);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_mem_callback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&token_result);
-
- res = curl_easy_perform(curl);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
+ //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_mem_callback);
+ //curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&token_result);
+
+ tokenfile_path = expand_path(CONFIG.google_tokenfile);
+ tokenfile = fopen(tokenfile_path, "wb");
+ if (tokenfile == NULL) {
+ perror("Failed to open tokenfile:");
+ fprintf(stderr, "Tokenfile: %s\n", tokenfile_path);
+ } else {
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, tokenfile);
+ res = curl_easy_perform(curl);
+ fclose(tokenfile);
+ }
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
@@ -177,9 +195,9 @@ void set_access_token(char* code, char* verifier) {
//fprintf(stderr, "Curl retrieved %lu bytes\n", (unsigned long)token_result.size);
//fprintf(stderr, "Curl content: %s\n", token_result.memory);
- access_token = extract_oauth_token(token_result.memory);
- token_ttl = extract_oauth_token_ttl(token_result.memory);
- refresh_token = extract_oauth_refresh_token(token_result.memory);
+ //access_token = extract_oauth_token(token_result.memory);
+ //token_ttl = extract_oauth_token_ttl(token_result.memory);
+ //refresh_token = extract_oauth_refresh_token(token_result.memory);
//fprintf(stderr, "Access token: %s\n", access_token);
//fprintf(stderr, "Token TTL: %i\n", token_ttl);
//fprintf(stderr, "Refresh token: %s\n", refresh_token);
@@ -345,7 +363,7 @@ void caldav_sync(struct tm* date, WINDOW* header) {
fprintf(stderr, "OAuth code: %s\n", code);
set_access_token(code, challenge);
- fprintf(stderr, "Access token: %s\n", access_token);
+ //fprintf(stderr, "Access token: %s\n", access_token);
char dstr[16];
mktime(date);
diff --git a/diary.c b/diary.c
@@ -321,8 +321,13 @@ bool read_config(const char* file_path)
CONFIG.editor = (char *) malloc(strlen(value_buf) + 1 * sizeof(char));
strcpy(CONFIG.editor, value_buf);
} else if (strcmp("google_tokenfile", key_buf) == 0) {
- CONFIG.google_tokenfile = (char *) malloc(strlen(value_buf) + 1 * sizeof(char));
- strcpy(CONFIG.google_tokenfile, value_buf);
+ wordexp_t tokenfile_wordexp;
+ if ( wordexp( value_buf, &tokenfile_wordexp, 0 ) == 0) {
+ // set expanded tokenfile path from config file
+ CONFIG.google_tokenfile = (char *) calloc(strlen(tokenfile_wordexp.we_wordv[0]) + 1, sizeof(char));
+ strcpy(CONFIG.google_tokenfile, tokenfile_wordexp.we_wordv[0]);
+ }
+ wordfree(&tokenfile_wordexp);
} else if (strcmp("google_clientid", key_buf) == 0) {
CONFIG.google_clientid = (char *) malloc(strlen(value_buf) + 1 * sizeof(char));
strcpy(CONFIG.google_clientid, value_buf);
diff --git a/utils.c b/utils.c
@@ -1,5 +1,17 @@
#include "utils.h"
+// Return expanded file path
+char* expand_path(char* str) {
+ char* res;
+ wordexp_t str_wordexp;
+ if ( wordexp( str, &str_wordexp, 0 ) == 0) {
+ res = (char *) calloc(strlen(str_wordexp.we_wordv[0]) + 1, sizeof(char));
+ strcpy(res, str_wordexp.we_wordv[0]);
+ }
+ wordfree(&str_wordexp);
+ return res;
+}
+
config CONFIG = {
.range = 1,
.weekday = 1,
diff --git a/utils.h b/utils.h
@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <time.h>
+#include <string.h>
+#include <wordexp.h>
#define GOOGLE_OAUTH_TOKEN_FILE "~/.diary-token"
#ifndef GOOGLE_OAUTH_CLIENT_ID
@@ -12,6 +14,8 @@
#define GOOGLE_OAUTH_CLIENT_SECRET ""
#endif
+char* expand_path(char* str);
+
typedef struct
{
// Path that holds the journal text files