diff options
Diffstat (limited to 'src/auth.c')
| -rw-r--r-- | src/auth.c | 60 | 
1 files changed, 60 insertions, 0 deletions
| diff --git a/src/auth.c b/src/auth.c new file mode 100644 index 0000000..b0ba98c --- /dev/null +++ b/src/auth.c @@ -0,0 +1,60 @@ +#define _POSIX_C_SOURCE 200809L +#include <err.h> +#include <string.h> +#include <jansson.h> +#include "string-util.h" +#include "auth.h" +#include "http.h" + +#define CLIENT_NAME "ap_client" + +char *instance_domain; +char *auth_token; + +static char *protocol = "https://"; +static char *app_register_url = "/api/v1/apps"; + +int register_app(char *instance) +{ +    json_t *root; +    json_error_t error; +    char *url; +    char *req_data; +    char *res_data; + +    root = json_pack_ex(&error, 1, "{s:s, s:s, s:s}", "client_name", +            CLIENT_NAME, "redirect_uris", "urn:ietf:wg:oauth:2.0:oob", "scopes", +            "read write push"); + +    if (!root) { +        fprintf(stderr, "register_app(): json pack error: line %d: %s\n", +                error.line, error.text); +        return -1; +    } + +    req_data = json_dumps(root, 0); +    json_decref(root); +    if (!req_data) { +        fprintf(stderr, "register_app(): failed to dump json\n"); +        return -1; +    } + +    size_t s = strlen(protocol) + strlen(instance) + strlen(app_register_url) + 1;  +    url = malloc(s); +    if (!url) { +        err(1, "register_app(): "); +    } + +    sprintf(url, "%s%s%s", protocol, instance, app_register_url); +    res_data = post_request(url, req_data); +    if (!res_data) { +        free(req_data); +        return -1; +    } + +    printf("res: \n%s\n", res_data); + +    free(req_data); +    free(res_data); +    return 0; +} | 
