FINALLY WORKING

Signed-off-by: Luca Fulchir <luca@fulchir.it>
parent 1228d883
......@@ -10,7 +10,7 @@ java:
- mkdir -p ${BUILDDIR} 2>/dev/null
${JAVAC} -cp ${JOLIEDIR}/jolie.jar -d ${BUILDDIR} jolie/net/utils/URLEncoder.java
${JAVAC} -cp ${JOLIEDIR}/jolie.jar:${JARDIR}/commons-codec-1.6.jar -d ${BUILDDIR} jolie/net/utils/Hmac.java
${JAVAC} -cp ${JOLIEDIR}/jolie.jar:${JARDIR}/httpcore-4.2.2.jar:${JARDIR}/httpclient-4.2.3.jar:${JARDIR}/commons-io-2.4.jar:${JARDIR}/commons-codec-1.6.jar:${JOLIEDIR}/extensions/http.jar -d ${BUILDDIR} jolie/net/utils/HTTP.java
${JAVAC} -cp ${JOLIEDIR}/jolie.jar:${JARDIR}/httpcore-4.2.2.jar:${JARDIR}/httpclient-4.2.3.jar:${JARDIR}/commons-io-2.4.jar:${JARDIR}/commons-codec-1.6.jar:${JOLIEDIR}/extensions/http.jar:${JOLIEDIR}/lib/json_simple.jar -d ${BUILDDIR} jolie/net/utils/HTTP.java
cd ${BUILDDIR} && jar cvf ${PACKAGE_NAME} ./jolie/net/utils/*class && cd ${CWD}
......
OAUTH2:
AuthRequest:
send:
-response_type (required = code)
-client_id (required)
-redirect_uri (optional)
-scope (optional)
-state (optional)
google:
-approval_prompt (force) maybe optional
-access_type (offline) ?
auth: https://accounts.google.com/o/oauth2/auth
get:
-code (required)
-scope (optional if)
-state (required if)
error:
-error
-blablabla
AccessRequest:
send:
-grant_type (required)
-code (required)
-redirect_uri (required)
-client_id (required)
google:
token: https://accounts.google.com/o/oauth2/token
get:
-access_token (required)
-token_type (required)
-expires_in (recommended)
-refresh_token (optional)
-scope (optional if)
error:
-error
-bla-bla
-------------------------------
?
-------------------------------
AuthRequest:
-response_type (required = token)
-client_it (required)
-redirect_uri (optional)
-scope (optional)
-state (recommended)
(redirect)
AccessResponse:
-access_token (required)
-token_type (required)
-expires_in (recommended)
-scope (optional if)
-state (required if)
......@@ -22,7 +22,7 @@
include "OAuth2.iol"
include "string_utils.iol"
include "console.iol"
include "jolie/net/utils/HTTP.iol"
//include "jolie/net/utils/HTTP.iol"
inputPort OAuth2 {
Location: "local"
......@@ -70,7 +70,7 @@ scope (error_handler) {
info.method = http_data.method;
newInfo.method = info.method;
http_data.headers[0].name = "User-Agent";
http_data.headers[0].value = "Jolie-OAuth2";
http_data.headers[0].value = "Jlie-Oauth2";
scope (error_handler) {
install(not_found =>
println@Console("error on request") ();
......@@ -87,31 +87,36 @@ scope (error_handler) {
http_data.content_format = "application/x-www-form-urlencoded";
sendRequest@HTTP(http_data) (answer);
println@Console("answer:::"+answer)();
if (is_defined(answer.error)) {
println@Console("error in answer:" + answer)();
throw (OAuth2_error)
};
JsonToValue@HTTP(answer)(answer_json);
if (is_defined(answer_json.access_token)) {
newInfo.access_token = answer_json.access_token;
undef(answer_json.access_token)
jsonToValue@HTTP(answer)(answer_json);
search.json << answer_json;
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)
};
if (is_defined(answer_json.refresh_token)) {
newInfo.refresh_token = answer_json.refresh_token;
undef(answer_json.refresh_token)
search.search = "refresh_token";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.refresh_token = value
};
if (is_defined(answer_json.expires_in)) {
newInfo.expires_in = answer_json.expires_in;
undef(answer_json.expires_in)
search.search = "expires_in";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.expires_in = value
};
if (is_defined(answer_json.token_type)) {
newInfo.token_type = answer_json.token_type;
undef(answer_json.token_type)
search.search = "token_type";
searchJson@HTTP(search)(value);
if (value.found) {
newInfo.token_type = value
} else {
//token_type is required by rfc
throw (oauth2_answer)
......@@ -151,7 +156,7 @@ scope (error_handler) {
};
sendRequest@HTTP(http_data) (answer);
JsonToValue@HTTP(answer)(answer_json);
jsonToValue@HTTP(answer)(answer_json);
if (is_defined(answer_json.access_token)) {
newInfo.access_token = answer_json.access_token;
undef(answer_json.access_token)
......
......@@ -19,6 +19,8 @@
* 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
......@@ -36,9 +38,9 @@ type OAuth2Info:void {
.access_token? :string
.token_type? :string
.refresh_token? :string
.expires? :int
.expires_in? :int
// HTTP additional user-specified parameters
.json? :any
.json? :HTTPJson
.headers? :OAuth2Pair
.query? :OAuth2Pair
}
......@@ -48,10 +50,13 @@ type OAuth2_pair:void {
.value :string
}
type OAuth2_pairSearch:void {
type OAuth2_Search:void {
.search :string
.pair* :OAuth2_pair
}
type OAuth2_SearchRes:string {
.found :bool
}
type OAuth2_data:void {
.pair* :OAuth2Pair
......@@ -59,11 +64,12 @@ type OAuth2_data:void {
interface OAuth2DataInterface {
RequestResponse:
DataAuth (OAuth2Info) (OAuth2_data),
DataAccess (OAuth2Info) (OAuth2_data),
DataRefresh (OAuth2Info) (OAuth2_data),
getPair (OAuth2_pairSearch) (string) throws not_found
DataAuth (OAuth2Info) (OAuth2_data),
DataAccess (OAuth2Info) (OAuth2_data),
DataRefresh (OAuth2Info) (OAuth2_data),
getPair (OAuth2_Search) (OAuth2_SearchRes)
}
outputPort OAuth2Data {
Interfaces: OAuth2DataInterface
}
......
......@@ -100,5 +100,24 @@ main {
}
}]{nullProcess}
[getPair (pairSearch) (result) {
result="";
result.found = false;
length@StringUtils(pairSearch.search)(search_length);
for (i = 0, i < #pairSearch.pair, i++) {
// uhm... confronto tra due stringhe? '==' non è corretto...
length@StringUtils(pairSearch.pair[i].name)(length2);
pairSearch.pair[i].name.prefix=pairSearch.search;
startsWith@StringUtils(pairSearch.pair[i].name)(prefix);
undef(pairSearch.pair[i].name.prefix);
if (prefix && (search_length == length2)) {
result = pairSearch.pair[i].value;
i = #pairSearch.pair;
result.found = true
}
}
}]{nullProcess}
}
......@@ -28,7 +28,6 @@ main
// data needed for the connection: where to connect, how and who we are
data.location_authorize = "https://accounts.google.com/o/oauth2/auth";
data.location_access = "https://accounts.google.com/o/oauth2/token";
data.location_access = "http://codepedia.eu/token";
data.method = "GET";
data.client_id = "393076792151.apps.googleusercontent.com";
data.client_secret = "26H4P4_MJ8lBqXxNb-4dAqTL";
......@@ -86,7 +85,12 @@ main
data.code = codeRequest.code;
data.method = "POST";
// next step in Auth2 authentication.
Access@OAuth2 (data) (answer)
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)()
}
}
}
......
......@@ -22,7 +22,6 @@
type HTTPpair: void {
.name :string
.value :string
.format? :string
}
type HTTPRequest:void {
......@@ -35,22 +34,34 @@ type HTTPRequest:void {
.query* :HTTPpair
}
type HTTPParseResult: void {
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,
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) (any) throws cannot_translate
jsonToValue (string) (HTTPJson) throws cannot_translate,
searchJson (HTTPJsonSearch) (HTTPJsonAnswer)
}
outputPort HTTP {
......
......@@ -33,10 +33,15 @@ import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.commons.io.IOUtils;
import org.apache.commons.codec.net.URLCodec;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import jolie.net.http.json.JsonUtils;
import java.util.Map;
import java.util.Map.Entry;
import jolie.runtime.JavaService;
import jolie.runtime.Value;
import jolie.runtime.ValueVector;
......@@ -44,9 +49,10 @@ import jolie.runtime.embedding.RequestResponse;
import jolie.runtime.FaultException;
import java.nio.charset.Charset;
public class HTTP extends JavaService
{
@RequestResponse
public static String URLencode (Value req) throws FaultException
{
......@@ -103,17 +109,118 @@ public static String buildURI (Value req) throws FaultException
}
@RequestResponse
public static Value JsonToValue (String json) throws FaultException
public static Value jsonToValue (String json) throws FaultException
{
try {
Value ret = Value.create();
JsonUtils.parseJsonIntoValue(new StringReader(json), ret);
return ret;
Value value = objectToValue(JSONValue.parseWithException(
new StringReader(json)), "");
return value;
} catch (FaultException e) {
throw e;
} catch (Exception e) {
throw new FaultException("cannot_translate", e);
}
}
private static Value jsonObjectToValue(JSONArray array, String key)
throws FaultException
{
Value val = Value.create();
val.setValue("key");
for (Object elem : array) {
Value tmp = objectToValue(elem, "");
val.getNewChild("child").deepCopy(tmp);
}
return val;
}
private static Value jsonObjectToValue(JSONObject obj, String key)
throws FaultException
{
Value value = Value.create();
value.setValue(key);
Map<String, Object> map = (Map<String, Object>)obj;
for(Entry< String, Object> entry : map.entrySet()) {
Value val = Value.create();
if (entry.getValue() instanceof JSONArray) {
val = jsonObjectToValue((JSONArray)entry.getValue(),
entry.getKey());
} else if (entry.getValue() instanceof JSONObject) {
val = Value.create();
Value child = jsonObjectToValue(
(JSONObject)entry.getValue(),
entry.getKey());
val.getNewChild("child").deepCopy(child);
} else {
val = objectToValue(entry.getValue(), entry.getKey());
}
value.getNewChild("child").deepCopy(val);
}
return value;
}
private static Value objectToValue(Object obj, String key) throws FaultException
{
Value value = Value.create();
value.setValue(key);
if (obj instanceof Integer) {
value.getFirstChild("value").setValue((Integer)obj);
} else if (obj instanceof Long) {
value.getFirstChild("value").setValue(((Long)obj).intValue());
} else if (obj instanceof Double) {
value.getFirstChild("value").setValue((Double)obj);
} else if (obj instanceof String) {
value.getFirstChild("value").setValue((String)obj);
} else if (obj instanceof Boolean) {
Boolean b = (Boolean)obj;
if (b) {
value.getFirstChild("value").setValue(true);
} else {
value.getFirstChild("value").setValue(false);
}
} else if (obj instanceof JSONObject) {
value = jsonObjectToValue((JSONObject)obj, key);
} else if (obj instanceof JSONArray) {
value = jsonObjectToValue((JSONArray)obj, key);
} else {
value.getFirstChild("value").setValue(obj.toString());
}
return value;
}
@RequestResponse
public static Value searchJson (Value search) throws FaultException
{
Value json = search.getFirstChild("json");
String toFind = search.getFirstChild("search").strValue();
Value ret = real_searchJson(toFind, json);
if (ret == null) {
ret = Value.create();
ret.getNewChild("found").setValue(false);
} else {
ret.getNewChild("found").setValue(true);
}
return ret;
}
private static Value real_searchJson(String toFind, Value json) {
if (json.strValue().equals(toFind)) {
if (json.hasChildren("value")) {
return json.getFirstChild("value");
} else {
return null;
}
} else {
// search children. NOT recursive.
for (Value child : json.getChildren("child")) {
if (child.strValue().equals(toFind)) {
if (child.hasChildren("value"))
return child.getFirstChild("value");
}
}
}
return null;
}
@RequestResponse
public static Value parse_www_form (String data) throws FaultException
{
......@@ -190,7 +297,6 @@ private static String sendPost (HttpClient httpclient, Value req)
}
UrlEncodedFormEntity entity =
new UrlEncodedFormEntity(postParams);
entity.setContentEncoding("UTF_8");
if (req.hasChildren("content_format")) {
entity.setContentType(req.getFirstChild(
"content_format").strValue());
......
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