support new auth system

This commit is contained in:
oupson 2022-02-25 09:10:58 +01:00
parent 8e311c221c
commit 96b073ea95
3 changed files with 101 additions and 46 deletions

View File

@ -1,6 +1,6 @@
.PHONY: all clean format
all: lib/libauthunivorleans.so lib/libauthunivorleans.a bin/test
all: lib/libauthunivorleans.so lib/libauthunivorleans.a bin/test bin/test_notes
CFLAGS += -Iinclude $(DEFINES) `pkg-config --cflags libcurl` -fPIC -Wall -Werror
@ -17,7 +17,7 @@ clean:
format:
clang-format src/*.c -i
clang-format include/*.h -i
clang-format include/**/*.h -i
CC ?= $(CC)
@ -34,5 +34,9 @@ lib/libauthunivorleans.a: obj/auth_univ_orleans.o obj/cookie_iterator.o obj/resu
$(AR) -cr $@ $^
bin/test: obj/test.o lib/libauthunivorleans.a
@mkdir -p $(@D)
$(CC) -o $@ $^ $(CFLAGS) `pkg-config --libs libcurl` `pkg-config --libs tidy`
bin/test_notes: obj/test_notes.o lib/libauthunivorleans.a
@mkdir -p $(@D)
$(CC) -o $@ $^ $(CFLAGS) `pkg-config --libs libcurl` `pkg-config --libs tidy`

View File

@ -7,10 +7,10 @@
#include <tidy/tidy.h>
#include <tidy/tidybuffio.h>
int __auth_univ_orleans_post_login(CURL *handle, char *lt, char *action,
int __auth_univ_orleans_post_login(CURL *handle, char *execution, char *action,
char *username, char *password);
int __get_action_and_lt(CURL *handle, char **lt, char **action);
int __get_action_and_execution(CURL *handle, char **execution, char **action);
size_t __dumb_write_callback(void *data, size_t size, size_t nmemb,
void *userp) {
@ -24,8 +24,8 @@ size_t __tidy_buf_write_cb(void *in, size_t size, size_t nmemb, void *out) {
return r;
}
int __get_action_and_lt_from_node(TidyDoc doc, TidyNode tnod, char **lt,
char **action);
int __get_action_and_execution_from_node(TidyDoc doc, TidyNode tnod,
char **execution, char **action);
result_t *auth_univ_get_session_token(char *username, char *password) {
CURL *handle = curl_easy_init();
@ -42,7 +42,6 @@ result_t *auth_univ_get_session_token(char *username, char *password) {
int res = auth_univ_orleans_login(handle, username, password);
if (res == CURLE_OK) {
cookie_iterator_t *list = new_cookie_iterator_from_curl(handle);
curl_easy_cleanup(handle);
@ -75,22 +74,22 @@ int auth_univ_orleans_login(CURL *handle, char *username, char *password) {
res = curl_easy_perform(handle);
if (res == CURLE_OK) {
char *lt = NULL;
char *execution = NULL;
char *action = NULL;
res = __get_action_and_lt(handle, &lt, &action);
res = __get_action_and_execution(handle, &execution, &action);
if (res == CURLE_OK) {
if (lt == NULL || action == NULL) {
if (execution == NULL || action == NULL) {
return 0;
}
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION,
__dumb_write_callback);
res = __auth_univ_orleans_post_login(handle, lt, action, username,
password);
res = __auth_univ_orleans_post_login(handle, execution, action,
username, password);
free(lt);
free(execution);
free(action);
return res;
} else {
@ -103,7 +102,7 @@ int auth_univ_orleans_login(CURL *handle, char *username, char *password) {
return 0;
}
int __get_action_and_lt(CURL *handle, char **lt, char **action) {
int __get_action_and_execution(CURL *handle, char **execution, char **action) {
TidyDoc tdoc;
TidyBuffer docbuf = {0};
// TidyBuffer tidy_errbuf = {0};
@ -111,9 +110,8 @@ int __get_action_and_lt(CURL *handle, char **lt, char **action) {
int err;
curl_easy_setopt(
handle, CURLOPT_URL,
"https://auth.univ-orleans.fr/cas/login?service=https://"
"ent.univ-orleans.fr/uPortal/"
"Login%3FrefUrl%3D%2FuPortal%2Ff%2Faccueil%2Fnormal%2Frender.uPn");
"https://auth.univ-orleans.fr/cas/"
"login?service=https%3A%2F%2Fnotes-iuto.univ-orleans.fr%2F");
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, __tidy_buf_write_cb);
tdoc = tidyCreate();
@ -129,8 +127,8 @@ int __get_action_and_lt(CURL *handle, char **lt, char **action) {
err = tidyParseBuffer(tdoc, &docbuf);
/* parse the input */ // TODO ERROR
if (err >= 0) {
err = __get_action_and_lt_from_node(tdoc, tidyGetRoot(tdoc), lt,
action); /* walk the tree */
err = __get_action_and_execution_from_node(
tdoc, tidyGetRoot(tdoc), execution, action); /* walk the tree */
}
}
@ -140,61 +138,75 @@ int __get_action_and_lt(CURL *handle, char **lt, char **action) {
return err;
}
int __auth_univ_orleans_post_login(CURL *handle, char *lt, char *action,
int __auth_univ_orleans_post_login(CURL *handle, char *execution, char *action,
char *username, char *password) {
char form_data[256];
char *encoded_lt = curl_easy_escape(handle, lt, strlen(lt));
char *encoded_execution =
curl_easy_escape(handle, execution, strlen(execution));
char *encoded_username =
curl_easy_escape(handle, username, strlen(username));
char *encoded_password =
curl_easy_escape(handle, password, strlen(password));
int nbr = snprintf(
form_data, 256,
"lt=%s&username=%s&password=%s&_eventId=submit&submit=SE+CONNECTER",
encoded_lt, encoded_username, encoded_password);
size_t form_data_size =
strlen(encoded_execution) + strlen(encoded_username) +
strlen(encoded_password) +
sizeof("username=&password=&_eventId=submit&execution=");
char *form_data = (char *)malloc(form_data_size * sizeof(char));
int nbr = snprintf(form_data, form_data_size,
"username=%s&password=%s&_eventId=submit&execution=%s",
encoded_username, encoded_password, encoded_execution);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, form_data);
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, nbr);
curl_easy_setopt(handle, CURLOPT_POST, 1L);
int action_size = strlen(action);
char *url = (char *)calloc(28 + action_size + 1, sizeof(char));
/*
int action_size = strlen(action);
char *url = (char *)calloc(33 + action_size + 1, sizeof(char));
memcpy(url, "https://auth.univ-orleans.fr", 29);
memcpy(url + 28, action, action_size);
memcpy(url, "https://auth.univ-orleans.fr/cas/", 33);
memcpy(url + 33, action, action_size);
*/
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_setopt(
handle, CURLOPT_URL, /* url*/
"https://auth.univ-orleans.fr/cas/"
"login?service=https%3A%2F%2Fnotes-iuto.univ-orleans.fr%2F");
int res = curl_easy_perform(handle);
free(url);
// free(url);
curl_free(encoded_lt);
curl_free(encoded_execution);
curl_free(encoded_username);
curl_free(encoded_password);
free(form_data);
return res;
}
int __get_action_and_lt_from_node(TidyDoc doc, TidyNode tnod, char **lt,
char **action) {
int __get_action_and_execution_from_node(TidyDoc doc, TidyNode tnod,
char **execution, char **action) {
TidyNode child;
for (child = tidyGetChild(tnod); child; child = tidyGetNext(child)) {
ctmbstr name = tidyNodeGetName(child);
if (name) {
TidyAttr attr;
int is_lt = 0;
int is_execution = 0;
int is_action = 0;
const char *value = NULL;
for (attr = tidyAttrFirst(child); attr; attr = tidyAttrNext(attr)) {
if (!is_lt && !is_action &&
if (!is_execution && !is_action &&
strcmp(tidyAttrName(attr), "name") == 0 &&
strcmp(tidyAttrValue(attr), "lt") == 0) // TODO OPTIMISE
strcmp(tidyAttrValue(attr), "execution") ==
0) // TODO OPTIMISE
{
is_lt = 1;
} else if (!is_action && !is_lt &&
is_execution = 1;
} else if (!is_action && !is_execution &&
strcmp(tidyAttrName(attr), "id") == 0 &&
strcmp(tidyAttrValue(attr), "fm1") ==
0) // TODO OPTIOMISE
@ -202,24 +214,25 @@ int __get_action_and_lt_from_node(TidyDoc doc, TidyNode tnod, char **lt,
is_action = 1;
} else if (strcmp(tidyAttrName(attr), "value") == 0) {
value = tidyAttrValue(attr);
} else if (!is_lt &&
} else if (!is_execution &&
strcmp(tidyAttrName(attr), "action") == 0) {
value = tidyAttrValue(attr);
}
}
if (is_lt && value != NULL) {
*lt = strdup(value);
if (is_execution && value != NULL) {
*execution = strdup(value);
} else if (is_action && value != NULL) {
*action = strdup(value);
}
}
if (*lt == NULL || *action == NULL) {
if (__get_action_and_lt_from_node(doc, child, lt, action) == 0) {
if (*execution == NULL || *action == NULL) {
if (__get_action_and_execution_from_node(doc, child, execution,
action) == 0) {
return 0;
}
}
}
return (*lt != NULL && *action != NULL) ? 0 : -1;
return (*execution != NULL && *action != NULL) ? 0 : -1;
}

38
src/test_notes.c Normal file
View File

@ -0,0 +1,38 @@
#include <auth_univ_orleans/auth_univ_orleans.h>
#include <auth_univ_orleans/cookie_iterator.h>
#include <stdlib.h>
size_t writeFunction(void *ptr, size_t size, size_t nmemb, void *data) {
fwrite(ptr, nmemb, size, stdout);
return size * nmemb;
}
int main(void) {
char *username = getenv("USERNAME");
char *password = getenv("PASSWORD");
if (username == NULL || password == NULL) {
fprintf(stderr, "ERROR : missing USERNAME or PASSWORD env var\n");
return -1;
}
CURL *handle = curl_easy_init();
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf);
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
int res = auth_univ_orleans_login(handle, username, password);
if (res != CURLE_OK) {
fprintf(stderr, "error : %s\n", errbuf);
return res;
}
curl_easy_setopt(handle, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_perform(handle);
curl_easy_cleanup(handle);
}