Search in sources :

Example 1 with Credential

use of org.eclipse.jetty.util.security.Credential in project jetty.project by eclipse.

the class LdapLoginModule method getUserInfo.

/**
     * get the available information about the user
     * <p>
     * for this LoginModule, the credential can be null which will result in a
     * binding ldap authentication scenario
     * <p>
     * roles are also an optional concept if required
     *
     * @param username the user name
     * @return the userinfo for the username
     * @throws Exception if unable to get the user info
     */
public UserInfo getUserInfo(String username) throws Exception {
    Attributes attributes = getUserAttributes(username);
    String pwdCredential = getUserCredentials(attributes);
    if (pwdCredential == null) {
        return null;
    }
    pwdCredential = convertCredentialLdapToJetty(pwdCredential);
    Credential credential = Credential.getCredential(pwdCredential);
    return new LDAPUserInfo(username, credential, attributes);
}
Also used : Credential(org.eclipse.jetty.util.security.Credential) Attributes(javax.naming.directory.Attributes)

Example 2 with Credential

use of org.eclipse.jetty.util.security.Credential in project blade by biezhi.

the class PropertyUserStore method loadUsers.

/* ------------------------------------------------------------ */
protected void loadUsers() throws IOException {
    if (_configPath == null)
        return;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading " + this + " from " + _configPath);
    }
    Properties properties = new Properties();
    if (getConfigResource().exists())
        properties.load(getConfigResource().getInputStream());
    Set<String> known = new HashSet<String>();
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        String username = ((String) entry.getKey()).trim();
        String credentials = ((String) entry.getValue()).trim();
        String roles = null;
        int c = credentials.indexOf(',');
        if (c > 0) {
            roles = credentials.substring(c + 1).trim();
            credentials = credentials.substring(0, c).trim();
        }
        if (username != null && username.length() > 0 && credentials != null && credentials.length() > 0) {
            String[] roleArray = IdentityService.NO_ROLES;
            if (roles != null && roles.length() > 0) {
                roleArray = StringUtil.csvSplit(roles);
            }
            known.add(username);
            Credential credential = Credential.getCredential(credentials);
            Principal userPrincipal = new AbstractLoginService.UserPrincipal(username, credential);
            Subject subject = new Subject();
            subject.getPrincipals().add(userPrincipal);
            subject.getPrivateCredentials().add(credential);
            if (roles != null) {
                for (String role : roleArray) {
                    subject.getPrincipals().add(new AbstractLoginService.RolePrincipal(role));
                }
            }
            subject.setReadOnly();
            _knownUserIdentities.put(username, _identityService.newUserIdentity(subject, userPrincipal, roleArray));
            notifyUpdate(username, credential, roleArray);
        }
    }
    synchronized (_knownUsers) {
        /*
             * if its not the initial load then we want to process removed users
             */
        if (!_firstLoad) {
            Iterator<String> users = _knownUsers.iterator();
            while (users.hasNext()) {
                String user = users.next();
                if (!known.contains(user)) {
                    _knownUserIdentities.remove(user);
                    notifyRemove(user);
                }
            }
        }
        /*
             * reset the tracked _users list to the known users we just processed
             */
        _knownUsers.clear();
        _knownUsers.addAll(known);
    }
    /*
         * set initial load to false as there should be no more initial loads
         */
    _firstLoad = false;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loaded " + this + " from " + _configPath);
    }
}
Also used : Credential(org.eclipse.jetty.util.security.Credential) Properties(java.util.Properties) Subject(javax.security.auth.Subject) HashMap(java.util.HashMap) Map(java.util.Map) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 3 with Credential

use of org.eclipse.jetty.util.security.Credential in project jetty.project by eclipse.

the class PropertyFileLoginModule method getUserInfo.

/**
     * 
     *
     * @param userName the user name
     * @throws Exception if unable to get the user information
     */
public UserInfo getUserInfo(String userName) throws Exception {
    PropertyUserStore propertyUserStore = _propertyUserStores.get(_filename);
    if (propertyUserStore == null)
        throw new IllegalStateException("PropertyUserStore should never be null here!");
    LOG.debug("Checking PropertyUserStore " + _filename + " for " + userName);
    UserIdentity userIdentity = propertyUserStore.getUserIdentity(userName);
    if (userIdentity == null)
        return null;
    //TODO in future versions change the impl of PropertyUserStore so its not
    //storing Subjects etc, just UserInfo
    Set<Principal> principals = userIdentity.getSubject().getPrincipals();
    List<String> roles = new ArrayList<String>();
    for (Principal principal : principals) {
        roles.add(principal.getName());
    }
    Credential credential = (Credential) userIdentity.getSubject().getPrivateCredentials().iterator().next();
    LOG.debug("Found: " + userName + " in PropertyUserStore " + _filename);
    return new UserInfo(userName, credential, roles);
}
Also used : PropertyUserStore(org.eclipse.jetty.security.PropertyUserStore) Credential(org.eclipse.jetty.util.security.Credential) UserIdentity(org.eclipse.jetty.server.UserIdentity) ArrayList(java.util.ArrayList) Principal(java.security.Principal)

Example 4 with Credential

use of org.eclipse.jetty.util.security.Credential in project jetty.project by eclipse.

the class PropertyUserStore method loadUsers.

/* ------------------------------------------------------------ */
protected void loadUsers() throws IOException {
    if (_configPath == null)
        return;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loading " + this + " from " + _configPath);
    }
    Properties properties = new Properties();
    if (getConfigResource().exists())
        properties.load(getConfigResource().getInputStream());
    Set<String> known = new HashSet<String>();
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        String username = ((String) entry.getKey()).trim();
        String credentials = ((String) entry.getValue()).trim();
        String roles = null;
        int c = credentials.indexOf(',');
        if (c > 0) {
            roles = credentials.substring(c + 1).trim();
            credentials = credentials.substring(0, c).trim();
        }
        if (username != null && username.length() > 0 && credentials != null && credentials.length() > 0) {
            String[] roleArray = IdentityService.NO_ROLES;
            if (roles != null && roles.length() > 0) {
                roleArray = StringUtil.csvSplit(roles);
            }
            known.add(username);
            Credential credential = Credential.getCredential(credentials);
            Principal userPrincipal = new AbstractLoginService.UserPrincipal(username, credential);
            Subject subject = new Subject();
            subject.getPrincipals().add(userPrincipal);
            subject.getPrivateCredentials().add(credential);
            if (roles != null) {
                for (String role : roleArray) {
                    subject.getPrincipals().add(new AbstractLoginService.RolePrincipal(role));
                }
            }
            subject.setReadOnly();
            _knownUserIdentities.put(username, _identityService.newUserIdentity(subject, userPrincipal, roleArray));
            notifyUpdate(username, credential, roleArray);
        }
    }
    synchronized (_knownUsers) {
        /*
             * if its not the initial load then we want to process removed users
             */
        if (!_firstLoad) {
            Iterator<String> users = _knownUsers.iterator();
            while (users.hasNext()) {
                String user = users.next();
                if (!known.contains(user)) {
                    _knownUserIdentities.remove(user);
                    notifyRemove(user);
                }
            }
        }
        /*
             * reset the tracked _users list to the known users we just processed
             */
        _knownUsers.clear();
        _knownUsers.addAll(known);
    }
    /*
         * set initial load to false as there should be no more initial loads
         */
    _firstLoad = false;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Loaded " + this + " from " + _configPath);
    }
}
Also used : Credential(org.eclipse.jetty.util.security.Credential) Properties(java.util.Properties) Subject(javax.security.auth.Subject) HashMap(java.util.HashMap) Map(java.util.Map) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 5 with Credential

use of org.eclipse.jetty.util.security.Credential in project elasticsearch-jetty by sonian.

the class ESLoginService method loadUser.

@Override
public UserIdentity loadUser(String user) {
    Log.debug("attempting to load user [{}]", user);
    try {
        GetResponse response = client.prepareGet(authIndex, authType, user).setFields(passwordField, rolesField).execute().actionGet();
        if (response.isExists()) {
            Log.debug("user [{}] exists; looking for credentials...", user);
            Credential credential = null;
            GetField passwordGetField = response.getField(passwordField);
            if (passwordGetField != null) {
                Log.debug("user [{}] using password auth", user);
                credential = Credential.getCredential((String) passwordGetField.getValue());
            }
            String[] roles = getStringValues(response.getField(rolesField));
            return putUser(user, credential, roles);
        }
    } catch (IndexMissingException e) {
        Log.warn("no auth index [{}]", authIndex);
    } catch (Exception e) {
        Log.warn("error finding user [" + user + "] in [" + authIndex + "]", e);
    }
    return null;
}
Also used : Credential(org.eclipse.jetty.util.security.Credential) GetField(org.elasticsearch.index.get.GetField) IndexMissingException(org.elasticsearch.indices.IndexMissingException) GetResponse(org.elasticsearch.action.get.GetResponse) IndexMissingException(org.elasticsearch.indices.IndexMissingException)

Aggregations

Credential (org.eclipse.jetty.util.security.Credential)7 Principal (java.security.Principal)4 Subject (javax.security.auth.Subject)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Properties (java.util.Properties)2 UserIdentity (org.eclipse.jetty.server.UserIdentity)2 ArrayList (java.util.ArrayList)1 Attributes (javax.naming.directory.Attributes)1 HttpException (org.apache.jena.atlas.web.HttpException)1 FusekiException (org.apache.jena.fuseki.FusekiException)1 KnownUser (org.eclipse.jetty.security.MappedLoginService.KnownUser)1 RolePrincipal (org.eclipse.jetty.security.MappedLoginService.RolePrincipal)1 PropertyUserStore (org.eclipse.jetty.security.PropertyUserStore)1 Password (org.eclipse.jetty.util.security.Password)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 GetField (org.elasticsearch.index.get.GetField)1 IndexMissingException (org.elasticsearch.indices.IndexMissingException)1