qlic

Zoho Cliq but not really
git clone git@getsh.org:qlic.git
Log | Files | Refs | LICENSE

qlic_oauth.h (5380B)


      1 #ifndef __QLIC_OAUTH_H
      2 #define __QLIC_OAUTH_H
      3 
      4 #include <oauth2.h>
      5 #include <stdio.h>
      6 #include <config.h>
      7 #include <nxjson.h>
      8 
      9 #include "qlic_types.h"
     10 #include "qlic_private.h"
     11 
     12 // TODO Choose between DB and text files for saving this information
     13 // If using text, choose between formats, yaml or json or other format, which is more suckless
     14 // If DB, sliqte3 is a good choice, but don't
     15 // Going to need a json parser and writer, might as well make the config files as json as well
     16 const char* saved_grant_token = NULL;
     17 const char* saved_access_token = NULL;
     18 /* const char* saved_grant_token = ""; */
     19 
     20 /* const char* saved_access_token = ""; */
     21 
     22 // TODO rethink this decision later, char* or const char* ?
     23 // Have to free the returning string at the end, who does that?
     24 char *json_access_code_transformer(char* str) {
     25 	const nx_json* json = nx_json_parse(str, nx_json_unicode_to_utf8);
     26 	if (json->type == NX_JSON_OBJECT) {
     27 		const nx_json* at = nx_json_get(json, "access_token");
     28 		if (at->type == NX_JSON_STRING) {
     29 			printf("str: %s\n", str);
     30 			const char* value = at->text_value;
     31 			size_t len = strlen(value);
     32 			char* copy = (char*)malloc(len);
     33 			strncpy(copy, value, len);
     34 			printf("copy: %s\n", copy);
     35 			return copy;
     36 		}
     37 	}
     38 	return NULL;
     39 }
     40 
     41 
     42 /*
     43  * returns >1 if the file is sucessfully read
     44  * returns the err value (<=0) in case of errors
     45  * return -2 if dest is NULL
     46  * returs -3 if dest_len is NULL
     47  */
     48 uint8_t read_contents(char* dest, size_t* dest_len, FILE* fp) {
     49 	if (dest == NULL) {
     50 		return -2;
     51 	}
     52 	if (dest_len == NULL) {
     53 		return -3;
     54 	}
     55 	int err = -1;
     56 	size_t res = *dest_len;
     57 	while ((res = fread(dest, 1, QLIC_FILE_BUFFER_SIZE, fp)) > 0) {
     58 		*dest_len += res;
     59 		if (res == 0) {
     60 			if((err = ferror(fp)) != 0) {
     61 				// error occured
     62 				return err;
     63 			} else if((err = feof(fp)) != 0) {
     64 				// return okay
     65 				return err;
     66 			}
     67 			/// unknown error
     68 			return -1;
     69 		} else {
     70 			// res > QLIC_FILE_READ_SIZE
     71 			dest = realloc(dest, *dest_len + QLIC_FILE_BUFFER_SIZE);
     72 		}
     73 	}
     74 	return -1;
     75 }
     76 
     77 QlicErrorCode qlic_read_config_file(QlicContext* ctx) {
     78 	/// @todo use xdg paths
     79 	FILE* fp = fopen("config.json", "r");
     80 	char* dest = malloc(sizeof(char) * QLIC_FILE_BUFFER_SIZE);
     81 	size_t dest_len = QLIC_FILE_BUFFER_SIZE;
     82 	read_contents(dest, &dest_len, fp);
     83 	const nx_json* json = nx_json_parse(dest, nx_json_unicode_to_utf8);
     84 	if (json->type == NX_JSON_OBJECT) {
     85 		const nx_json* at_user = nx_json_get(json, "user_id");
     86 		if (at_user == NULL) return QLIC_ERROR;
     87 		if (at_user->type == NX_JSON_OBJECT) {
     88 			const nx_json* at_client_id = nx_json_get(at_user, "client_id");
     89 			if (at_client_id == NULL) return QLIC_ERROR;
     90 			if (at_client_id->type == NX_JSON_STRING) {
     91 				qlic_error(at_client_id->text_value);
     92 				/// @todo maybe nxjson already string length has it?
     93 				__QLIC_ASSIGN_STRING(ctx->client_id, at_client_id->text_value);
     94 				qlic_error(ctx->client_id->string);
     95 				if (at_client_id->text_value == NULL) {
     96 					return QLIC_ERROR;
     97 				}
     98 			}
     99 			const nx_json* at_client_secret = nx_json_get(at_user, "client_secret");
    100 			if (at_client_secret == NULL) return QLIC_ERROR;
    101 			if (at_client_secret->type == NX_JSON_STRING) {
    102 				qlic_error(at_client_secret->text_value);
    103 				/// @todo maybe nxjson already string length has it?
    104 				__QLIC_ASSIGN_STRING(ctx->client_secret, at_client_secret->text_value);
    105 				qlic_error(ctx->client_secret->string);
    106 				if (at_client_secret->text_value == NULL) {
    107 					return QLIC_ERROR;
    108 				}
    109 			}
    110 		}
    111 	}
    112 	return QLIC_ERROR;
    113 }
    114 
    115 char* start_oauth_server(QlicContext* ctx) {
    116 	int err = -1;
    117 	err = qlic_read_config_file(ctx);
    118 	if (err) {
    119 		qlic_error("Not able to read config file");
    120 		QLIC_PANIC();
    121 	}
    122 
    123 	oauth2_config* conf = oauth2_init(ctx->client_id->string, ctx->client_secret->string);
    124 	if (conf == NULL) {
    125 		qlic_error("conf is null\n");
    126 		QLIC_PANIC();
    127 	}
    128 	conf->access_auth_code_transformer = json_access_code_transformer;
    129     oauth2_set_redirect_uri(conf, CLIQ_REDIRECT_URI);
    130 	// TODO generate true state instead of LOL
    131 	char* redir_uri = NULL;
    132 	char* grant_token = malloc(255 * sizeof(char));
    133 	if (saved_grant_token == NULL) {
    134 		redir_uri = oauth2_request_auth_code(conf, CLIQ_AUTH_ENDPOINT, CLIQ_SCOPE, "LOL", "online");
    135 
    136 		printf("Visit this url and hit authorize: %s\n", redir_uri);
    137 		printf("Now put the auth token here: ");
    138 
    139 		scanf("%s", grant_token);
    140 	} else {
    141 		strcpy(grant_token, saved_grant_token);
    142 	}
    143 
    144 	if (grant_token == NULL) {
    145 		printf("grant_token is null");
    146 		return NULL;
    147 	}
    148 
    149     //Now test token based auth
    150     char* access_token = NULL;
    151 	if (saved_access_token == NULL) {
    152 		access_token = oauth2_access_auth_code(conf, CLIQ_TOKEN_ENDPOINT, grant_token, CLIQ_SCOPE);
    153 		if (access_token == NULL) {
    154 			printf("access_token: %ld is null\n", (long)access_token);
    155 			return NULL;
    156 		}
    157 	} else {
    158 		access_token = malloc(255 * sizeof(char));
    159 		strcpy(access_token, saved_access_token);
    160 	}
    161 
    162     oauth2_set_auth_code(conf, access_token);
    163     printf("Access Token: %s\n", access_token);
    164     /* free(access_token); */
    165 
    166     /* printf("Enter your Facebook status: "); */
    167     /* char status[255]; */
    168     /* scanf("%s", status); */
    169     /* char status2[255]; */
    170     /* sprintf(status2, "message=%s", status); */
    171 
    172     /* access_token = oauth2_request(conf, "https://graph.facebook.com/slugonamission/feed", status2); */
    173 
    174     /* printf("%s\n", access_token); */
    175 
    176     oauth2_cleanup(conf);
    177 	return access_token;
    178 }
    179 #endif