Example usage for java.security Principal toString

List of usage examples for java.security Principal toString

Introduction

In this page you can find the example usage for java.security Principal toString.

Prototype

public String toString();

Source Link

Document

Returns a string representation of this principal.

Usage

From source file:org.apache.wiki.PageManager.java

/**
 *  Listens for {@link org.apache.wiki.event.WikiSecurityEvent#PROFILE_NAME_CHANGED}
 *  events. If a user profile's name changes, each page ACL is inspected. If an entry contains
 *  a name that has changed, it is replaced with the new one. No events are emitted
 *  as a consequence of this method, because the page contents are still the same; it is
 *  only the representations of the names within the ACL that are changing.
 * /*from  ww w .j  a v a 2 s.c o m*/
 *  @param event The event
 */
public void actionPerformed(WikiEvent event) {
    if (!(event instanceof WikiSecurityEvent)) {
        return;
    }

    WikiSecurityEvent se = (WikiSecurityEvent) event;
    if (se.getType() == WikiSecurityEvent.PROFILE_NAME_CHANGED) {
        UserProfile[] profiles = (UserProfile[]) se.getTarget();
        Principal[] oldPrincipals = new Principal[] { new WikiPrincipal(profiles[0].getLoginName()),
                new WikiPrincipal(profiles[0].getFullname()), new WikiPrincipal(profiles[0].getWikiName()) };
        Principal newPrincipal = new WikiPrincipal(profiles[1].getFullname());

        // Examine each page ACL
        try {
            int pagesChanged = 0;
            Collection pages = getAllPages();
            for (Iterator it = pages.iterator(); it.hasNext();) {
                WikiPage page = (WikiPage) it.next();
                boolean aclChanged = changeAcl(page, oldPrincipals, newPrincipal);
                if (aclChanged) {
                    // If the Acl needed changing, change it now
                    try {
                        m_engine.getAclManager().setPermissions(page, page.getAcl());
                    } catch (WikiSecurityException e) {
                        log.error(
                                "Could not change page ACL for page " + page.getName() + ": " + e.getMessage(),
                                e);
                    }
                    pagesChanged++;
                }
            }
            log.info("Profile name change for '" + newPrincipal.toString() + "' caused " + pagesChanged
                    + " page ACLs to change also.");
        } catch (ProviderException e) {
            // Oooo! This is really bad...
            log.error("Could not change user name in Page ACLs because of Provider error:" + e.getMessage(), e);
        }
    }
}

From source file:org.gluu.oxtrust.action.Authenticator.java

/**
 * Authenticate using credentials passed from web request header
 *//*from  www .  ja v a 2s.c  o m*/
public boolean shibboleth2Authenticate() {
    log.debug("Checking if user authenticated with shibboleth already");
    boolean result = false;
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();

    String authType = request.getAuthType();
    String userUid = request.getHeader("REMOTE_USER");
    String userUidlower = request.getHeader("remote_user");
    Enumeration<?> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        log.trace(headerName + "-->" + request.getHeader(headerName));
    }
    log.debug("Username is " + userUid);
    log.debug("UsernameLower is " + userUidlower);
    log.debug("AuthType is " + authType);

    Map<String, String[]> headers = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestHeaderValuesMap();
    for (String name : headers.keySet()) {
        log.trace(name + "==>" + StringUtils.join(headers.get(name)));
    }

    if (StringHelper.isEmpty(userUid) || StringHelper.isEmpty(authType) || !authType.equals("shibboleth")) {
        result = false;
        return result;
    }

    Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
    Matcher matcher = pattern.matcher(userUid);

    User user = null;
    if (matcher.matches()) {
        // Find user by uid
        user = personService.getPersonByEmail(userUid);
    } else {
        // Find user by uid
        user = personService.getUserByUid(userUid);
    }

    if (user == null) {
        result = false;
        return result;
    }
    log.debug("Person Inum is " + user.getInum());

    if (GluuStatus.ACTIVE.getValue().equals(user.getAttribute("gluuStatus"))) {

        credentials.setUsername(user.getUid());
        // credentials.setPassword("");
        Principal principal = new SimplePrincipal(user.getUid());
        log.debug("Principal is " + principal.toString());

        identity.acceptExternallyAuthenticatedPrincipal(principal);

        log.info("User '{0}' authenticated with shibboleth already", userUid);
        identity.quietLogin();
        postLogin(user);

        Contexts.getSessionContext().set(OxTrustConstants.APPLICATION_AUTHORIZATION_TYPE,
                OxTrustConstants.APPLICATION_AUTHORIZATION_NAME_SHIBBOLETH2);

        result = true;
        if (Events.exists()) {
            facesMessages.clear();
            Events.instance().raiseEvent(Identity.EVENT_LOGIN_SUCCESSFUL);
        }
    } else {
        result = false;
    }

    return result;
}

From source file:org.lockss.servlet.LockssServlet.java

String getUsername() {
    Principal user = req.getUserPrincipal();
    return user != null ? user.toString() : null;
}

From source file:org.openremote.controller.rest.FindCertificateByID.java

protected String getChain(String username) throws Exception {
    username = URLDecoder.decode(username, "UTF-8");
    String rootCAPath = configurationService.getItem("ca_path");
    String keystore = rootCAPath + "/server.jks";

    StringBuffer sb = new StringBuffer();
    sb.append(Constants.STATUS_XML_HEADER);

    sb.append("\n<chain>\n<server>\n");

    try {//from  www.java  2s . c  o  m
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(new FileInputStream(keystore), "password".toCharArray());
        Certificate certificate = ks.getCertificate(CA_ALIAS);
        sb.append(new String(Base64.encodeBase64(certificate.getEncoded())));
    } catch (KeyStoreException e) {
        logger.error(e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        logger.error(e.getMessage());
    } catch (CertificateException e) {
        logger.error(e.getMessage());
    }

    sb.append("</server>\n<client>\n");

    try {
        Certificate certificate = clientService.getClientCertificate(username);
        if (certificate != null) {
            // Check client certificate
            //if(clientService.(dn, datum)
            X509Certificate x509cert = (X509Certificate) certificate;
            Principal dname = x509cert.getSubjectDN();
            Date notAfterDate = x509cert.getNotAfter();

            if (clientService.isClientValid(dname.toString())) {
                if (clientService.isClientDateValid(notAfterDate)) {
                    sb.append(new String(Base64.encodeBase64(certificate.getEncoded())));
                } else {
                    throw new Exception(ERROR_DATE_EXPIRED);
                }
            } else {
                throw new Exception(ERROR_INVALID_DN);
            }
        } else {
            logger.error("Client certificate is not found/null.");
        }
    } catch (CertificateEncodingException e) {
        logger.error(e.getMessage());
    }

    sb.append("</client>\n</chain>");
    sb.append(Constants.STATUS_XML_TAIL);

    return sb.toString();
}