Doug Anson / HTTPClient-SSL

Dependencies:   mbedTLSLibrary

Dependents:   SalesforceInterface df-2014-heroku-thermostat-k64f SalesforceInterface

Fork of HTTPClient by wolf SSL

Files at this revision

API Documentation at this revision

Comitter:
ansond
Date:
Wed Sep 17 21:36:14 2014 +0000
Parent:
35:16f0a44cc53e
Child:
37:29941a3bae90
Commit message:
updates

Changed in this revision

HTTPClient.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPClient.h Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPClient.cpp	Fri Sep 12 20:36:21 2014 +0000
+++ b/HTTPClient.cpp	Wed Sep 17 21:36:14 2014 +0000
@@ -26,7 +26,7 @@
 #include "rtos.h"
 
 //Debug is disabled by default
-#if 0
+#ifdef DEBUG_HTTP
 //Enable debug
 #include <cstdio>
 #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
@@ -114,7 +114,7 @@
 }
 
 HTTPClient::HTTPClient() :
-    m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0)
+    m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0), m_oauthToken(NULL)
 {
 
     /* CyaSSL_Debugging_ON() ; */
@@ -123,6 +123,9 @@
     ssl = 0 ;
     SSLver = 3 ; 
     m_basicAuthUser = NULL ;
+    m_basicAuthPassword = NULL;
+    m_httpResponseCode = 0;
+    m_oauthToken = NULL;
     redirect_url = NULL ;
     redirect = 0 ;
     header = NULL ;
@@ -133,6 +136,22 @@
 
 }
 
+HTTPResult HTTPClient::oauthToken(const char *token) { // OAUTH2 Authentication
+    // reset if called
+    if (m_oauthToken != NULL) free((void *)m_oauthToken);
+    m_oauthToken = NULL;
+    
+    // fill in if able...
+    if (token != NULL && strlen(token) > 0) {    
+        m_oauthToken = (char *)malloc(strlen(token)+1);
+        memset((void *)m_oauthToken,0,strlen(token)+1);
+        strcpy((char *)m_oauthToken,token);
+        this->basicAuth(NULL,NULL);
+    }
+    
+    return HTTP_OK ;
+}
+
 HTTPResult HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification
 {
     #define AUTHB_SIZE 128
@@ -143,12 +162,14 @@
     if (user != NULL) {
         m_basicAuthUser = (char *)malloc(strlen(user)+1);
         strcpy((char *)m_basicAuthUser, user);
+        this->oauthToken(NULL);
     }
     
     if (m_basicAuthPassword) free((void *)m_basicAuthPassword);
     if (password != NULL) {
         m_basicAuthPassword = (char *)malloc(strlen(password)+1);
         strcpy((char *)m_basicAuthPassword, password); 
+        this->oauthToken(NULL);
     }   
     
     return HTTP_OK ;
@@ -353,7 +374,12 @@
     //Send default headers
     DBG("Sending headers");
     if(m_basicAuthUser && m_basicAuthPassword) {
-        bAuth() ; /* send out Basic Auth header */        
+        DBG("Sending basic auth header");
+        bAuth() ;    /* send out Basic Auth header */        
+    }
+    else if (m_oauthToken) {
+        DBG("Sending OAUTH header");
+        tokenAuth(); /* send out OAUTH header */
     }
     if( pDataOut != NULL ) {
         if( pDataOut->getIsChunked() ) {
@@ -826,6 +852,17 @@
     return HTTP_OK;
 }
 
+HTTPResult HTTPClient::tokenAuth(void)
+{
+    HTTPResult ret ; 
+    ret = send("Authorization: Bearer ") ;
+    CHECK_CONN_ERR(ret);
+    DBG("oauthToken: %s", m_oauthToken) ;
+    ret = send((char *)m_oauthToken) ;
+    CHECK_CONN_ERR(ret); 
+    return HTTP_OK ;
+}
+
 HTTPResult HTTPClient::bAuth(void)
 {
     HTTPResult ret ;
--- a/HTTPClient.h	Fri Sep 12 20:36:21 2014 +0000
+++ b/HTTPClient.h	Wed Sep 17 21:36:14 2014 +0000
@@ -60,6 +60,13 @@
     ~HTTPClient();
 
     /**
+    Provides a OAUTH2 authentification feature 
+    Pass NULL pointer to switch back to no authentication
+    @param token OAUTH2 token
+    */
+    HTTPResult oauthToken(const char* token); // OAUTH2 Token Authentification
+    
+    /**
     Provides a basic authentification feature (Base64 encoded username and password)
     Pass two NULL pointers to switch back to no authentication
     @param user username to use for authentication, must remain valid durlng the whole HTTP session
@@ -142,6 +149,7 @@
     HTTPResult parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
     void cyassl_free(void) ;
     HTTPResult bAuth(void) ;
+    HTTPResult tokenAuth(void) ;
     HTTPResult readHeader(void) ;
     
     //Parameters
@@ -150,6 +158,7 @@
 
     const char* m_basicAuthUser;
     const char* m_basicAuthPassword;
+    const char* m_oauthToken;
     int m_httpResponseCode;
 
     const char * header ;