base.jee.api.cassandra.InvalidateSession Maven / Gradle / Ivy
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package base.jee.api.cassandra;
import java.io.IOException;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import base.KeyValue;
import base.Query;
import base.json.Json;
import base.security.User;
import static base.jee.api.cassandra.util.Log.log;
/**
*/
public class InvalidateSession extends Query {
private CassandraAPI api;
private String site;
private String token;
private String ip;
public InvalidateSession() {
}
public InvalidateSession(CassandraAPI api, String site, String token, String ip) {
this.api = api;
this.site = site;
this.token = token;
this.ip = ip;
if(api == null) {
throw new IllegalArgumentException("Invalid parameter: api");
}
if(token == null) {
throw new IllegalArgumentException("Invalid parameter: token");
}
}
@Override
public Query newWithParameters(Map parameters) {
return new InvalidateSession(
(CassandraAPI)parameters.get("api"),
((User)parameters.get("user")).getSite(),
(String)parameters.get("token"),
((User)parameters.get("user")).getIp());
}
public List execute() throws IOException {
List results = new LinkedList<>();
Session s = api.getCassandraSession();
PreparedStatement q = s.prepare("select person_uuid from session_token where site = ? and uid = ?");
for(Row r : s.execute(q.bind(site, token))) {
log(s, "INFO", User.userWithUuidAndIp(r.getUUID(0), ip, site), "Signout.");
q = s.prepare("delete from session_token where site = ? and uid = ?");
s.execute(q.bind(site, token));
results.add(new KeyValue("success", "ok"));
return results;
}
log(s, "FINER", User.userWithIp(ip, ""), "Request to invalidate " + token + " did not correspond to an active session.");
results.add(new KeyValue("success", "ok"));
return results;
}
@Override
public String getJsonParameters() {
return "{" +
"\"site\":\"" + site + "\"," +
"\"token\":\"" + token + "\"," +
"\"ip\":\"" + Json.escape(ip) + "\"" +
"}";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy