Skip to content

Commit

Permalink
Merge pull request #5 from parallaxinc/oauth
Browse files Browse the repository at this point in the history
Oauth
  • Loading branch information
michel-cf authored Jul 13, 2016
2 parents 05a4995 + 7f3740d commit 1845da9
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.parallax.client.cloudsession.exceptions.ServerException;
import com.parallax.client.cloudsession.exceptions.UnknownUserException;
import com.parallax.client.cloudsession.exceptions.UserBlockedException;
import com.parallax.client.cloudsession.exceptions.WrongAuthenticationSourceException;
import com.parallax.client.cloudsession.objects.User;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -37,7 +38,7 @@ public CloudSessionAuthenticateService(String server, String baseUrl) {

}

public User authenticateLocalUser(String login, String password) throws UnknownUserException, UserBlockedException, EmailNotConfirmedException, InsufficientBucketTokensException, ServerException {
public User authenticateLocalUser(String login, String password) throws UnknownUserException, UserBlockedException, EmailNotConfirmedException, InsufficientBucketTokensException, WrongAuthenticationSourceException, ServerException {
try {
Map<String, String> data = new HashMap<>();
data.put("email", login);
Expand Down Expand Up @@ -70,6 +71,9 @@ public User authenticateLocalUser(String login, String password) throws UnknownU
throw new EmailNotConfirmedException(message);
case 470:
throw new InsufficientBucketTokensException();
case 480:
String authenticationSource = responseObject.get("data").getAsString();
throw new WrongAuthenticationSourceException(authenticationSource);
}
LOG.warn("Unexpected error: {}", response);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.parallax.client.cloudsession.exceptions.ServerException;
import com.parallax.client.cloudsession.exceptions.UnknownUserException;
import com.parallax.client.cloudsession.exceptions.UnknownUserIdException;
import com.parallax.client.cloudsession.exceptions.WrongAuthenticationSourceException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
Expand All @@ -37,7 +38,7 @@ public CloudSessionLocalUserService(String server, String baseUrl) {
this.BASE_URL = baseUrl;
}

public boolean doPasswordReset(String token, String email, String password, String passwordConfirm) throws UnknownUserException, PasswordVerifyException, PasswordComplexityException, ServerException {
public boolean doPasswordReset(String token, String email, String password, String passwordConfirm) throws UnknownUserException, PasswordVerifyException, PasswordComplexityException, WrongAuthenticationSourceException, ServerException {
try {
Map<String, String> data = new HashMap<>();
data.put("token", token);
Expand All @@ -60,6 +61,8 @@ public boolean doPasswordReset(String token, String email, String password, Stri
throw new PasswordVerifyException();
case 490:
throw new PasswordComplexityException();
case 480:
throw new WrongAuthenticationSourceException(responseObject.get("data").getAsString());
}
return false;
}
Expand All @@ -72,7 +75,7 @@ public boolean doPasswordReset(String token, String email, String password, Stri
}
}

public boolean requestPasswordReset(String email) throws UnknownUserException, InsufficientBucketTokensException, ServerException {
public boolean requestPasswordReset(String email) throws UnknownUserException, InsufficientBucketTokensException, WrongAuthenticationSourceException, ServerException {
try {
HttpRequest request = HttpRequest.get(getUrl("/local/reset/" + email)).header("server", SERVER);
// int responseCode = request.code();
Expand All @@ -89,6 +92,8 @@ public boolean requestPasswordReset(String email) throws UnknownUserException, I
throw new UnknownUserException(responseObject.get("data").getAsString());
case 470:
throw new InsufficientBucketTokensException(responseObject.get("message").getAsString(), responseObject.get("data").getAsString());
case 480:
throw new WrongAuthenticationSourceException(responseObject.get("data").getAsString());
}
return false;
}
Expand All @@ -101,7 +106,7 @@ public boolean requestPasswordReset(String email) throws UnknownUserException, I
}
}

public boolean doConfirm(String email, String token) throws UnknownUserException, ServerException {
public boolean doConfirm(String email, String token) throws UnknownUserException, WrongAuthenticationSourceException, ServerException {
try {
Map<String, String> data = new HashMap<>();
data.put("email", email);
Expand All @@ -119,6 +124,8 @@ public boolean doConfirm(String email, String token) throws UnknownUserException
switch (responseObject.get("code").getAsInt()) {
case 400:
throw new UnknownUserException(responseObject.get("data").getAsString());
case 480:
throw new WrongAuthenticationSourceException(responseObject.get("data").getAsString());
case 510:
return false;
}
Expand All @@ -133,7 +140,7 @@ public boolean doConfirm(String email, String token) throws UnknownUserException
}
}

public boolean requestNewConfirmEmail(String email) throws UnknownUserException, InsufficientBucketTokensException, EmailAlreadyConfirmedException, ServerException {
public boolean requestNewConfirmEmail(String email) throws UnknownUserException, InsufficientBucketTokensException, EmailAlreadyConfirmedException, WrongAuthenticationSourceException, ServerException {
try {
HttpRequest request = HttpRequest.get(getUrl("/local/confirm/" + email)).header("server", SERVER);
// int responseCode = request.code();
Expand All @@ -150,6 +157,8 @@ public boolean requestNewConfirmEmail(String email) throws UnknownUserException,
throw new UnknownUserException(responseObject.get("data").getAsString());
case 470:
throw new InsufficientBucketTokensException(responseObject.get("message").getAsString(), responseObject.get("data").getAsString());
case 480:
throw new WrongAuthenticationSourceException(responseObject.get("data").getAsString());
case 520:
throw new EmailAlreadyConfirmedException(responseObject.get("message").getAsString());
}
Expand All @@ -165,7 +174,7 @@ private String getUrl(String actionUrl) {
return BASE_URL + actionUrl;
}

public boolean changePassword(Long idUser, String oldPassword, String password, String confirmPassword) throws UnknownUserIdException, PasswordVerifyException, PasswordComplexityException, ServerException {
public boolean changePassword(Long idUser, String oldPassword, String password, String confirmPassword) throws UnknownUserIdException, PasswordVerifyException, PasswordComplexityException, WrongAuthenticationSourceException, ServerException {
try {
Map<String, String> data = new HashMap<>();
data.put("old-password", oldPassword);
Expand All @@ -186,6 +195,8 @@ public boolean changePassword(Long idUser, String oldPassword, String password,
throw new UnknownUserIdException(responseObject.get("data").getAsString());
case 460:
throw new PasswordVerifyException();
case 480:
throw new WrongAuthenticationSourceException(responseObject.get("data").getAsString());
case 490:
throw new PasswordComplexityException();
case 510:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.parallax.client.cloudsession;

import com.github.kevinsawicki.http.HttpRequest;
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.NonUniqueEmailException;
import com.parallax.client.cloudsession.exceptions.ScreennameUsedException;
import com.parallax.client.cloudsession.exceptions.ServerException;
import com.parallax.client.cloudsession.exceptions.UnknownUserException;
import com.parallax.client.cloudsession.exceptions.WrongAuthenticationSourceException;
import com.parallax.client.cloudsession.objects.User;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author Michel
*/
public class CloudSessionOAuthService {

private final Logger LOG = LoggerFactory.getLogger(CloudSessionOAuthService.class);
private final String BASE_URL;
private final String SERVER;

public CloudSessionOAuthService(String server, String baseUrl) {
this.SERVER = server;
this.BASE_URL = baseUrl;

}

public User validateUser(String login, String authenticationSource) throws UnknownUserException, WrongAuthenticationSourceException, ServerException {
try {
Map<String, String> data = new HashMap<>();
data.put("email", login);
data.put("source", authenticationSource);
HttpRequest httpRequest = HttpRequest.post(getUrl("/oauth/validate")).header("server", SERVER).form(data);
String response = httpRequest.body();

JsonElement jelement = new JsonParser().parse(response);
JsonObject responseObject = jelement.getAsJsonObject();
if (responseObject.get("success").getAsBoolean()) {
JsonObject userJson = responseObject.get("user").getAsJsonObject();
User user = new User();
user.setId(userJson.get("id").getAsLong());
user.setEmail(userJson.get("email").getAsString());
user.setLocale(userJson.get("locale").getAsString());
user.setScreenname(userJson.get("screenname").getAsString());
return user;
} else {
String message = responseObject.get("message").getAsString();
switch (responseObject.get("code").getAsInt()) {
case 400:
throw new UnknownUserException(login, message);
case 480:
String userAuthenticationSource = responseObject.get("data").getAsString();
throw new WrongAuthenticationSourceException(userAuthenticationSource);
}
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 syntace service error", jse);
throw new ServerException(jse);
}
}

public Long registerUser(String email, String authenticationSource, String locale, String screenname) throws NonUniqueEmailException, ScreennameUsedException, ServerException {
try {
Map<String, String> data = new HashMap<>();
data.put("email", email);
data.put("source", authenticationSource);
data.put("locale", locale);
data.put("screenname", screenname);
HttpRequest request = HttpRequest.post(getUrl("/oauth/create")).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("user").getAsLong();
} else {
switch (responseObject.get("code").getAsInt()) {
case 450:
throw new NonUniqueEmailException(responseObject.get("data").getAsString());
case 500:
throw new ScreennameUsedException(responseObject.get("data").getAsString());
}
return null;
}
} catch (HttpRequest.HttpRequestException hre) {
LOG.error("Inter service error", hre);
throw new ServerException(hre);
} catch (JsonSyntaxException jse) {
LOG.error("Json syntace service error", jse);
throw new ServerException(jse);
}
}

private String getUrl(String actionUrl) {
return BASE_URL + actionUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public User getUser(String email) throws UnknownUserException, ServerException {
user.setEmail(userJson.get("email").getAsString());
user.setLocale(userJson.get("locale").getAsString());
user.setScreenname(userJson.get("screenname").getAsString());
user.setAuthenticationSource(userJson.get("authentication-source").getAsString());
return user;
} else {
String message = responseObject.get("message").getAsString();
Expand All @@ -67,6 +68,41 @@ public User getUser(String email) throws UnknownUserException, ServerException {
}
}

public User getUserByScreenname(String screenname) throws UnknownUserException, ServerException {
try {
HttpRequest request = HttpRequest.get(getUrl("/user/screenname/" + screenname));
// 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()) {
JsonObject userJson = responseObject.get("user").getAsJsonObject();
User user = new User();
user.setId(userJson.get("id").getAsLong());
user.setEmail(userJson.get("email").getAsString());
user.setLocale(userJson.get("locale").getAsString());
user.setScreenname(userJson.get("screenname").getAsString());
user.setAuthenticationSource(userJson.get("authentication-source").getAsString());
return user;
} else {
String message = responseObject.get("message").getAsString();
switch (responseObject.get("code").getAsInt()) {
case 400:
throw new UnknownUserException(screenname, message);
}
return null;
}
} catch (HttpRequest.HttpRequestException hre) {
LOG.error("Inter service error", hre);
throw new ServerException(hre);
} catch (JsonSyntaxException jse) {
LOG.error("Json syntace service error", jse);
throw new ServerException(jse);
}
}

public User getUser(Long idUser) throws UnknownUserIdException, ServerException {
try {
HttpRequest request = HttpRequest.get(getUrl("/user/id/" + idUser));
Expand All @@ -83,6 +119,7 @@ public User getUser(Long idUser) throws UnknownUserIdException, ServerException
user.setEmail(userJson.get("email").getAsString());
user.setLocale(userJson.get("locale").getAsString());
user.setScreenname(userJson.get("screenname").getAsString());
user.setAuthenticationSource(userJson.get("authentication-source").getAsString());
return user;
} else {
switch (responseObject.get("code").getAsInt()) {
Expand Down Expand Up @@ -118,6 +155,7 @@ public User changeUserInfo(Long idUser, String screenname) throws UnknownUserIdE
user.setEmail(userJson.get("email").getAsString());
user.setLocale(userJson.get("locale").getAsString());
user.setScreenname(userJson.get("screenname").getAsString());
user.setAuthenticationSource(userJson.get("authentication-source").getAsString());
return user;
} else {
String message = responseObject.get("message").getAsString();
Expand Down Expand Up @@ -156,6 +194,7 @@ public User changeUserLocale(Long idUser, String locale) throws UnknownUserIdExc
user.setEmail(userJson.get("email").getAsString());
user.setLocale(userJson.get("locale").getAsString());
user.setScreenname(userJson.get("screenname").getAsString());
user.setAuthenticationSource(userJson.get("authentication-source").getAsString());
return user;
} else {
String message = responseObject.get("message").getAsString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.parallax.client.cloudsession.exceptions;

/**
*
* @author Michel
*/
public class WrongAuthenticationSourceException extends Exception {

private static final String DEFAULT_MESSAGE = "Wrong authentication srouce";
private String authenticationSource;

public WrongAuthenticationSourceException() {
super(DEFAULT_MESSAGE);
}

public WrongAuthenticationSourceException(String authenticationSource) {
super(DEFAULT_MESSAGE);
this.authenticationSource = authenticationSource;
}

public WrongAuthenticationSourceException(String authenticationSource, String message) {
super(message);
this.authenticationSource = authenticationSource;
}

public String getAuthenticationSource() {
return authenticationSource;
}

}
12 changes: 11 additions & 1 deletion src/main/java/com/parallax/client/cloudsession/objects/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ public class User implements Serializable {
private String email;
private String locale;
private String screenname;
private String authenticationSource;

public User() {
}

public User(Long id, String email, String locale) {
public User(Long id, String email, String locale, String authenticationSource) {
this.id = id;
this.email = email;
this.locale = locale;
this.authenticationSource = authenticationSource;
}

public Long getId() {
Expand Down Expand Up @@ -59,4 +61,12 @@ public void setScreenname(String screenname) {
this.screenname = screenname;
}

public String getAuthenticationSource() {
return authenticationSource;
}

public void setAuthenticationSource(String authenticationSource) {
this.authenticationSource = authenticationSource;
}

}

0 comments on commit 1845da9

Please sign in to comment.