Skip to content

Commit

Permalink
Merge pull request #22 from zfi/master
Browse files Browse the repository at this point in the history
Release v1.2.1
  • Loading branch information
zfi authored Sep 11, 2018
2 parents 41d9215 + c89a1e3 commit 80d2b72
Show file tree
Hide file tree
Showing 8 changed files with 765 additions and 597 deletions.
17 changes: 14 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,42 @@
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<!-- END Testing -->


<!-- Application logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j13</artifactId>
<version>1.0.1</version>
</dependency>


<!-- HTTP Request wrapper -->
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
<version>6.0</version>
</dependency>


<!-- Convert between Java Objects and JSON objects -->
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>

</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ public class CloudSessionAuthenticateService {
*/
private final Logger LOG = LoggerFactory.getLogger(CloudSessionAuthenticateService.class);


/**
* Base URL use to obtain authentication service.
*/
private final String BASE_URL;


/**
* Host name
*/
Expand Down Expand Up @@ -84,32 +86,29 @@ public User authenticateLocalUser(String login, String password)
WrongAuthenticationSourceException,
ServerException {

LOG.debug("Attempting to authenticate user: {}", login);
LOG.info("Contacting endpoint '/authenticate/local");

try {
Map<String, String> data = new HashMap<>();
data.put("email", login);
data.put("password", password);

// Issue POST request to attempt login
HttpRequest httpRequest = HttpRequest
HttpRequest request = HttpRequest
.post(getUrl("/authenticate/local"))
.header("server", SERVER)
.form(data);

// Convert response from login attempt
String response = httpRequest.body();

LOG.debug("Received a response: {}", response);

if (request.ok()) {
String response = request.body();
JsonElement jelement = new JsonParser().parse(response);
JsonObject responseObject = jelement.getAsJsonObject();

if (responseObject.get("success").getAsBoolean()) {
// Create and return a user object
JsonObject userJson = responseObject.get("user").getAsJsonObject();
User user = new User();

User user = new User();
user.setId(userJson.get("id").getAsLong());
user.setEmail(userJson.get("email").getAsString());
user.setLocale(userJson.get("locale").getAsString());
Expand Down Expand Up @@ -154,13 +153,16 @@ public User authenticateLocalUser(String login, String password)
LOG.warn("Unexpected error: {}", response);
return null;
}
}
} catch (HttpRequest.HttpRequestException hre) {
LOG.error("Inter service error", hre);
throw new ServerException(hre);
} catch (JsonSyntaxException jse) {
LOG.error("Json syntax error", jse);
throw new ServerException(jse);
}

return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@
package com.parallax.client.cloudsession;

import com.github.kevinsawicki.http.HttpRequest;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

import com.parallax.client.cloudsession.exceptions.EmailNotConfirmedException;
import com.parallax.client.cloudsession.exceptions.ServerException;
import com.parallax.client.cloudsession.exceptions.UnknownUserIdException;
import com.parallax.client.cloudsession.exceptions.UserBlockedException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,6 +33,7 @@
public class CloudSessionAuthenticationTokenService {

private final Logger LOG = LoggerFactory.getLogger(CloudSessionAuthenticationTokenService.class);

private final String BASE_URL;
private final String SERVER;

Expand Down Expand Up @@ -57,31 +62,44 @@ private String getUrl(String actionUrl) {
* @throws EmailNotConfirmedException
* @throws ServerException
*/
public String request(Long idUser, String browser, String ipAddress) throws UnknownUserIdException, UserBlockedException, EmailNotConfirmedException, ServerException {
public String request(Long idUser, String browser, String ipAddress) throws
UnknownUserIdException,
UserBlockedException,
EmailNotConfirmedException,
ServerException {

LOG.debug("Contacting endpoint '/authtoken/request");

try {
Map<String, String> data = new HashMap<>();
data.put("idUser", String.valueOf(idUser));
data.put("browser", browser);
data.put("ipAddress", ipAddress);
HttpRequest request = HttpRequest.post(getUrl("/authtoken/request")).header("server", SERVER).form(data);
// int responseCode = request.code();
// System.out.println("Response code: " + responseCode);
String response = request.body();
// System.out.println(response);
JsonElement jelement = new JsonParser().parse(response);
JsonObject responseObject = jelement.getAsJsonObject();
if (responseObject.get("success").getAsBoolean()) {
return responseObject.get("token").getAsString();
} else {
switch (responseObject.get("code").getAsInt()) {
case 400:
throw new UnknownUserIdException(responseObject.get("data").getAsString());
case 420:
throw new UserBlockedException();
case 430:
throw new EmailNotConfirmedException();
}

HttpRequest request = HttpRequest
.post(getUrl("/authtoken/request"))
.header("server", SERVER)
.form(data);

if (request.ok()) {
String response = request.body();
JsonElement jelement = new JsonParser().parse(response);
JsonObject responseObject = jelement.getAsJsonObject();

if (responseObject.get("success").getAsBoolean()) {
return responseObject.get("token").getAsString();
} else {
switch (responseObject.get("code").getAsInt()) {
case 400:
throw new UnknownUserIdException(responseObject.get("data").getAsString());
case 420:
throw new UserBlockedException();
case 430:
throw new EmailNotConfirmedException();
}

return null;
}
}
} catch (HttpRequest.HttpRequestException hre) {
LOG.error("Inter service error", hre);
Expand All @@ -90,7 +108,10 @@ public String request(Long idUser, String browser, String ipAddress) throws Unkn
LOG.error("Json syntax error: {}", jse.getMessage());
throw new ServerException(jse);
}

return null;
}


/**
*
Expand All @@ -102,23 +123,33 @@ public String request(Long idUser, String browser, String ipAddress) throws Unkn
* @throws ServerException
*/
public boolean doConfirm(String token, Long idUser, String browser, String ipAddress) throws ServerException {
String response = "";

LOG.debug("Contacting endpoint '/authtoken/confirm");

String response = null;

try {
Map<String, String> data = new HashMap<>();
data.put("token", token);
data.put("idUser", String.valueOf(idUser));
data.put("browser", browser);
data.put("ipAddress", ipAddress);
HttpRequest request = HttpRequest.post(getUrl("/authtoken/confirm")).header("server", SERVER).form(data);

response = request.body();
JsonElement jelement = new JsonParser().parse(response);
JsonObject responseObject = jelement.getAsJsonObject();
if (responseObject.get("success").getAsBoolean()) {
return true;
} else {
return false;

HttpRequest request = HttpRequest
.post(getUrl("/authtoken/confirm"))
.header("server", SERVER)
.form(data);

if (request.ok()) {
response = request.body();
JsonElement jelement = new JsonParser().parse(response);
JsonObject responseObject = jelement.getAsJsonObject();

if (responseObject.get("success").getAsBoolean()) {
return true;
} else {
return false;
}
}
} catch (HttpRequest.HttpRequestException hre) {
LOG.error("Inter service error", hre);
Expand All @@ -128,8 +159,11 @@ public boolean doConfirm(String token, Long idUser, String browser, String ipAdd
"Json syntax error: {} - {}",response, jse.getMessage());
throw new ServerException(jse);
}

return false;
}


/**
*
* @param idUser
Expand All @@ -139,28 +173,40 @@ public boolean doConfirm(String token, Long idUser, String browser, String ipAdd
* @throws ServerException
*/
public List<String> getTokens(Long idUser, String browser, String ipAddress) throws ServerException {

LOG.debug("Contacting endpoint '/authtoken/tokens");

try {
Map<String, String> data = new HashMap<>();
data.put("browser", browser);
data.put("ipAddress", ipAddress);
HttpRequest request = HttpRequest.post(getUrl("/authtoken/tokens/" + idUser)).header("server", SERVER).form(data);

String response = request.body();
JsonElement jelement = new JsonParser().parse(response);
JsonArray jsonTokens = jelement.getAsJsonArray();
List<String> tokens = new ArrayList<>();

for (JsonElement token : jsonTokens) {
tokens.add(token.getAsString());

HttpRequest request = HttpRequest
.post(getUrl("/authtoken/tokens/" + idUser))
.header("server", SERVER)
.form(data);

if (request.ok()) {
String response = request.body();
JsonElement jelement = new JsonParser().parse(response);
JsonArray jsonTokens = jelement.getAsJsonArray();
List<String> tokens = new ArrayList<>();

for (JsonElement token : jsonTokens) {
tokens.add(token.getAsString());
}

return tokens;
}
return tokens;
} catch (HttpRequest.HttpRequestException hre) {
LOG.error("Inter service error", hre);
throw new ServerException(hre);
} catch (JsonSyntaxException jse) {
LOG.error("Json syntax error: {}", jse.getMessage());
throw new ServerException(jse);
}

return null;
}

}
Loading

0 comments on commit 80d2b72

Please sign in to comment.