-
Notifications
You must be signed in to change notification settings - Fork 4
Merge fix-issue33 with master to fix issue #33. #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6597429
Added OpenID login solution for #33.
Unihedro 85d8245
grabbing latest changes from master and merging them into issue-branch
Vogel612 c8980ad
Fixed overlooked merge conflict in javadocs
Vogel612 b044de4
Added clean logging to Stackexchange Login
Vogel612 bc61062
fixed double replies when calling eval-command
Vogel612 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -9,14 +9,25 @@ | |
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.w3c.dom.Document; | ||
import org.xml.sax.SAXException; | ||
|
||
import com.gargoylesoftware.htmlunit.*; | ||
import com.gargoylesoftware.htmlunit.html.HtmlForm; | ||
import com.gargoylesoftware.htmlunit.html.HtmlPage; | ||
import com.gargoylesoftware.htmlunit.util.NameValuePair; | ||
import com.gargoylesoftware.htmlunit.util.UrlUtils; | ||
import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper; | ||
import com.gmail.inverseconduit.SESite; | ||
import com.gmail.inverseconduit.datatype.*; | ||
import com.gmail.inverseconduit.utils.PrintUtils; | ||
import com.google.common.base.Strings; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.gson.Gson; | ||
|
||
public class StackExchangeChat implements ChatInterface { | ||
|
@@ -59,19 +70,75 @@ public StackExchangeChat() { | |
*/ | ||
@Override | ||
public boolean login(ProviderDescriptor descriptor, CredentialsProvider credentials) { | ||
HtmlPage loginPage = getLoginPage(descriptor); | ||
if (null == loginPage) { return false; } | ||
if (descriptor.getDescription().toString().matches("(?!.*?(?:stackoverflow|meta)).+")) | ||
loggedIn = openIdLogin(credentials.getIdentificator(), credentials.getAuthenticator()); | ||
else { | ||
HtmlPage loginPage = getLoginPage(descriptor); | ||
if (null == loginPage) { return false; } | ||
|
||
HtmlForm loginForm = extractLoginForm(credentials.getIdentificator(), credentials.getAuthenticator(), loginPage); | ||
HtmlForm loginForm = extractLoginForm(credentials.getIdentificator(), credentials.getAuthenticator(), loginPage); | ||
|
||
WebResponse response = submitLoginForm(loginForm); | ||
if (null == response) { return false; } | ||
WebResponse response = submitLoginForm(loginForm); | ||
if (null == response) { return false; } | ||
|
||
loggedIn = (response.getStatusCode() == 200); | ||
logLoginMessage(descriptor.getDescription().toString(), credentials.getIdentificator(), response); | ||
loggedIn = (response.getStatusCode() == 200); | ||
logLoginMessage(descriptor.getDescription().toString(), credentials.getIdentificator(), response); | ||
} | ||
return loggedIn; | ||
} | ||
|
||
public boolean openIdLogin(String email, String password) { | ||
try { | ||
final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); | ||
{ // Log into Stack Exchange. | ||
WebResponse getRes = webClient.loadWebResponse(new WebRequest(UrlUtils.toUrlUnsafe("https://openid.stackexchange.com/account/login"), HttpMethod.GET)); | ||
if (getRes == null) { | ||
LOGGER.log(Level.SEVERE, "Could not get OpenID fkey."); | ||
return false; | ||
} | ||
|
||
String response = getRes.getContentAsString(); | ||
LOGGER.info("Got response from fetching fkey of OpenID login:" + response); | ||
|
||
WebRequest post = new WebRequest(UrlUtils.toUrlUnsafe("https://openid.stackexchange.com/account/login/submit"), HttpMethod.POST); | ||
try { | ||
post.setRequestParameters(ImmutableList.of(new NameValuePair("email", email), new NameValuePair("password", password), | ||
new NameValuePair("fkey", builder.parse(getRes.getContentAsStream()).getElementById("fkey").getAttribute("value")))); | ||
} catch(SAXException die) { | ||
LOGGER.log(Level.SEVERE, "Parsing response text as DOM has caused a parse exception.", die); | ||
return false; | ||
} | ||
WebResponse postRes = webClient.loadWebResponse(post); | ||
if (null == postRes || !Strings.isNullOrEmpty(postRes.getResponseHeaderValue("p3p"))) { | ||
LOGGER.log(Level.SEVERE, "Could not post to submit of OpenID."); | ||
return false; | ||
} | ||
} | ||
|
||
{ // Log into Stack Exchange Chat. | ||
WebResponse getRes = webClient.loadWebResponse(new WebRequest(UrlUtils.toUrlUnsafe("http://stackexchange.com/users/chat-login"), HttpMethod.GET)); | ||
WebRequest post = new WebRequest(UrlUtils.toUrlUnsafe("http://chat.stackexchange.com/users/login/global"), HttpMethod.POST); | ||
try { | ||
Document dom = builder.parse(getRes.getContentAsStream()); | ||
post.setRequestParameters(ImmutableList.of(new NameValuePair("authToken", dom.getElementById("authToken").getAttribute("value")), new NameValuePair("nonce", | ||
dom.getElementById("nonce").getAttribute("value")))); | ||
} catch(SAXException die) { | ||
LOGGER.log(Level.SEVERE, "Parsing response text as DOM has caused a parse exception.", die); | ||
return false; | ||
} | ||
post.setAdditionalHeaders(ImmutableMap.of("Origin", "http://chat.stackexchange.com", "Referer", "http://chat.stackexchange.com")); | ||
WebResponse postRes = webClient.loadWebResponse(post); | ||
String response = postRes.getContentAsString(); | ||
return response.contains("Welcome"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems extremely brittle to me. Probably a failed login gets us a http status code 403 instead. That would probably be better |
||
} | ||
} catch(ParserConfigurationException e) { | ||
// Ignore - virtually impossible. | ||
} catch(IOException e) { | ||
LOGGER.log(Level.SEVERE, "Encountered IO-Exception when trying to login: ", e); | ||
} | ||
return false; | ||
} | ||
|
||
private WebResponse submitLoginForm(HtmlForm loginForm) { | ||
WebResponse response; | ||
try { | ||
|
@@ -187,7 +254,7 @@ private String handleMessageOversize(final SeChatDescriptor descriptor, String m | |
StringBuilder messageBuilder = new StringBuilder(500); | ||
for (String token : messageTokens) { | ||
if (messageBuilder.length() + token.length() < 495) { | ||
messageBuilder.append(" " + token); | ||
messageBuilder.append(' ').append(token); | ||
} | ||
else { | ||
LOGGER.log(Level.FINER, "Message split part: " + messageBuilder.toString()); | ||
|
@@ -230,7 +297,11 @@ private boolean sendMessage(String restRootUrl, String fkey, String message) { | |
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* Queries the 5 latest messages for all chatrooms and enqueues them to | ||
* the subscribed {@link ChatWorker Workers}, respecting the already handled | ||
* timestamps as maintained internally. | ||
* | ||
* @see ChatInterface#queryMessages() | ||
*/ | ||
@Override | ||
public void broadcast(final String message) { | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make stackexchange the if-case and handle the problematic login in a separate method?