fix headers loop & refreshOauth2

Signed-off-by: Luca Fulchir <luca@fulchir.it>
parent 1d700727
......@@ -19,7 +19,7 @@
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
include "jolie/net/utils/HTTP.iol"
include "../utils/HTTP.iol"
include "string_utils.iol"
include "console.iol"
include "OAuth1Types.iol"
......
......@@ -24,7 +24,7 @@ include "time.iol"
include "string_utils.iol"
include "OAuth1Types.iol"
include "jolie/net/utils/Hmac.iol"
include "jolie/net/utils/HTTP.iol"
include "../utils/HTTP.iol"
execution {sequential}
inputPort OAuth1Data{
......
......@@ -19,14 +19,7 @@
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
include "OAuth2Data.iol"
interface OAuth2Interface {
RequestResponse:
Auth (OAuth2Info) (string),
Access (OAuth2Info) (OAuth2Info),
Refresh (OAuth2Info) (OAuth2Info),
}
include "OAuth2Types.iol"
outputPort OAuth2 {
Interfaces: OAuth2Interface
......
......@@ -19,7 +19,8 @@
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
include "OAuth2.iol"
include "OAuth2Data.iol"
include "../utils/HTTP.iol"
include "string_utils.iol"
include "console.iol"
......@@ -31,7 +32,7 @@ inputPort OAuth2 {
execution {concurrent}
main {
Auth (info) (answer) {
[Auth (info) (answer) {
http_data.URI = info.location_authorize;
toUpperCase@StringUtils (info.method) (http_data.method);
info.method = http_data.method;
......@@ -60,9 +61,9 @@ scope (error_handler) {
buildURI@HTTP(http_data) (answer)
}
};
}]{nullProcess}
Access (info) (newInfo) {
[Access (info) (newInfo) {
newInfo << info;
http_data.URI = info.location_access;
toUpperCase@StringUtils (info.method) (http_data.method);
......@@ -85,28 +86,30 @@ scope (error_handler) {
throw(OAuth2_error),
cannot_translate =>
println@Console("didn't receive proper json")();
throw (Oauth2_error)
throw (OAuth2_error)
);
DataAccess@OAuth2Data (info) (data_query);
http_data.content << data_query.pair;
http_data.content_format = "application/x-www-form-urlencoded";
sendRequest@HTTP(http_data) (answer);
if (is_defined(answer.error)) {
println@Console("error in answer:" + answer)();
throw (OAuth2_error)
};
jsonToValue@HTTP(answer)(answer_json);
search.json << answer_json;
search.search = "error";
searchJson@HTTP(search)(value);
if (value.found) {
println@Console("error in Accesing resource.")();
throw(OAuth2_error)
};
search.search = "access_token";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.access_token = value
} else {
//access_token is required by rfc
throw (oauth2_answer)
println@Console("Wrong answer from server")();
throw (OAuth2_answer)
};
search.search = "refresh_token";
searchJson@HTTP(search)(value);
......@@ -124,13 +127,14 @@ scope (error_handler) {
newInfo.token_type = value
} else {
//token_type is required by rfc
throw (oauth2_answer)
println@Console("Wrong answer from server")();
throw (OAuh2_error)
};
newInfo.json = answer_json
newInfo.json << answer_json
}
};
}]{nullProcess}
Refresh (info) (newInfo) {
[Refresh (info) (newInfo) {
newInfo << info;
http_data.URI = info.location_access;
toUpperCase@StringUtils (info.method) (http_data.method);
......@@ -156,39 +160,54 @@ scope (error_handler) {
throw (Oauth2_error)
);
DataRefresh@OAuth2Data (info) (data_query);
for (count=#newInfo.query, count < (#newInfo.query + #data_query.pair),
count++) {
http_data.query[count] << data_query.pair[count -
#newInfo.query]
};
http_data.query << newInfo.query;
http_data.content << data_query.pair;
http_data.content_format = "application/x-www-form-urlencoded";
sendRequest@HTTP(http_data) (answer);
jsonToValue@HTTP(answer)(answer_json);
if (is_defined(answer_json.access_token)) {
newInfo.access_token = answer_json.access_token;
search.json << answer_json;
search.search = "error";
searchJson@HTTP(search)(value);
if (value.found) {
println@Console("error in Refreshing token")();
throw(OAuth2_error)
};
search.search = "access_token";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.access_token = value;
undef(answer_json.access_token)
} else {
//access_token is required by rfc
throw (oauth2_answer)
println@Console("Wrong answer from server")();
throw (OAuth2_error)
};
if (is_defined(answer_json.refresh_token)) {
newInfo.refresh_token = answer_json.refresh_token;
search.search = "refresh_token";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.refresh_token = value;
undef(answer_json.refresh_token)
};
if (is_defined(answer_json.expires_in)) {
newInfo.expires_in = answer_json.expires_in;
search.search = "expires_in";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.expires_in = value;
undef(answer_json.expires_in)
};
if (is_defined(answer_json.token_type)) {
newInfo.token_type = answer_json.token_type;
search.search = "token_type";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.token_type = value;
undef(answer_json.token_type)
} else {
//token_type is required by rfc
throw (oauth2_answer)
println@Console("Wrong answer from server")();
throw (OAuth2_error)
};
newInfo.json = answer_json
}
newInfo.json << answer_json
}
}]{nullProcess}
}
......@@ -19,52 +19,7 @@
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
include "jolie/net/utils/HTTP.iol"
type OAuth2Pair:void {
.name :string
.value :string
}
type OAuth2Info:void {
.location_authorize :string
.location_access :string
.method :string
.client_id :string
.client_secret :string
.code? :string
.redirect_uri? :string
.scope? :string
.state? :string
.access_token? :string
.token_type? :string
.refresh_token? :string
.expires_in? :int
// HTTP additional user-specified parameters
.json? :HTTPJson
.headers? :OAuth2Pair
.query? :OAuth2Pair
}
type OAuth2_Search:void {
.search :string
.pair* :OAuth2Pair
}
type OAuth2_SearchRes:string {
.found :bool
}
type OAuth2_data:void {
.pair* :OAuth2Pair
}
interface OAuth2DataInterface {
RequestResponse:
DataAuth (OAuth2Info) (OAuth2_data),
DataAccess (OAuth2Info) (OAuth2_data),
DataRefresh (OAuth2Info) (OAuth2_data),
getPair (OAuth2_Search) (OAuth2_SearchRes)
}
include "OAuth2Types.iol"
outputPort OAuth2Data {
Interfaces: OAuth2DataInterface
......
......@@ -19,8 +19,8 @@
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
include "OAuth2Data.iol"
include "string_utils.iol"
include "OAuth2Types.iol"
inputPort OAuth2Data{
Location: "local"
......@@ -30,7 +30,7 @@ inputPort OAuth2Data{
execution {concurrent}
main {
DataAuth (info) (data) {
[DataAuth (info) (data) {
data.pair[0].name = "client_id";
data.pair[0].value = info.client_id;
data.pair[1].name = "response_type";
......@@ -51,9 +51,9 @@ DataAuth (info) (data) {
data.pair[count].value = info.state;
count++
}
};
}]{nullProcess}
DataAccess (info) (data) {
[DataAccess (info) (data) {
if (!is_defined(info.code) || !is_defined(info.client_secret)) {
throw (oauth2_missing_data)
};
......@@ -76,14 +76,14 @@ DataAccess (info) (data) {
data.pair[count].value = info.scope;
count++
}
};
}]{nullProcess}
DataRefresh (info) (data) {
[DataRefresh (info) (data) {
if (!is_defined(info.refresh_token) || !is_defined(info.client_secret)) {
throw (oauth2_missing_data)
};
data.pair[0].name = "client_id";
data.pair[0].value = info.client_id;
data.pair[0].value = info.client_id;
data.pair[1].name = "grant_type";
data.pair[1].value = "refresh_token";
data.pair[2].name = "refresh_token";
......@@ -94,9 +94,9 @@ DataRefresh (info) (data) {
data.pair[4].name = "scope";
data.pair[4].value = info.scope
}
};
}]{nullProcess}
getPair (pairSearch) (result) {
[getPair (pairSearch) (result) {
result="";
result.found = false;
length@StringUtils(pairSearch.search)(search_length);
......@@ -112,7 +112,7 @@ getPair (pairSearch) (result) {
result.found = true
}
}
}
}]{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 "../utils/HTTPTypes.iol"
type OAuth2Pair:void {
.name :string
.value :string
}
type OAuth2Info:void {
.location_authorize :string
.location_access :string
.method :string
.client_id :string
.client_secret :string
.code? :string
.redirect_uri? :string
.scope? :string
.state? :string
.access_token? :string
.token_type? :string
.refresh_token? :string
.expires_in? :int
// HTTP additional user-specified parameters
.json? :HTTPJson
.headers? :OAuth2Pair
.query? :OAuth2Pair
}
type OAuth2_Search:void {
.search :string
.pair* :OAuth2Pair
}
type OAuth2_SearchRes:string {
.found :bool
}
type OAuth2_data:void {
.pair* :OAuth2Pair
}
interface OAuth2DataInterface {
RequestResponse:
DataAuth (OAuth2Info) (OAuth2_data),
DataAccess (OAuth2Info) (OAuth2_data),
DataRefresh (OAuth2Info) (OAuth2_data),
getPair (OAuth2_Search) (OAuth2_SearchRes)
}
interface OAuth2Interface {
RequestResponse:
Auth (OAuth2Info) (string),
Access (OAuth2Info) (OAuth2Info),
Refresh (OAuth2Info) (OAuth2Info),
}
include "console.iol"
include "OAuth2.iol"
include "string_utils.iol"
include "OAuth2.iol"
type codeRequest:void {
......@@ -80,14 +80,17 @@ main
};
data.code = codeRequest.code;
data.method = "POST";
undef(data.redirect_uri);
// next step in Auth2 authentication.
Access@OAuth2 (data) (answer);
println@Console("token:" + answer.access_token)();
println@Console("expires_in:" + answer.expires_in)();
if (is_defined(answer.refresh_token)) {
println@Console("refresh_token:" + answer.refresh_token)()
}
};
println@Console("Now refreshing the token:")();
Refresh@OAuth2(answer)(refresh_answer)
}
}
......@@ -19,50 +19,7 @@
* For details about the authors of this software, see the AUTHORS file. *
***************************************************************************/
type HTTPpair: void {
.name :string
.value :string
}
type HTTPRequest:void {
.URI :string
.method :string
.content* :HTTPpair
.content_format?:string
.cookies? :string
.headers* :HTTPpair
.query* :HTTPpair
}
type HTTPParseResult:void {
.pair* :HTTPpair
}
type HTTPJson:string {
.value? :any
.child* :HTTPJson
}
type HTTPJsonSearch:void {
.json :HTTPJson
.search :string
}
type HTTPJsonAnswer:any {
.found :bool
}
type HTTPURLEncoding:string {
.encoding :string
}
interface HTTPInterface {
RequestResponse:
sendRequest (HTTPRequest) (string) throws send_error,
buildURI (HTTPRequest) (string) throws buildURI_fault,
URLencode (HTTPURLEncoding) (string) throws Encoding_fault,
parse_www_form (string) (HTTPParseResult) throws parse_error,
jsonToValue (string) (HTTPJson) throws cannot_translate,
searchJson (HTTPJsonSearch) (HTTPJsonAnswer)
}
include "HTTPTypes.iol"
outputPort HTTP {
Interfaces:
......
/***************************************************************************
* 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. *
***************************************************************************/
type HTTPpair: void {
.name :string
.value :string
}
type HTTPRequest:void {
.URI :string
.method :string
.content* :HTTPpair
.content_format?:string
.cookies? :string
.headers* :HTTPpair
.query* :HTTPpair
}
type HTTPParseResult:void {
.pair* :HTTPpair
}
type HTTPJson:string {
.value? :any
.child* :HTTPJson
}
type HTTPJsonSearch:void {
.json :HTTPJson
.search :string
}
type HTTPJsonAnswer:any {
.found :bool
}
type HTTPURLEncoding:string {
.encoding :string
}
interface HTTPInterface {
RequestResponse:
sendRequest (HTTPRequest) (string) throws send_error,
buildURI (HTTPRequest) (string) throws buildURI_fault,
URLencode (HTTPURLEncoding) (string) throws Encoding_fault,
parse_www_form (string) (HTTPParseResult) throws parse_error,
jsonToValue (string) (HTTPJson) throws cannot_translate,
searchJson (HTTPJsonSearch) (HTTPJsonAnswer)
}
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