]> diplodocus.org Git - nmh/blobdiff - sbr/oauth.c
showfile.c: Move interface to own file.
[nmh] / sbr / oauth.c
index 3aeeccbe407e541c47febf135b79e4a03a33f9bd..195b9b8fe77e8724cae524a971112b0850123943 100755 (executable)
@@ -5,7 +5,10 @@
  * complete copyright information.
  */
 
-#include <h/mh.h>
+#include "h/mh.h"
+#include "trimcpy.h"
+#include "getcpy.h"
+#include "error.h"
 
 #ifdef OAUTH_SUPPORT
 
 #include <unistd.h>
 
 #include <curl/curl.h>
-#include <thirdparty/jsmn/jsmn.h>
+#include "thirdparty/jsmn/jsmn.h"
 
-#include <h/oauth.h>
-#include <h/utils.h>
+#include "h/oauth.h"
+#include "h/utils.h"
 #include "lock_file.h"
 
 #define JSON_TYPE "application/json"
@@ -141,15 +144,15 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res,
     char *client_res;
 
     if (!mh_oauth_new (&ctx, svc))
-        adios(NULL, "%s", mh_oauth_get_err_string(ctx));
+        die("%s", mh_oauth_get_err_string(ctx));
 
     if (log != NULL) mh_oauth_log_to(stderr, ctx);
 
-    fn = mh_xstrdup(mh_oauth_cred_fn(svc));
+    fn = mh_oauth_cred_fn(svc);
     fp = lkfopendata(fn, "r+", &failed_to_lock);
     if (fp == NULL) {
         if (errno == ENOENT) {
-            adios(NULL, "no credentials -- run mhlogin -saslmech xoauth2 -authservice %s", svc);
+            die("no credentials -- run mhlogin -saslmech xoauth2 -authservice %s", svc);
         }
         adios(fn, "failed to open");
     }
@@ -158,24 +161,24 @@ mh_oauth_do_xoauth(const char *user, const char *svc, unsigned char **oauth_res,
     }
 
     if ((cred = mh_oauth_cred_load(fp, ctx, user)) == NULL) {
-        adios(NULL, "%s", mh_oauth_get_err_string(ctx));
+        die("%s", mh_oauth_get_err_string(ctx));
     }
 
     if (!mh_oauth_access_token_valid(time(NULL), cred)) {
         if (!mh_oauth_refresh(cred)) {
             if (mh_oauth_get_err_code(ctx) == MH_OAUTH_NO_REFRESH) {
-                adios(NULL, "no valid credentials -- run mhlogin -saslmech xoauth2 -authservice %s", svc);
+                die("no valid credentials -- run mhlogin -saslmech xoauth2 -authservice %s", svc);
             }
             if (mh_oauth_get_err_code(ctx) == MH_OAUTH_BAD_GRANT) {
-                adios(NULL, "credentials rejected -- run mhlogin -saslmech xoauth2 -authservice %s", svc);
+                die("credentials rejected -- run mhlogin -saslmech xoauth2 -authservice %s", svc);
             }
             inform("error refreshing OAuth2 token");
-            adios(NULL, "%s", mh_oauth_get_err_string(ctx));
+            die("%s", mh_oauth_get_err_string(ctx));
         }
 
         fseek(fp, 0, SEEK_SET);
         if (!mh_oauth_cred_save(fp, cred, user)) {
-            adios(NULL, "%s", mh_oauth_get_err_string(ctx));
+            die("%s", mh_oauth_get_err_string(ctx));
         }
     }
 
@@ -226,7 +229,7 @@ set_err_http(mh_oauth_ctx *ctx, const struct curl_ctx *curl_ctx)
     if (curl_ctx->res_len > 0
         && is_json(curl_ctx->content_type)
         && get_json_strings(curl_ctx->res_body, curl_ctx->res_len, ctx->log,
-                            "error", &error, (void *)NULL)
+                            "error", &error, NULL)
         && error != NULL) {
         if (strcmp(error, "invalid_grant") == 0) {
             code = MH_OAUTH_BAD_GRANT;
@@ -386,7 +389,7 @@ mh_oauth_get_authorize_url(mh_oauth_ctx *ctx)
                         "client_id", ctx->svc.client_id,
                         "redirect_uri", ctx->svc.redirect_uri,
                         "scope", ctx->svc.scope,
-                        (void *)NULL)) {
+                        NULL)) {
         set_err(ctx, MH_OAUTH_REQUEST_INIT);
         return NULL;
     }
@@ -410,7 +413,7 @@ cred_from_response(mh_oauth_cred *cred, const char *content_type,
                           "access_token", &access_token,
                           "expires_in", &expires_in,
                           "refresh_token", &refresh_token,
-                          (void *)NULL)) {
+                          NULL)) {
         goto out;
     }
 
@@ -498,7 +501,7 @@ mh_oauth_authorize(const char *code, mh_oauth_ctx *ctx)
                         "redirect_uri", ctx->svc.redirect_uri,
                         "client_id", ctx->svc.client_id,
                         "client_secret", ctx->svc.client_secret,
-                        (void *)NULL)) {
+                        NULL)) {
         set_err(ctx, MH_OAUTH_REQUEST_INIT);
         return NULL;
     }
@@ -531,7 +534,7 @@ mh_oauth_refresh(mh_oauth_cred *cred)
                         "refresh_token", cred->refresh_token,
                         "client_id", ctx->svc.client_id,
                         "client_secret", ctx->svc.client_secret,
-                        (void *)NULL)) {
+                        NULL)) {
         set_err(ctx, MH_OAUTH_REQUEST_INIT);
         return false;
     }
@@ -945,7 +948,7 @@ post(struct curl_ctx *ctx, const char *url, const char *req_body)
         curl_easy_setopt(curl, CURLOPT_DEBUGDATA, ctx->log);
     }
 
-    if ((status = curl_easy_setopt(curl, CURLOPT_URL, url)) != CURLE_OK) {
+    if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK) {
         return false;
     }
 
@@ -971,14 +974,10 @@ post(struct curl_ctx *ctx, const char *url, const char *req_body)
         return false;
     }
 
-    if ((status = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
-                                    &ctx->res_code)) != CURLE_OK
-        || (status = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE,
-                                       &ctx->content_type)) != CURLE_OK) {
-        return false;
-    }
-
-    return true;
+    return curl_easy_getinfo(curl,
+            CURLINFO_RESPONSE_CODE, &ctx->res_code) == CURLE_OK &&
+        curl_easy_getinfo(curl,
+            CURLINFO_CONTENT_TYPE, &ctx->content_type) == CURLE_OK;
 }
 
 /*******************************************************************************
@@ -1019,11 +1018,8 @@ parse_json(jsmntok_t **tokens, size_t *tokens_len,
            of the response body. */
         *tokens = mh_xrealloc(*tokens, *tokens_len * sizeof **tokens);
     }
-    if (r <= 0) {
-        return false;
-    }
 
-    return true;
+    return r > 0;
 }
 
 /*