-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from parallaxinc/oauth
Oauth
- Loading branch information
Showing
6 changed files
with
223 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/com/parallax/client/cloudsession/CloudSessionOAuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../java/com/parallax/client/cloudsession/exceptions/WrongAuthenticationSourceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters