View Javadoc
1   package org.argeo.cms.auth;
2   
3   import java.time.ZonedDateTime;
4   import java.util.Collection;
5   import java.util.Locale;
6   import java.util.UUID;
7   
8   import javax.naming.ldap.LdapName;
9   import javax.security.auth.Subject;
10  
11  import org.argeo.naming.LdapAttrs;
12  import org.osgi.framework.BundleContext;
13  import org.osgi.framework.InvalidSyntaxException;
14  import org.osgi.framework.ServiceReference;
15  import org.osgi.service.useradmin.Authorization;
16  
17  /** An authenticated user session. */
18  public interface CmsSession {
19  	final static String USER_DN = LdapAttrs.DN;
20  	final static String SESSION_UUID = LdapAttrs.entryUUID.name();
21  	final static String SESSION_LOCAL_ID = LdapAttrs.uniqueIdentifier.name();
22  
23  	UUID getUuid();
24  
25  	LdapName getUserDn();
26  
27  	String getLocalId();
28  
29  	Authorization getAuthorization();
30  
31  	boolean isAnonymous();
32  
33  	ZonedDateTime getCreationTime();
34  
35  	ZonedDateTime getEnd();
36  
37  	Locale getLocale();
38  
39  	boolean isValid();
40  
41  	/** @return The {@link CmsSession} for this {@link Subject} or null. */
42  	static CmsSession getCmsSession(BundleContext bc, Subject subject) {
43  		if (subject.getPrivateCredentials(CmsSessionId.class).isEmpty())
44  			return null;
45  		CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
46  		String uuid = cmsSessionId.getUuid().toString();
47  		Collection<ServiceReference<CmsSession>> sr;
48  		try {
49  			sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_UUID + "=" + uuid + ")");
50  		} catch (InvalidSyntaxException e) {
51  			throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid, e);
52  		}
53  		ServiceReference<CmsSession> cmsSessionRef;
54  		if (sr.size() == 1) {
55  			cmsSessionRef = sr.iterator().next();
56  			return bc.getService(cmsSessionRef);
57  		} else if (sr.size() == 0) {
58  			return null;
59  		} else
60  			throw new IllegalStateException(sr.size() + " CMS sessions registered for " + uuid);
61  	}
62  }