use of org.wikidata.wdtk.wikibaseapi.ApiConnection in project OpenRefine by OpenRefine.
the class ConnectionManager method logout.
public void logout(String mediaWikiApiEndpoint) {
ApiConnection connection = endpointToConnection.get(mediaWikiApiEndpoint);
if (connection != null) {
try {
connection.logout();
endpointToConnection.remove(mediaWikiApiEndpoint);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} catch (MediaWikiApiErrorException e) {
if ("assertuserfailed".equals(e.getErrorCode())) {
// it turns out we were already logged out
endpointToConnection.remove(mediaWikiApiEndpoint);
} else {
logger.error(e.getMessage(), e);
}
}
}
}
use of org.wikidata.wdtk.wikibaseapi.ApiConnection in project OpenRefine by OpenRefine.
the class LoginCommand method doPost.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (!hasValidCSRFToken(request)) {
respondCSRFError(response);
return;
}
if (manager == null) {
manager = ConnectionManager.getInstance();
}
String mediawikiApiEndpoint = removeCRLF(request.getParameter(API_ENDPOINT));
if (isBlank(mediawikiApiEndpoint)) {
CommandUtilities.respondError(response, "missing parameter '" + API_ENDPOINT + "'");
return;
}
String mediawikiApiEndpointPrefix = sanitizeCookieKey(mediawikiApiEndpoint + '-');
if ("true".equals(request.getParameter("logout"))) {
manager.logout(mediawikiApiEndpoint);
removeUsernamePasswordCookies(mediawikiApiEndpointPrefix, request, response);
removeOwnerOnlyConsumerCookies(mediawikiApiEndpointPrefix, request, response);
respond(request, response);
// return directly
return;
}
boolean remember = "on".equals(request.getParameter("remember-credentials"));
// Credentials from parameters have higher priority than those from cookies.
String username = request.getParameter(USERNAME);
String password = request.getParameter(PASSWORD);
String consumerToken = request.getParameter(CONSUMER_TOKEN);
String consumerSecret = request.getParameter(CONSUMER_SECRET);
String accessToken = request.getParameter(ACCESS_TOKEN);
String accessSecret = request.getParameter(ACCESS_SECRET);
if (isBlank(username) && isBlank(password) && isBlank(consumerToken) && isBlank(consumerSecret) && isBlank(accessToken) && isBlank(accessSecret)) {
// In this case, we use cookies to login, and we will always remember the credentials in cookies.
remember = true;
Map<String, String> cookieMap = processCookiesWithPrefix(mediawikiApiEndpointPrefix, request.getCookies());
username = cookieMap.get(USERNAME);
consumerToken = cookieMap.get(CONSUMER_TOKEN);
consumerSecret = cookieMap.get(CONSUMER_SECRET);
accessToken = cookieMap.get(ACCESS_TOKEN);
accessSecret = cookieMap.get(ACCESS_SECRET);
if (isBlank(consumerToken) && isBlank(consumerSecret) && isBlank(accessToken) && isBlank(accessSecret)) {
// Try logging in with the cookies of a password-based connection.
List<Cookie> cookieList = new ArrayList<>();
for (Map.Entry<String, String> entry : cookieMap.entrySet()) {
if (entry.getKey().startsWith(WIKIBASE_COOKIE_PREFIX)) {
String name = entry.getKey().substring(WIKIBASE_COOKIE_PREFIX.length());
Cookie newCookie = new Cookie(name, entry.getValue());
cookieList.add(newCookie);
}
}
if (cookieList.size() > 0 && isNotBlank(username)) {
removeOwnerOnlyConsumerCookies(mediawikiApiEndpointPrefix, request, response);
if (manager.login(mediawikiApiEndpoint, username, cookieList)) {
respond(request, response);
return;
} else {
removeUsernamePasswordCookies(mediawikiApiEndpointPrefix, request, response);
}
}
}
}
if (isNotBlank(username) && isNotBlank(password)) {
// the old credentials in cookies should be cleared.
if (manager.login(mediawikiApiEndpoint, username, password) && remember) {
ApiConnection connection = manager.getConnection(mediawikiApiEndpoint);
List<HttpCookie> cookies = ((BasicApiConnection) connection).getCookies();
String prefix = mediawikiApiEndpointPrefix + WIKIBASE_COOKIE_PREFIX;
for (HttpCookie cookie : cookies) {
setCookie(response, prefix + cookie.getName(), cookie.getValue());
}
// Though the cookies from the connection contain some cookies of username,
// we cannot make sure that all Wikibase instances use the same cookie key
// to retrieve the username. So we choose to set the username cookie with our own cookie key.
setCookie(response, mediawikiApiEndpointPrefix + USERNAME, connection.getCurrentUser());
} else {
removeUsernamePasswordCookies(mediawikiApiEndpointPrefix, request, response);
}
removeOwnerOnlyConsumerCookies(mediawikiApiEndpointPrefix, request, response);
} else if (isNotBlank(consumerToken) && isNotBlank(consumerSecret) && isNotBlank(accessToken) && isNotBlank(accessSecret)) {
if (manager.login(mediawikiApiEndpoint, consumerToken, consumerSecret, accessToken, accessSecret) && remember) {
setCookie(response, mediawikiApiEndpointPrefix + CONSUMER_TOKEN, consumerToken);
setCookie(response, mediawikiApiEndpointPrefix + CONSUMER_SECRET, consumerSecret);
setCookie(response, mediawikiApiEndpointPrefix + ACCESS_TOKEN, accessToken);
setCookie(response, mediawikiApiEndpointPrefix + ACCESS_SECRET, accessSecret);
} else {
removeOwnerOnlyConsumerCookies(mediawikiApiEndpointPrefix, request, response);
}
removeUsernamePasswordCookies(mediawikiApiEndpointPrefix, request, response);
}
respond(request, response);
}
Aggregations