-rw-r--r-- | test/.gitignore | 1 | ||||
-rw-r--r-- | test/Makefile.am | 17 | ||||
-rw-r--r-- | test/test-oauth-consumer.cc | 83 |
3 files changed, 94 insertions, 7 deletions
diff --git a/test/.gitignore b/test/.gitignore index 3d88495..7b234bd 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -7,3 +7,4 @@ /RP-db.cc /OP.cgi /OP-db.cc +/test-oauth-consumer diff --git a/test/Makefile.am b/test/Makefile.am index 8fedf48..f0c0ea8 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,4 +1,4 @@ -noinst_PROGRAMS = test idiscover RP.cgi OP.cgi +noinst_PROGRAMS = test idiscover RP.cgi OP.cgi test-oauth-consumer AM_CPPFLAGS=${CPPFLAGS_DEBUG} DEFAULT_INCLUDES = -I${top_builddir} @@ -14,15 +14,18 @@ EXTRA_DIST= \ idiscover_SOURCES = idiscover.cc idiscover_LDADD = ${top_builddir}/lib/libopkele.la +test_oauth_consumer_SOURCES = test-oauth-consumer.cc +test_oauth_consumer_LDADD = ${top_builddir}/lib/libopkele.la + if HAVE_SQLITE3 -if HAVE_KINGATE -if HAVE_UUID +if HAVE_UUID +if HAVE_KINGATE RP_cgi_SOURCES = RP.cc nodist_RP_cgi_SOURCES = RP-db.cc RP_cgi_LDADD = ${top_builddir}/lib/libopkele.la \ - ${SQLITE3_LIBS} ${KINGATE_LIBS} ${UUID_LIBS} -RP_cgi_CFLAGS = ${SQLITE3_CFLAGS} ${KINGATE_CFLAGS} ${UUID_CFLAGS} + ${SQLITE3_LIBS} ${KINGATE_LIBS} +RP_cgi_CFLAGS = ${SQLITE3_CFLAGS} ${KINGATE_CFLAGS} RP-db.cc: RP-db.sql ( \ @@ -46,6 +49,6 @@ OP-db.cc: OP-db.sql clean-local: rm -f RP-db.cc OP-db.cc -endif #HAVE_UUID -endif #HAVE_KINGATE +endif #HAVE_KINGATE +endif #HAVE_UUID endif #HAVE_SQLITE3 diff --git a/test/test-oauth-consumer.cc b/test/test-oauth-consumer.cc new file mode 100644 index 0000000..3b3ca70 --- a/dev/null +++ b/test/test-oauth-consumer.cc @@ -0,0 +1,83 @@ +#include <iostream> +#include <cassert> +#include <stdexcept> +using namespace std; +#include <openssl/sha.h> +#include <openssl/evp.h> +#include <openssl/hmac.h> +#include <opkele/exception.h> +#include <opkele/debug.h> +#include <opkele/util.h> +#include <opkele/util-internal.h> +#include <opkele/curl.h> +#include <opkele/oauth/consumer.h> + +ostream& operator<<(ostream& o,const opkele::oauth::token_t& t) { + o << "{ key: \"" << t.key << "\", secret: \"" << t.secret <<"\" }"; + return o; +} + +int main(int,char**) { + try { + opkele::oauth::simple_consumer sc( + opkele::oauth::simple_provider_endpoints( + "http://term.ie/oauth/example/request_token.php", + "http://term.ie/oauth/example/user_authorization.php", + "http://term.ie/oauth/example/access_token.php", + "HMAC-SHA1", opkele::oauth::oauth_post_body, + opkele::oauth::oauth_auth_header), + opkele::oauth::token_t( "key","secret" ) ); + opkele::oauth::token_t rt = sc.get_request_token(); + cout << "Request token: " << rt << endl; + cout << "Authorize URL: " << sc.get_authorize_url(rt) << endl; + opkele::oauth::token_t at = sc.get_access_token(rt); + cout << "Access token: " << at << endl; + + opkele::fields_t test; + test.set_field("foo","bar"); + opkele::util::curl_pick_t curl = opkele::util::curl_t::easy_init(); + opkele::oauth::http_request_t hr("POST", + "http://term.ie/oauth/example/echo_api.php"); + sc.prepare_request(hr, + opkele::fields_t(),test, + opkele::oauth::oauth_auth_header,"HMAC-SHA1", + &at,"realm"); + DOUT_("url: " << hr.url << endl + << "body: " << hr.body << endl + << "header: " << hr.authorize_header); + opkele::util::curl_slist_t rh; + rh.append("Authorization: "+hr.authorize_header); + CURLcode r; + (r=curl.misc_sets()) + || (r=curl.set_write()) + || (r=curl.easy_setopt(CURLOPT_HTTPHEADER,rh) ) + || (r=curl.easy_setopt(CURLOPT_URL,hr.url.c_str())) + || (r=curl.easy_setopt(CURLOPT_POST,1)) + || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,hr.body.c_str())) + || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,hr.body.size())); + if(r) + throw opkele::exception_curl(OPKELE_CP_ "failed to set curly options",r); + if( (r=curl.easy_perform()) ) + throw opkele::exception_curl(OPKELE_CP_ "failed to perform curly request",r); + DOUT_("Response: " << endl << curl.response); + +#ifdef OPKELE_HAVE_KONFORKA + }catch(konforka::exception& e) { + cerr + << "oops, caught " << opkele::util::abi_demangle(typeid(e).name()) << endl + << " what: " << e.what() << endl + << " where: " << e.where() << endl; + if(!e._seen.empty()) { + cerr << " seen:" << endl; + for(list<konforka::code_point>::const_iterator + i=e._seen.begin();i!=e._seen.end();++i) { + cerr << " " << i->c_str() << endl; + } + } +#endif + }catch(std::exception& e){ + cerr + << "oops, caught " << opkele::util::abi_demangle(typeid(e).name()) << endl + << " what: " << e.what() << endl; + } +} |