Skip to content
GMSClient.java 42.5 KiB
Newer Older
        AccessControlContext acContext = AccessController.getContext();
        Subject subject = Subject.getSubject(acContext);
        // only consult cache if the userID is of the calling subject
        if (userIsSubject(userID, subject))
            Set<GroupMemberships> gset = subject.getPrivateCredentials(GroupMemberships.class);
            if (gset == null || gset.isEmpty())
                GroupMemberships mems = new GroupMemberships(serviceID.toString(), userID);
                subject.getPrivateCredentials().add(mems);
                return mems;

            // check to ensure they have the same service URI
            if (!serviceID.toString().equals(mems.getServiceURI()))
            {
                log.debug("Not using cache because of differing service URIs: " +
                    "[" + serviceID.toString() + "][" + mems.getServiceURI() + "]");
    protected Group getCachedGroup(Principal userID, String groupID, Role role)
        List<Group> groups = getCachedGroups(userID, role, false);
        if (groups == null)
            return null; // no cache
        for (Group g : groups)
        {
            if (g.getID().getName().equals(groupID))
        }
        return null;
    }
    protected List<Group> getCachedGroups(Principal userID, Role role, boolean complete)
        GroupMemberships mems = getGroupCache(userID);
        Boolean cacheState = mems.isComplete(role);
        if (!complete || Boolean.TRUE.equals(cacheState))
    protected void addCachedGroup(Principal userID, Group group, Role role)
        GroupMemberships mems = getGroupCache(userID);
Patrick Dowler's avatar
Patrick Dowler committed
        mems.add(group, role);
    protected void setCachedGroups(Principal userID, List<Group> groups, Role role)
        GroupMemberships mems = getGroupCache(userID);
Patrick Dowler's avatar
Patrick Dowler committed
        mems.add(groups, role);
    protected boolean userIsSubject(Principal userID, Subject subject)
    {
        if (userID == null || subject == null)
        {
            return false;
        }
Dustin Jenkins's avatar
Dustin Jenkins committed
        for (Principal subjectPrincipal : subject.getPrincipals())
            if (AuthenticationUtil.equals(subjectPrincipal, userID))
        return false;
    protected RegistryClient getRegistryClient()
    {
        return new RegistryClient();
    }

    /**
     * Lookup the Service URL for the given standard.  The current AuthMethod
     * will be taken into account.
     *
     * @param standard  The URI standard to look up.
     * @return          URL for the service.
     * @throws AccessControlException       If the URL cannot be found for the
     *                                      provided AuthMethod.
     */
    private URL lookupServiceURL(final URI standard)
            throws AccessControlException
    {
        Subject subject = AuthenticationUtil.getCurrentSubject();
        AuthMethod am = getAuthMethod(subject);
        
        URL serviceURL = getRegistryClient().getServiceURL(this.serviceID, standard, am);
        
        // now that we have a URL we can check if the cookie will actually be sent to it
        if (AuthMethod.COOKIE.equals(am))
        {
            try
            {
                boolean domainMatch = false;
                String domain = NetUtil.getDomainName(serviceURL);
                for (SSOCookieCredential cc : subject.getPublicCredentials(SSOCookieCredential.class))
                {
                    if (cc.getDomain().equals(domain))
                        domainMatch = true;
                } 
                if (!domainMatch)
                {
Patrick Dowler's avatar
Patrick Dowler committed
                    throw new AccessControlException("no SSOCookieCredential for domain " + domain);
                }
            }
            catch(IOException ex)
            {
                throw new RuntimeException("failure checking domain for cookie use", ex);
            }
        }
        
        if (serviceURL == null)
        {
Dustin Jenkins's avatar
Dustin Jenkins committed
            throw new RuntimeException(
                    String.format("Unable to get Service URL for '%s', '%s', '%s'",
                                  serviceID.toString(), standard, am));
    
    private AuthMethod getAuthMethod(Subject subject)
Dustin Jenkins's avatar
Dustin Jenkins committed
    {
        if (subject != null)
        {
            // web services use CDP to load a proxy cert so prefer that
            X509CertificateChain privateKeyChain = X509CertificateChain.findPrivateKeyChain(
                    subject.getPublicCredentials());
            if (privateKeyChain != null)
                return AuthMethod.CERT;
            
            // ui applications pass cookie(s) along
            Set sso = subject.getPublicCredentials(SSOCookieCredential.class);
            if ( !sso.isEmpty() )
Dustin Jenkins's avatar
Dustin Jenkins committed
            {
                return AuthMethod.COOKIE;
Dustin Jenkins's avatar
Dustin Jenkins committed
            }
            
            // AuthMethod.PASSWORD not supported
            // AuthMethod.TOKEN not supported
            throw new AccessControlException("No valid public credentials.");
        }
        else
        {
Dustin Jenkins's avatar
Dustin Jenkins committed
            throw new AccessControlException("Anonymous access not supported.");
Dustin Jenkins's avatar
Dustin Jenkins committed
        }
    }