All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.jee.api.cassandra.GetSession 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 base.Query;
import base.jee.api.Settings;
import base.security.User;

import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


import static base.jee.Constants.MAX_IP_ADDRESS_LENGTH;

/**
 * Lookup a small set of information about the person associated
 * this token and reset the expriry time of the token. This is
 * the minimal set of data that a user facing client (web page,
 * or iPhone) might need to render the user interface, such as
 * the menu bar.
 */
public class GetSession extends Query {

	private CassandraAPI api;
	private String site;
	private String token;
	private String ip;

	public GetSession(CassandraAPI api, String site, String token, String ip) {

		if(api == null) {
			throw new IllegalArgumentException("Invalid parameter: api");
		}
		if(ip != null && ip.length() > MAX_IP_ADDRESS_LENGTH) {
			throw new IllegalArgumentException("Invalid IP address.");
		}

		this.api = api;
		this.ip = ip;
		this.site = site;
		this.token = token == null?null:token.trim();
	}

	public GetSession() {
	}

	@Override
	public Query newWithParameters(Map parameters) {
		return new GetSession(
				(CassandraAPI)parameters.get("api"),
				(String)parameters.get("site"),
				(String)parameters.get("token"),
				(String)parameters.get("ip")
				);
	}

	public List execute() throws IOException {
		List results = new LinkedList<>();

		if(token == null || token.length() == 0 || site == null || site.length() == 0) {
			User u = new User(api.getUnauthenticatedRolesCache(), ip, site);
			results.add(u);
			return results;
		}

		Settings settings = api.getSettingsCache();
		Session s = api.getCassandraSession();

		PreparedStatement q = s.prepare(
				"select person_uuid, first_name, last_name, roles " +
				"from session_token " +
				"where site=? and uid = ?");
		for(Row r : s.execute(q.bind(site, token))) {
			User u = new User(r.getUUID(0),
					r.getString(1),
					r.getString(2),
					User.roleSet(r.getString(3)),
					api.getAuthenticatedRolesCache(),
					site,
					token,
					ip);
			results.add(u);

			q = s.prepare(
					"update session_token " +
					"set expiry = ? " +
					"where site=? and uid = ?");
			s.execute(q.bind((new Date()).getTime()/1000 + Long.parseLong(settings.get("session.expiry")), site, token));

			return results;
		}

		User u = User.userWithIp(ip, site);
		results.add(u);
		return results;
	}

	@Override
	public String getJsonParameters() {
		return "{" +
			"\"token\":\"" + token + "\"," +
			"\"ip\":" + (ip == null?"null":"\"" + ip + "\"") +
			"}";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy