Skip to content

Commit

Permalink
Merge pull request #26 from KIT-SCC/dev
Browse files Browse the repository at this point in the history
Dev: dynamic registration & token revocation
  • Loading branch information
zachmann authored Aug 28, 2017
2 parents 95fc68c + 01e4983 commit ec013b0
Show file tree
Hide file tree
Showing 19 changed files with 712 additions and 196 deletions.
1 change: 1 addition & 0 deletions src/file_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

char* getOidcDir() ;
oidc_error_t writeOidcFile(const char* filename, const char* text) ;
oidc_error_t writeFile(const char* filepath, const char* text) ;
char* readOidcFile(const char* filename) ;
char* readFile(const char* path);
int fileDoesExist(const char* path);
Expand Down
41 changes: 38 additions & 3 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ static size_t write_callback(void *ptr, size_t size, size_t nmemb, struct string
oidc_error_t CURLErrorHandling(int res, CURL* curl) {
switch(res) {
case CURLE_OK:
return 0;
{
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
syslog(LOG_AUTHPRIV|LOG_DEBUG, "Received status code %ld", http_code);
oidc_errno = OIDC_SUCCESS;
return OIDC_SUCCESS;
}
case CURLE_URL_MALFORMAT:
case CURLE_COULDNT_RESOLVE_HOST:
syslog(LOG_AUTHPRIV|LOG_ALERT, "%s (%s:%d) HTTPS Request failed: %s Please check the provided URLs.\n", __func__, __FILE__, __LINE__, curl_easy_strerror(res));
Expand All @@ -65,6 +71,7 @@ oidc_error_t CURLErrorHandling(int res, CURL* curl) {
default:
syslog(LOG_AUTHPRIV|LOG_ALERT, "%s (%s:%d) curl_easy_perform() failed: %s\n", __func__, __FILE__, __LINE__, curl_easy_strerror(res));
curl_easy_cleanup(curl);
oidc_errno = OIDC_EERROR;
return OIDC_EERROR;
}
}
Expand Down Expand Up @@ -139,6 +146,29 @@ void setPostData(CURL* curl, const char* data) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data_len);
}

/** @fn void setUrlEncodedData(CURL* curl, const char* data)
* @brief sets the data to be posted
* @param curl the curl instance
* @param data the data to be posted
*/
void setUrlEncodedData(CURL* curl, const char* data) {
long data_len = (long)strlen(data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data_len);
}

void setHeaders(CURL* curl, struct curl_slist* headers) {
if(headers) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
}

void setBasicAuth(CURL* curl, const char* username, const char* password) {
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERNAME, username);
curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
}

/** @fn int perform(CURL* curl)
* @brief performs the https request and checks for errors
* @param curl the curl instance
Expand All @@ -165,7 +195,7 @@ void cleanup(CURL* curl) {
* @return a pointer to the response. Has to be freed after usage. If the Https
* call failed, NULL is returned.
*/
char* httpsGET(const char* url, const char* cert_path) {
char* httpsGET(const char* url, struct curl_slist* headers, const char* cert_path) {
syslog(LOG_AUTHPRIV|LOG_DEBUG, "Https GET to: %s",url);
CURL* curl = init();
setUrl(curl, url);
Expand All @@ -174,6 +204,7 @@ char* httpsGET(const char* url, const char* cert_path) {
return NULL;
}
setSSLOpts(curl, cert_path);
setHeaders(curl, headers);
if(perform(curl)!=OIDC_SUCCESS) {
return NULL;
}
Expand All @@ -191,7 +222,7 @@ char* httpsGET(const char* url, const char* cert_path) {
* @return a pointer to the response. Has to be freed after usage. If the Https
* call failed, NULL is returned.
*/
char* httpsPOST(const char* url, const char* data, const char* cert_path) {
char* httpsPOST(const char* url, const char* data, struct curl_slist* headers, const char* cert_path, const char* username, const char* password) {
syslog(LOG_AUTHPRIV|LOG_DEBUG, "Https POST to: %s",url);
CURL* curl = init();
setUrl(curl, url);
Expand All @@ -202,6 +233,10 @@ char* httpsPOST(const char* url, const char* data, const char* cert_path) {
}
setPostData(curl, data);
setSSLOpts(curl, cert_path);
setHeaders(curl, headers);
if(username && password) {
setBasicAuth(curl, username, password);
}
if(perform(curl)!=OIDC_SUCCESS) {
return NULL;
}
Expand Down
6 changes: 4 additions & 2 deletions src/http.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef HTTP_H
#define HTTP_H

char* httpsGET(const char* url, const char* cert_path) ;
char* httpsPOST(const char* url, const char data[], const char* cert_path) ;
#include <curl/curl.h>

char* httpsGET(const char* url, struct curl_slist *list, const char* cert_path) ;
char* httpsPOST(const char* url, const char* data, struct curl_slist* headers, const char* cert_path, const char* username, const char* password) ;

#endif
7 changes: 5 additions & 2 deletions src/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

#include "oidc_array.h"

#define RESPONSE_SUCCESS_CLIENT "{\"status\":\"success\", \"client\":%s}"
#define RESPONSE_ERROR_CLIENT_INFO "{\"status\":\"failed\", \"error\":\"%s\", \"client\":%s, \"info\":\"%s\"}"
#define RESPONSE_STATUS_SUCCESS "{\"status\":\"success\"}"
#define RESPONSE_STATUS_ENDPOINT "{\"status\":\"%s\", \"token_endpoint\":\"%s\"}"
#define RESPONSE_STATUS_ENDPOINT_REFRESH "{\"status\":\"%s\", \"token_endpoint\":\"%s\", \"refresh_token\":\"%s\"}"
#define RESPONSE_STATUS_ENDPOINT "{\"status\":\"%s\", \"token_endpoint\":\"%s\", \"authorization_endpoint\":\"%s\", \"registration_endpoint\":\"%s\", \"revocation_endpoint\":\"%s\"}"
#define RESPONSE_STATUS_ENDPOINT_REFRESH "{\"status\":\"%s\", \"token_endpoint\":\"%s\", \"authorization_endpoint\":\"%s\", \"registration_endpoint\":\"%s\", \"revocation_endpoint\":\"%s\", \"refresh_token\":\"%s\"}"
#define RESPONSE_STATUS_ACCESS "{\"status\":\"%s\", \"access_token\":\"%s\"}"
#define RESPONSE_STATUS_PROVIDER "{\"status\":\"%s\", \"provider_list\":\"%s\"}"
#define RESPONSE_STATUS_REGISTER "{\"status\":\"%s\", \"response\":%s}"
#define RESPONSE_ERROR "{\"status\":\"failure\", \"error\":\"%s\"}"

#define REQUEST "{\"request\":\"%s\", %s}"
Expand Down
52 changes: 52 additions & 0 deletions src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "json.h"
#include "oidc_error.h"
#include "oidc_utilities.h"


/** @fn char* getJSONValue(const char* json, const char* key)
Expand Down Expand Up @@ -122,3 +123,54 @@ oidc_error_t checkParseResult(int r, jsmntok_t t) {
}


char* json_addValue(char* json, const char* key, const char* value) {
if(json==NULL || key==NULL || value==NULL) {
oidc_errno = OIDC_EARGNULL;
return json;
}
const char* const fmt = "%s, \"%s\":%s}";
int len = strlen(json);
if(json[len-1]!='}') {
oidc_errno = OIDC_EJSONADD;
return json;
}
json[len-1] = '\0';
char* tmp = calloc(sizeof(char), snprintf(NULL, 0, fmt, json, key, value)+1);
if(tmp==NULL) {
oidc_errno = OIDC_EALLOC;
return json;
}
sprintf(tmp, fmt, json, key, value);
if(tmp[1]==',') {
tmp[1]=' ';
}
clearFreeString(json);
oidc_errno = OIDC_SUCCESS;
return tmp;
}

char* json_addStringValue(char* json, const char* key, char* value) {
if(json==NULL || key==NULL || value==NULL) {
oidc_errno = OIDC_EARGNULL;
return json;
}
char* tmp = calloc(sizeof(char), strlen(value)+2+1);
if(tmp==NULL) {
oidc_errno = OIDC_EALLOC;
return json;
}
sprintf(tmp, "\"%s\"", value);
char* res = json_addValue(json, key, tmp);
clearFreeString(tmp);
return res;
}

int json_hasKey(char* json, const char* key) {
char* value = getJSONValue(json, key);
if(value) {
clearFreeString(value);
return 1;
} else {
return 0;
}
}
3 changes: 3 additions & 0 deletions src/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ int getJSONValues(const char* json, struct key_value* pairs, size_t size) ;
char* getValuefromTokens(jsmntok_t t[], int r, const char* key, const char* json) ;
int jsoneq(const char *json, jsmntok_t *tok, const char *s) ;
int checkParseResult(int r, jsmntok_t t) ;
char* json_addValue(char* json, const char* key, const char* value) ;
char* json_addStringValue(char* json, const char* key, char* value) ;
int json_hasKey(char* json, const char* key) ;

#endif // OIDC_JSON_H
5 changes: 3 additions & 2 deletions src/oidc-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
#include "prompt.h"
#include "ipc.h"
#include "oidc_utilities.h"
#include "version.h"

#define OIDC_SOCK_ENV_NAME "OIDC_SOCK"

const char *argp_program_version = "oidc-add 0.2.0";
const char *argp_program_version = ADD_VERSION;

const char *argp_program_bug_address = "<[email protected]>";
const char *argp_program_bug_address = BUG_ADDRESS;

/* This structure is used by main to communicate with parse_opt. */
struct arguments {
Expand Down
Loading

0 comments on commit ec013b0

Please sign in to comment.