should be working.

si dovrebbe salvare i token e token_secret su file, ma sembra funzionare
testato con dropbox

Signed-off-by: Luca Fulchir <luca@fulchir.it>
parent fd773213
include "OAuth1Data.iol"
type OAuth1Init:void {
.location_request :string
.location_authorize :string
.location_access :string
.method :string
.consumer_key :string
.secret? :string
}
type OAuth1Token: void {
.location_request :string
.location_authorize :string
.location_access :string
.method :string
.consumer_key :string
.secret? :string
.token :string
.token_secret :string
}
interface OAuth1Interface {
RequestResponse:
RequestToken (OAuth1Init) (OAuth1Token) throws wrong_answer,
getReferral (OAuth1Token) (string)
RequestToken (OAuth1Info) (OAuth1Info) throws wrong_answer,
getReferral (OAuth1Info) (string),
getAccess (OAuth1Info) (tokenPair)
}
outputPort OAuth1 {
......
......@@ -12,6 +12,26 @@ execution { concurrent}
main {
[RequestToken (where) (token) {
http_data.URI = where.location_request;
toUpperCase@StringUtils (where.method) (http_data.method);
where.method = http_data.method;
http_data.headers[0].name = "userAgent";
http_data.headers[0].value = "Jolie-OAuth";
http_data.headers[1].name = "Authorization";
DataRequestToken@OAuth1Data (where) (http_data.headers[1].value);
sendRequest@genericHTTP(http_data) (answer);
parseAnswer@OAuth1Data(answer) (result);
token << where;
token.token = result.token;
token.token_secret = result.token_secret
}]{nullProcess}
[getReferral (info) (output) {
http_data.URI = info.location_authorize;
http_data.headers[0].name = "userAgent";
......@@ -26,74 +46,18 @@ main {
}] {nullProcess}
[RequestToken (where) (token) {
http_data.URI = where.location_request;
toUpperCase@StringUtils (where.method) (http_data.method);
where.method = http_data.method;
[getAccess (info) (output) {
http_data.URI = info.location_access;
http_data.headers[0].name = "userAgent";
http_data.headers[0].value = "Jolie-OAuth";
http_data.method = "POST";
http_data.headers[1].name = "Authorization";
http_data.headers[1].value = "";
DataRequestToken@OAuth1Data (where) (http_data.headers[1].value);
DataAuthenticate@OAuth1Data (info) (http_data.headers[1].value);
sendRequest@genericHTTP(http_data) (answer);
answer.word = "&";
indexOf@StringUtils (answer) (index);
undef(answer.word);
// TODO: UNDOCOMENTED: what if we can not find the string??
parseAnswer@OAuth1Data (answer) (output)
answer.begin = 0;
answer.end = index;
substring@StringUtils(answer) (tmp1);
undef(answer.begin);
undef(answer.end);
length@StringUtils(answer) (answer.end);
answer.begin = index + 1;
substring@StringUtils(answer) (tmp2);
// now search "oauth_token" and "oauth_token_secret"
// and put them in the right data structure
tmp1.prefix="oauth_token";
startsWith@StringUtils (tmp1) (prefix1Ok);
tmp2.prefix="oauth_token_secret";
startsWith@StringUtils (tmp2) (prefix2Ok);
token << where;
if (prefix1Ok && prefix2Ok) {
// found them, right order. now extract only the tokens
undef(tmp1.prefix);
length@StringUtils(tmp1) (tmp1.end);
tmp1.begin = 12;
substring@StringUtils(tmp1) (token.token);
undef(tmp2.prefix);
length@StringUtils(tmp2) (tmp2.end);
tmp2.begin = 19;
substring@StringUtils(tmp2) (token.token_secret)
} else {
// wrong order ?
tmp1.prefix = "oauth_token_secret";
tmp2.prefix = "oauth_token";
startsWith@StringUtils (tmp1) (prefix1Ok);
startsWith@StringUtils (tmp2) (prefix2Ok);
if (prefix1Ok && prefix2Ok) {
// found them, wrong order. now extract only the tokens
undef(tmp2.prefix);
length@StringUtils(tmp2) (tmp2.end);
tmp2.begin = 12;
substring@StringUtils(tmp2) (token.token);
undef(tmp1.prefix);
length@StringUtils(tmp1) (tmp1.end);
tmp1.begin = 19;
substring@StringUtils(tmp1) (token.token_secret)
} else {
throw (wrong_answer)
}
}
}]{nullProcess}
}] {nullProcess}
}
include "console.iol"
include "OAuth1.iol"
include "security_utils.iol"
main
{
// data needed for the connection: where to connect, how and who we are
data.location_request = "https://api.dropbox.com/1/oauth/request_token";
data.location_authorize = "https://www.dropbox.com/1/oauth/authorize";
data.location_access = "https://api.dropbox.com/1/oauth/access_token";
data.method = "post";
data.consumer_key = "27250pnzil7tmhx";
data.secret = "6yj3c6mjbdixm6i";
// send the message
RequestToken@OAuth1 (data) (answer);
println@Console (answer.token)();
println@Console (answer.token_secret)();
getReferral@OAuth1 (answer) (referr);
println@Console (">>>>" + referr)()
}
type OAuth1Locations:void {
type OAuth1Info:void {
.location_request :string
.location_authorize :string
.location_access :string
......@@ -7,11 +7,20 @@ type OAuth1Locations:void {
.callback? :string
.consumer_key :string
.secret? :string
.token? :string
.token_secret? :string
}
type tokenPair:void {
.token :string
.token_secret :string
}
interface OAuth1DataInterface {
RequestResponse:
DataRequestToken (OAuth1Locations) (string)
DataRequestToken (OAuth1Info) (string),
DataAuthenticate (OAuth1Info) (string),
parseAnswer (string) (tokenPair) throws wrong_answer
}
outputPort OAuth1Data {
Interfaces: OAuth1DataInterface
......
......@@ -76,6 +76,115 @@ scope (error_handler) {
"oauth_callback=\"" + where.callback + "\","
}
}]{nullProcess}
[DataAuthenticate (info) (postString)
{
// getCurrentTimeMillis is a temporary interface, but there's
// nothing better :(
getCurrentTimeMillis@Time(void)(tmp_timestamp);
tmp_timestamp = tmp_timestamp / 1000;
// a little too much as a nonce, but it's easier this way :p
createSecureToken@SecurityUtils(void)(oauth_nonce);
toSort.item[0] = "oauth_signature_method=HMAC-SHA1";
toSort.item[1] = "oauth_timestamp=" + tmp_timestamp;
toSort.item[2] = "oauth_nonce=" + oauth_nonce;
toSort.item[3] = "oauth_version=1.0";
toSort.item[4] = "oauth_consumer_key=" + info.consumer_key;
toSort.item[5] = "oauth_token=" + info.token;
sort@StringUtils (toSort) (sorted);
toEncode.encoding = "UTF8";
toEncode.input = info.location_access;
encode@URLEncoder (toEncode) (encodedLocation);
tmp = "";
for (i = 0, i < (#toSort.item - 1), i++) {
tmp = tmp + sorted.item[i] + "&"
};
tmp = tmp + sorted.item[#sorted.item - 1];
toEncode.input = tmp;
encode@URLEncoder (toEncode) (encodedParams);
toHash.secret = info.secret + "&" + info.token_secret;
toHash.data = info.method + "&" + encodedLocation + "&" + encodedParams;
sha1@Hmac (toHash) (tmpSignature);
toEncode.input = tmpSignature;
encode@URLEncoder (toEncode) (signature);
postString = "OAuth" +
"oauth_consumer_key=\"" + info.consumer_key + "\"," +
"oauth_signature_method=\"HMAC-SHA1\"," +
"oauth_timestamp=\"" + tmp_timestamp + "\"," +
"oauth_nonce=\"" + oauth_nonce + "\"," +
"oauth_version=\"1.0\"," +
"oauth_signature=\"" + signature + "\""
}]{nullProcess}
[parseAnswer (answer) (token) {
scope (parsing) {
install( StringIndexOutOfBoundsException =>
throw(wrong_answer)
);
answer.word = "&";
indexOf@StringUtils (answer) (index);
undef(answer.word);
// TODO: UNDOCOMENTED: what if we can not find the string??
answer.begin = 0;
answer.end = index;
substring@StringUtils(answer) (tmp1);
undef(answer.begin);
undef(answer.end);
length@StringUtils(answer) (answer.end);
answer.begin = index + 1;
substring@StringUtils(answer) (tmp2);
undef(answer.begin);
undef(answer.end);
// now search "oauth_token" and "oauth_token_secret"
// and put them in the right data structure
tmp1.prefix="oauth_token";
startsWith@StringUtils (tmp1) (prefix1Ok);
tmp2.prefix="oauth_token_secret";
startsWith@StringUtils (tmp2) (prefix2Ok);
if (prefix1Ok && prefix2Ok) {
// found them, right order. now extract only the tokens
undef(tmp1.prefix);
length@StringUtils(tmp1) (tmp1.end);
tmp1.begin = 12;
substring@StringUtils(tmp1) (token.token);
undef(tmp2.prefix);
length@StringUtils(tmp2) (tmp2.end);
tmp2.begin = 19;
substring@StringUtils(tmp2) (token.token_secret)
} else {
// wrong order ?
tmp1.prefix = "oauth_token_secret";
tmp2.prefix = "oauth_token";
startsWith@StringUtils (tmp1) (prefix1Ok);
startsWith@StringUtils (tmp2) (prefix2Ok);
if (prefix1Ok && prefix2Ok) {
// found them, wrong order. now extract only the tokens
undef(tmp2.prefix);
length@StringUtils(tmp2) (tmp2.end);
tmp2.begin = 12;
substring@StringUtils(tmp2) (token.token);
undef(tmp1.prefix);
length@StringUtils(tmp1) (tmp1.end);
tmp1.begin = 19;
substring@StringUtils(tmp1) (token.token_secret)
} else {
throw (wrong_answer)
}
}
}
}]{nullProcess}
}
......
/***************************************************************************
* Copyright (C) 2013 by Luca Fulchir <luca@fulchir.it> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
include "console.iol"
include "console.iol"
include "OAuth1.iol"
include "security_utils.iol"
include "jolie/net/utils/genericHTTP.iol"
include "jolie/net/utils/URLEncoder.iol"
main
{
createSecureToken@SecurityUtils(void)(uff);
println@Console( "uuid: " + uff )();
input.input = "http://printer.example.com/ready";
input.encoding = "UTF-8";
// data needed for the connection: where to connect, how and who we are
data.location_request = "https://api.dropbox.com/1/oauth/request_token";
data.location_authorize = "https://www.dropbox.com/1/oauth/authorize";
data.location_access = "https://api.dropbox.com/1/oauth/access_token";
data.method = "post";
data.consumer_key = "27250pnzil7tmhx";
data.secret = "6yj3c6mjbdixm6i";
encode@URLEncoder(input)(output);
println@Console( "Answer is " + output )();
// send the message
RequestToken@OAuth1 (data) (answer);
println@Console ("token: " + answer.token)();
println@Console ("token_secret: " + answer.token_secret)();
http.URI = "http://codepedia.eu/test";
http.method = "POST";
http.headers[0].name="Auth";
http.headers[0].value="authvalue";
http.parameters[0].name="lol";
http.parameters[0].value="lolvalue 42/asdf";
getReferral@OAuth1 (answer) (referr);
println@Console ("auth this app with: " + referr)();
sendRequest@genericHTTP (http) (answer);
println@Console( "Post is " + answer )()
getAccess@OAuth1 (answer) (tokens);
println@Console ("token: " + tokens.token)();
println@Console ("secret: " + tokens.token_secret)()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment