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

pl.edu.icm.unity.stdext.identity.SessionIdentityModel Maven / Gradle / Ivy

Go to download

Standard plugins which are distributed with the system: attribute syntaxes, identity types, credentials

There is a newer version: 4.0.2
Show newest version
/*
 * Copyright (c) 2014 ICM Uniwersytet Warszawski All rights reserved.
 * See LICENCE.txt file for licensing information.
 */
package pl.edu.icm.unity.stdext.identity;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import pl.edu.icm.unity.engine.api.authn.LoginSession;

/**
 * Maintains targeted identities value. The data is stored per session identifiers. Additionally 
 * information about expiration handling is stored.
 * @author K. Benedyczak
 */
public class SessionIdentityModel
{
	private ObjectMapper mapper;
	private PerSessionEntry entry;
	
	public SessionIdentityModel(ObjectMapper mapper, ObjectNode entryVal)
	{
		this.mapper = mapper;
		entry = new PerSessionEntry(
				entryVal.get("absoluteTTL").asLong(), 
				entryVal.get("relativeTTL").asLong(),
				entryVal.get("lastUsage").asLong(),
				entryVal.get("idValue").asText());
	}

	public SessionIdentityModel(ObjectMapper mapper, LoginSession session, String identity)
	{
		this.mapper = mapper;
		entry = new PerSessionEntry(
				session.getExpires() == null ? -1 : session.getExpires().getTime(), 
				session.getMaxInactivity()*10, 
				System.currentTimeMillis(), 
				identity);
		long msInHour = 3600000;
		if (entry.relativeTTL < 24*msInHour)
			entry.relativeTTL = 24*msInHour;
	}
	
	public ObjectNode serialize()
	{
		ObjectNode eN = mapper.createObjectNode();
		eN.put("absoluteTTL", entry.absoluteTTL);
		eN.put("relativeTTL", entry.relativeTTL);
		eN.put("lastUsage", entry.lastUsage);
		eN.put("idValue", entry.idValue);
		return eN;
	}
	
	public PerSessionEntry getEntry()
	{
		return entry;
	}


	public static class PerSessionEntry
	{
		private long absoluteTTL;
		private long relativeTTL;
		private long lastUsage;
		private String idValue;

		public PerSessionEntry(long absoluteTTL, long relativeTTL, long lastUsage, String idValue)
		{
			this.absoluteTTL = absoluteTTL;
			this.relativeTTL = relativeTTL;
			this.lastUsage = lastUsage;
			this.idValue = idValue;
		}
		
		public boolean isExpired()
		{
			long now = System.currentTimeMillis();
			return absoluteTTL < now || relativeTTL < now-lastUsage;
		}
		
		public String getValue()
		{
			return idValue;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy