Example usage for javax.security.auth Subject getPrincipals

List of usage examples for javax.security.auth Subject getPrincipals

Introduction

In this page you can find the example usage for javax.security.auth Subject getPrincipals.

Prototype

public Set<Principal> getPrincipals() 

Source Link

Document

Return the Set of Principals associated with this Subject .

Usage

From source file:de.ingrid.server.security.IngridRealm.java

@Override
public Principal authenticate(final String userName, final Object password, final Request request) {

    Principal principal = null;/*ww  w. j  a  v a2 s  .co  m*/
    try {
        final RequestCallbackHandler handler = new RequestCallbackHandler(request);
        final LoginContext loginContext = new LoginContext("IngridLogin", handler);
        loginContext.login();
        final Subject subject = loginContext.getSubject();
        final Set<Principal> principals = subject.getPrincipals();
        final Principal tmpPrincipal = principals.isEmpty() ? principal : principals.iterator().next();
        if (tmpPrincipal instanceof KnownPrincipal) {
            final KnownPrincipal knownPrincipal = (KnownPrincipal) tmpPrincipal;
            knownPrincipal.setLoginContext(loginContext);
            principal = knownPrincipal;
            LOG.info("principal has logged in: " + principal);
        }
    } catch (final LoginException e) {
        LOG.error("login error for user: " + userName);
    }
    if (principal == null) {
        LOG.info("login failed for userName: " + userName);
    }
    return principal;
}

From source file:edu.internet2.middleware.shibboleth.idp.system.conf1.SAML2SSOTestCase.java

protected Saml2LoginContext buildLoginContext(String relyingPartyId) throws Exception {
    Principal principal = new UsernamePrincipal("test");

    Subject subject = new Subject();
    subject.getPrincipals().add(principal);

    AuthenticationMethodInformation authnInfo = new AuthenticationMethodInformationImpl(subject, principal,
            "urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified", new DateTime(), 3600);

    AuthnRequest request = buildAuthnRequest(relyingPartyId);

    Saml2LoginContext loginContext = new Saml2LoginContext(relyingPartyId, null, request);
    loginContext.setAuthenticationMethodInformation(authnInfo);
    loginContext.setPrincipalAuthenticated(true);
    loginContext.setRelyingParty(relyingPartyId);

    return loginContext;
}

From source file:de.ingrid.admin.security.IngridRealm.java

@Override
public Principal authenticate(String userName, Object password, Request request) {

    Principal principal = null;/*from  w  w  w .ja  v a 2  s .  c  om*/
    try {
        RequestCallbackHandler handler = new RequestCallbackHandler(request);
        String[] url = request.getRequestURL().toString().split("/base/auth/j_security_check");
        // remember redirect url to jump to after initialization
        request.getSession().setAttribute("redirectUrl",
                request.getSession().getAttribute("org.mortbay.jetty.URI"));
        // automatically redirect to the welcome page, which initialize plug description into session
        request.getSession().setAttribute("org.mortbay.jetty.URI", url[0].concat("/base/welcome.html"));
        LoginContext loginContext = new LoginContext("IngridLogin", handler);
        loginContext.login();
        Subject subject = loginContext.getSubject();
        Set<Principal> principals = subject.getPrincipals();
        Principal tmpPrincipal = principals.isEmpty() ? principal : principals.iterator().next();
        if (tmpPrincipal instanceof KnownPrincipal) {
            KnownPrincipal knownPrincipal = (KnownPrincipal) tmpPrincipal;
            knownPrincipal.setLoginContext(loginContext);
            principal = knownPrincipal;
            LOG.info("principal has logged in: " + principal);
        }
    } catch (LoginException e) {
        LOG.error("login error for user: " + userName, e);
    }
    if (principal == null) {
        LOG.info("login failed for userName: " + userName);
    }
    return principal;
}

From source file:org.betaconceptframework.astroboa.console.security.IdentityStoreRunAsSystem.java

private Subject createSubjectForSystemUserAndItsRoles(String cmsRepositoryId) {

    Subject subject = new Subject();

    //System identity
    subject.getPrincipals().add(new IdentityPrincipal(IdentityPrincipal.SYSTEM));

    //Load default roles for SYSTEM USER
    //Must return at list one group named "Roles" in order to be 
    Group rolesPrincipal = new CmsGroup(AstroboaPrincipalName.Roles.toString());

    for (CmsRole cmsRole : CmsRole.values()) {
        rolesPrincipal.addMember(new CmsPrincipal(CmsRoleAffiliationFactory.INSTANCE
                .getCmsRoleAffiliationForRepository(cmsRole, cmsRepositoryId)));
    }//from  w w  w .j  a  v a 2s  . c  om

    subject.getPrincipals().add(rolesPrincipal);

    return subject;
}

From source file:fi.okm.mpass.shibboleth.authn.impl.ValidateWilmaResponse.java

/** {@inheritDoc} */
@Override/*  w  w  w.  ja v  a2  s  .co  m*/
@Nonnull
protected Subject populateSubject(@Nonnull final Subject subject) {
    subject.getPrincipals().add(new UsernamePrincipal(String
            .valueOf(getQueryParam(getHttpServletRequest(), WilmaAuthenticationContext.PARAM_NAME_USER_ID))));
    log.trace("{}: Subject successfully populated", getLogPrefix());
    return subject;
}

From source file:org.jspresso.hrsample.backend.BackTestStartup.java

private Subject createTestSubject() {
    Subject testSubject = new Subject();
    UserPrincipal p = new UserPrincipal("demo");
    testSubject.getPrincipals().add(p);
    p.putCustomProperty(UserPrincipal.LANGUAGE_PROPERTY, "en");
    Group rolesGroup = new SimpleGroup(SecurityHelper.ROLES_GROUP_NAME);
    rolesGroup.addMember(new SimplePrincipal("administrator"));
    testSubject.getPrincipals().add(rolesGroup);
    return testSubject;
}

From source file:org.apache.activemq.jaas.PropertiesLoginModuleTest.java

public void testLogin() throws LoginException {
    LoginContext context = new LoginContext("PropertiesLogin", new UserPassHandler("first", "secret"));
    context.login();/*from   w  ww  . j a v a  2s .c om*/

    Subject subject = context.getSubject();

    assertEquals("Should have three principals", 3, subject.getPrincipals().size());
    assertEquals("Should have one user principal", 1, subject.getPrincipals(UserPrincipal.class).size());
    assertEquals("Should have two group principals", 2, subject.getPrincipals(GroupPrincipal.class).size());

    context.logout();

    assertEquals("Should have zero principals", 0, subject.getPrincipals().size());
}

From source file:uk.org.openeyes.oink.security.TestSimpleIdentityService.java

@Test
public void testGetOrganizationWorksForValidSubject() {
    SimpleIdentityService identityService = new SimpleIdentityService();
    Subject s = new Subject();
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("bob@moorfields",
            "password");
    s.getPrincipals().add(token);

    String organisation = identityService.getOrganisation(s);

    String expectedOrganisation = "moorfields";
    assertEquals(expectedOrganisation, organisation);
}

From source file:uk.org.openeyes.oink.security.TestSimpleIdentityService.java

@Test
public void testGetUserIdForValidSubject() {
    SimpleIdentityService identityService = new SimpleIdentityService();
    Subject s = new Subject();
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("bob@moorfields",
            "password");
    s.getPrincipals().add(token);

    String user = identityService.getUserId(s);

    String expectedUser = "bob";
    assertEquals(expectedUser, user);/*  www.j  a  v a2 s . c  o  m*/
}

From source file:org.josso.auth.scheme.test.RememberMeAuthSchemeTest.java

@Test
public void testRememberMe() throws Exception {

    RememberMeAuthScheme scheme = (RememberMeAuthScheme) applicationContext
            .getBean("josso-rememberme-authentication");
    assert scheme != null : "No authentication scheme configured";

    String tokenValue = scheme.getRemembermeTokenForUser("user1");

    Credential token = scheme.newCredential(RememberMeAuthScheme.REMEMBER_ME_TOKEN_CREDENTIAL_NAME, tokenValue);
    assert token != null : "No 'token' Credential created by provider";

    Credential username = scheme.newCredential(RememberMeAuthScheme.USERNAME_CREDENTIAL_NAME, "user1");
    assert username != null : "No 'username' Credential created by provider";

    Subject s = new Subject();
    scheme.initialize(new Credential[] { token, username }, s);

    scheme.authenticate();/* w ww .j av  a2 s  .c  o  m*/
    scheme.confirm();

    assert s.getPrincipals().size() == 1 : "Expected one principal, got : " + s.getPrincipals().size();

    Principal user = s.getPrincipals().iterator().next();
    assert user.getName().equals("user1") : "Expected user1 principal, got : " + user.getName();
}