Iddawc
Handle the flow of OAuth2 and OpenID Connect authentication process from the client side.
Iddawc API documentation

Iddawc is a C library used to implement OAuth2/OIDC clients according to the OAuth2 RFC and the OpenID Connect Specs.

It's based on Ulfius library for the HTTP requests and response management and Rhonabwy library for the JWKs management.

Iddawc supports the following features:

  • Loading openid-configuration endpoints and parsing the results
  • Making auth requests using the given parameters (client_id, client_secret, redirect_uri, etc.) and parsing the result
  • Making token requests using the given parameters (code, client_id, client_secret, redirect_uri, etc.) and parsing the result
  • Making userinfo, token introspection, token revocation requests
  • Parse responses, validate id_token
  • Registering new clients using the register endpoint if any
  • Sending signed and or encrypted requests in the auth and token endpoints

Return values

Lots of functions in Rhonabwy library return an int value. The returned value can be one of the following:

#define I_OK 0
#define I_ERROR 1
#define I_ERROR_PARAM 2
#define I_ERROR_MEMORY 3
#define I_ERROR_UNAUTHORIZED 4
#define I_ERROR_SERVER 5

If a function is successful, it will return I_OK (0), otherwise an error code is returned.

Global init and close

It's recommended to use i_global_init and i_global_close at the beginning and at the end of your program to initialize and cleanup internal values and settings. This will make outgoing requests faster, especially if you use lots of them, and dispatch your memory allocation functions in curl and Jansson if you changed them. These functions are NOT thread-safe, so you must use them in a single thread context.

int i_global_init()
Definition: iddawc.c:883
void i_global_close()
Definition: iddawc.c:892

Log messages

Usually, a log message is displayed to explain more specifically what happened on error. The log manager used is Yder. You can enable Yder log messages on the console with the following command at the beginning of your program:

int main() {
y_init_logs("Iddawc", Y_LOG_MODE_CONSOLE, Y_LOG_LEVEL_DEBUG, NULL, "Starting Iddawc client program");
// Do your code here
y_close_logs();
}

Example of an error log message:

2020-04-05T16:14:31 - Iddawc: i_run_auth_request - Unsupported auth_method

Go to Yder API Documentation for more details.

Core functions and struct _i_session * variables

Iddawc is based in the struct _i_session to store all the required parameters and results to work. You must use the init and clean functions before using a struct _i_session * and after finishing using it.

int i_init_session(struct _i_session * i_session);
void i_clean_session(struct _i_session * i_session);
int i_init_session(struct _i_session *i_session)
Definition: iddawc.c:901
void i_clean_session(struct _i_session *i_session)
Definition: iddawc.c:998
Definition: iddawc.h:168

Get or set properties

To set or get parameters stored in the struct _i_session *, you must use the appropriate function

int i_set_response_type(struct _i_session * i_session, uint i_value);
int i_set_result(struct _i_session * i_session, uint i_value);
int i_set_int_parameter(struct _i_session * i_session, i_option option, uint i_value);
int i_set_str_parameter(struct _i_session * i_session, i_option option, const char * s_value);
int i_set_additional_parameter(struct _i_session * i_session, const char * s_key, const char * s_value);
int i_set_additional_response(struct _i_session * i_session, const char * s_key, const char * s_value);
uint i_get_response_type(struct _i_session * i_session);
uint i_get_result(struct _i_session * i_session);
uint i_get_int_parameter(struct _i_session * i_session, i_option option);
const char * i_get_str_parameter(struct _i_session * i_session, i_option option);
const char * i_get_additional_parameter(struct _i_session * i_session, const char * s_key);
const char * i_get_additional_response(struct _i_session * i_session, const char * s_key);
int i_set_rich_authorization_request(struct _i_session * i_session, const char * type, const char * value);
int i_remove_rich_authorization_request(struct _i_session * i_session, const char * type);
char * i_get_rich_authorization_request(struct _i_session * i_session, const char * type);
i_option
Definition: iddawc.h:91
uint i_get_int_parameter(struct _i_session *i_session, i_option option)
Definition: iddawc.c:1871
uint i_get_result(struct _i_session *i_session)
Definition: iddawc.c:1867
int i_set_additional_parameter(struct _i_session *i_session, const char *s_key, const char *s_value)
Definition: iddawc.c:1570
int i_set_int_parameter(struct _i_session *i_session, i_option option, uint i_value)
Definition: iddawc.c:1059
char * i_get_rich_authorization_request(struct _i_session *i_session, const char *type)
Definition: iddawc.c:3393
const char * i_get_additional_parameter(struct _i_session *i_session, const char *s_key)
Definition: iddawc.c:2134
int i_set_result(struct _i_session *i_session, uint i_value)
Definition: iddawc.c:1055
const char * i_get_additional_response(struct _i_session *i_session, const char *s_key)
Definition: iddawc.c:2142
const char * i_get_str_parameter(struct _i_session *i_session, i_option option)
Definition: iddawc.c:1993
int i_remove_rich_authorization_request(struct _i_session *i_session, const char *type)
Definition: iddawc.c:3373
uint i_get_response_type(struct _i_session *i_session)
Definition: iddawc.c:1863
int i_set_response_type(struct _i_session *i_session, uint i_value)
Definition: iddawc.c:1051
int i_set_str_parameter(struct _i_session *i_session, i_option option, const char *s_value)
Definition: iddawc.c:1184
int i_set_additional_response(struct _i_session *i_session, const char *s_key, const char *s_value)
Definition: iddawc.c:1582
int i_set_rich_authorization_request(struct _i_session *i_session, const char *type, const char *value)
Definition: iddawc.c:3339

Import or export sessions

Iddawc supports importing or exporting struct _i_session *. The export format is JSON. Be careful, the JSON output is unsecured and contains all secrets and tokens without encryption!

You can import and export either in json_t * or char *, the char * format is a JSON stringified.

json_t * i_export_session_json_t(struct _i_session * i_session);
int i_import_session_json_t(struct _i_session * i_session, json_t * j_import);
char * i_export_session_str(struct _i_session * i_session);
int i_import_session_str(struct _i_session * i_session, const char * str_import);
char * i_export_session_str(struct _i_session *i_session)
Definition: iddawc.c:3137
int i_import_session_json_t(struct _i_session *i_session, json_t *j_import)
Definition: iddawc.c:3040
json_t * i_export_session_json_t(struct _i_session *i_session)
Definition: iddawc.c:2958
int i_import_session_str(struct _i_session *i_session, const char *str_import)
Definition: iddawc.c:3148

Run OAuth2 or OIDC requests

Finally, to run OAuth2 or OIDC requests, you must use the dedicated functions using the initialized and set struct _i_session * and some additional parameters if required.

Load openid-config

When available, you can load the Openid Config endpoint. This will parse the result and fill the struct _i_session * parameters with all the required results (auth endpoint, public keys, signature algorithms, etc.). Using this function required to have set the property I_OPT_OPENID_CONFIG_ENDPOINT.

int i_load_openid_config(struct _i_session * i_session);
int i_load_openid_config(struct _i_session *i_session)
Definition: iddawc.c:1694

Build and run auth request and parse results

The function i_build_auth_url_get can be used to build the full auth request with all the parameters in the URL query for a GET request.

int i_build_auth_url_get(struct _i_session * i_session);
int i_build_auth_url_get(struct _i_session *i_session)
Definition: iddawc.c:2150

The function i_run_auth_request builds the full auth requests and executes it. If the OAuth2 server answers with a successful response, the response will be parsed in the session properties. Otherwise, the redirect_to value and the errors if any will be parsed and made available in the session properties.

int i_run_auth_request(struct _i_session * i_session);
int i_run_auth_request(struct _i_session *i_session)
Definition: iddawc.c:2274

If the auth request is executed by an external program such as the browser, you can parse the redirect_to response afterwards using this function. You must set the I_OPT_REDIRECT_TO.

int i_parse_redirect_to(struct _i_session * i_session);
int i_parse_redirect_to(struct _i_session *i_session)
Definition: iddawc.c:1921

Build and run device authorization requests and parse results

If you need to run a device authorization request, you need to use the response type I_RESPONSE_TYPE_DEVICE_CODE and use the function i_run_device_auth_request, the parameter I_OPT_DEVICE_AUTHORIZATION_ENDPOINT must be set. On success, the parameters I_OPT_DEVICE_AUTH_CODE, I_OPT_DEVICE_AUTH_USER_CODE, I_OPT_DEVICE_AUTH_VERIFICATION_URI, I_OPT_DEVICE_AUTH_VERIFICATION_URI_COMPLETE, I_OPT_DEVICE_AUTH_EXPIRES_IN and I_OPT_DEVICE_AUTH_INTERVAL will be set. After that, you'll need to run i_run_token_request (see below) every few seconds until you get a success or a definitive error.

int i_run_device_auth_request(struct _i_session * i_session);
int i_run_device_auth_request(struct _i_session *i_session)
Definition: iddawc.c:3410

Build and run pushed authorization requests and parse results

If you need to run a device authorization request, you need to use the function i_run_par_request, and the other parameters as if the request was a normal auth request (response_type, client_id, scope, redirect_uri, authenticaiton type, etc.). On success, the parameters I_OPT_PUSHED_AUTH_REQ_URI and I_OPT_PUSHED_AUTH_REQ_EXPIRES_IN will be automatically set. Then the function i_build_auth_url_get will build an auth url using the request_uri and client_id parameters only.

int i_run_par_request(struct _i_session * i_session);
int i_run_par_request(struct _i_session *i_session)
Definition: iddawc.c:3482

Build and run token requests and parse results

If you need to execute a request in the token endpoint, to get a refresh token from a code or refresh a token for example,

int i_run_token_request(struct _i_session * i_session);
int i_run_token_request(struct _i_session *i_session)
Definition: iddawc.c:2379

Verify an id_token

If the auth or token endpoints returns an id_token, this one will be parsed, the signature will be verified and the content will be validated to make sure the id_token is valid. You can also manually validate an id_token using the dedicated function. The property I_OPT_ID_TOKEN and the public key property must be set.

int i_verify_id_token(struct _i_session * i_session);
int i_verify_id_token(struct _i_session *i_session)
Definition: iddawc.c:2645

Load userinfo

If an access_token is available, you can make a request to the userinfo endpoint to get information about the user. The function i_load_userinfo_custom is a more advanced userinfo request where you can specify query or header parameters, to request more claims or the result a signed JWT.

int i_load_userinfo(struct _i_session * i_session);
int i_load_userinfo_custom(struct _i_session * i_session, const char * http_method, struct _u_map * additional_query, struct _u_map * additional_headers);
int i_load_userinfo_custom(struct _i_session *i_session, const char *http_method, struct _u_map *additional_query, struct _u_map *additional_headers)
Definition: iddawc.c:1747
int i_load_userinfo(struct _i_session *i_session)
Definition: iddawc.c:1735

Introspect or revoke tokens

To execute introspection or revocation requests, you must set the session property I_OPT_TOKEN_TARGET and I_OPT_TOKEN_TARGET_TYPE_HINT if required.

int i_introspect_token(struct _i_session * i_session, json_t ** j_result);
int i_revoke_token(struct _i_session * i_session);
int i_revoke_token(struct _i_session *i_session)
Definition: iddawc.c:2785
int i_introspect_token(struct _i_session *i_session, json_t **j_result)
Definition: iddawc.c:2840

Register new clients

If available, you can register a new client. You may have to set a I_OPT_ACCESS_TOKEN property, depending on the server configuration. If update_session is true and the registration is successful, the properties I_OPT_CLIENT_ID and I_OPT_CLIENT_SECRET will be set to the session, and the first redirect_to entry will be used as I_OPT_REDIRECT_TO value.

int i_register_client(struct _i_session * i_session, json_t * j_parameters, int update_session, json_t ** j_result);
int i_register_client(struct _i_session *i_session, json_t *j_parameters, int update_session, json_t **j_result)
Definition: iddawc.c:2897

Generate a DPoP token

You can use your client's private key parameters to generate a DPoP token

char * i_generate_dpop_token(struct _i_session * i_session, const char * htm, const char * htu, time_t iat);
char * i_generate_dpop_token(struct _i_session *i_session, const char *htm, const char *htu, time_t iat)
Definition: iddawc.c:3166

Perform a HTTP request to a Resource Service

This features uses Ulfius' ulfius_send_http_request function to proceed. This function requires at least a struct _u_request with all the request parameters. Iddawc will add the access token previously obtained to the HTTP request using the Bearer usage specified. If the access token is expired, Iddawc will attempt to refresh the token. If specified, Iddawc will generate and add a DPoP token in the request using the request parameters.

int i_perform_api_request(struct _i_session * i_session, struct _u_request * http_request, struct _u_response * http_response, int refresh_if_expired, int bearer_type, int use_dpop, time_t dpop_iat);
int i_perform_api_request(struct _i_session *i_session, struct _u_request *http_request, struct _u_response *http_response, int refresh_if_expired, int bearer_type, int use_dpop, time_t dpop_iat)
Definition: iddawc.c:3249

Here is an example of how to use i_perform_api_request:

struct _i_session i_session;
struct _u_request req;
struct _u_response resp;
json_t * j_resp;
i_init_session(&i_session);
/*
* All the process to get an access token is hidden, this example considers the _i_session has an access token
*/
ulfius_init_request(&req);
ulfius_init_response(&resp);
ulfius_set_request_properties(&req, U_OPT_HTTP_VERB, "GET", U_OPT_HTTP_URL, "https://resource.tld/object", U_OPT_NONE);
if (i_perform_api_request(&i_session, &req, &resp, 1, I_BEARER_TYPE_HEADER, 1, 0) == I_OK && resp.status == 200) {
// j_resp contains the JSON response of the protected resource
j_resp = ulfius_get_json_body_response(&resp, NULL);
}
i_clean_session(&i_session);
ulfius_clean_request(&req);
ulfius_clean_response(&resp);
json_decref(j_resp);
#define I_BEARER_TYPE_HEADER
Bearer type header, the token will be available in the header.
Definition: iddawc.h:78
#define I_OK
Success.
Definition: iddawc.h:44