Java Code Examples for javax.security.auth.Subject#getPublicCredentials()

The following examples show how to use javax.security.auth.Subject#getPublicCredentials() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskanaEngineImpl.java    From taskana with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T runAsAdmin(Supplier<T> supplier) {

  Subject subject = Subject.getSubject(AccessController.getContext());
  if (subject == null) {
    // dont add authorisation if none is available.
    return supplier.get();
  }

  Set<Principal> principalsCopy = new HashSet<>(subject.getPrincipals());
  Set<Object> privateCredentialsCopy = new HashSet<>(subject.getPrivateCredentials());
  Set<Object> publicCredentialsCopy = new HashSet<>(subject.getPublicCredentials());

  String adminName =
      this.getEngine().getConfiguration().getRoleMap().get(TaskanaRole.ADMIN).stream()
          .findFirst()
          .orElseThrow(() -> new TaskanaRuntimeException("There is no admin configured"));

  principalsCopy.add(new GroupPrincipal(adminName));
  Subject subject1 =
      new Subject(true, principalsCopy, privateCredentialsCopy, publicCredentialsCopy);

  return Subject.doAs(subject1, (PrivilegedAction<T>) supplier::get);
}
 
Example 2
Source File: SsoUtil.java    From iaf with Apache License 2.0 6 votes vote down vote up
public static String getSsoToken() throws WSSecurityException, CredentialDestroyedException, CredentialExpiredException {
	String result=null;

	Subject subj=WSSubject.getCallerSubject();

	if (subj==null) {
		throw new WSSecurityException("could not find Subject");
	}
	Set pubs=subj.getPublicCredentials();
	if (pubs==null) {
		throw new WSSecurityException("could not find PublicCredentials");
	}
	for (Iterator it=pubs.iterator();result==null && it.hasNext();) {
		Object pc = it.next();
		if (pc instanceof WSCredentialImpl) {
			WSCredentialImpl wsci = (WSCredentialImpl)pc;
			byte token[] = wsci.getCredentialToken();
			if (token!=null && token.length>0) {
				result=Base64.encodeBase64String(token);
			}
		}
	}
	return result;
}
 
Example 3
Source File: CurrentUserContext.java    From taskana with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the unique security name of the first public credentials found in the WSSubject as
 * userid.
 *
 * @return the userid of the caller. If the userid could not be obtained, null is returned.
 */
private static String getUserIdFromWsSubject() {
  try {
    Class<?> wsSubjectClass = Class.forName(WSSUBJECT_CLASSNAME);
    Method getCallerSubjectMethod =
        wsSubjectClass.getMethod(GET_CALLER_SUBJECT_METHOD, (Class<?>[]) null);
    Subject callerSubject = (Subject) getCallerSubjectMethod.invoke(null, (Object[]) null);
    LOGGER.debug("Subject of caller: {}", callerSubject);
    if (callerSubject != null) {
      Set<Object> publicCredentials = callerSubject.getPublicCredentials();
      LOGGER.debug("Public credentials of caller: {}", publicCredentials);
      return publicCredentials.stream()
          .map(
              wrap(
                  credential ->
                      credential
                          .getClass()
                          .getMethod(GET_UNIQUE_SECURITY_NAME_METHOD, (Class<?>[]) null)
                          .invoke(credential, (Object[]) null)))
          .peek(
              o ->
                  LOGGER.debug(
                      "Returning the unique security name of first public credential: {}", o))
          .map(Object::toString)
          .map(CurrentUserContext::convertAccessId)
          .findFirst()
          .orElse(null);
    }
  } catch (Exception e) {
    LOGGER.warn("Could not get user from WSSubject. Going ahead unauthorized.");
  }
  return null;
}
 
Example 4
Source File: HttpManagementUtil.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
public static Subject createServletConnectionSubject(final HttpServletRequest request, Subject original)
{
    Subject subject = new Subject(false,
                          original.getPrincipals(),
                          original.getPublicCredentials(),
                          original.getPrivateCredentials());
    subject.getPrincipals().add(new ServletConnectionPrincipal(request));
    subject.setReadOnly();
    return subject;
}
 
Example 5
Source File: SubjectNullTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void testAddAll() {
    // Create a well formed subject and additional collections
    Subject mtSubj = makeSubj(false, false, false);
    Set<Principal> morePrincs = new HashSet<>(Arrays.asList(tmplAddPrincs));
    Set<Object> morePubVals = new HashSet<>(Arrays.asList(tmplAddPubVals));
    Set<Object> morePrvVals = new HashSet<>(Arrays.asList(tmplAddPrvVals));

    // Run one success test for each Subject family to verify the
    // overloaded method works as intended.
    Set<Principal> setPrin = mtSubj.getPrincipals();
    Set<Object> setPubCreds = mtSubj.getPublicCredentials();
    Set<Object> setPrvCreds = mtSubj.getPrivateCredentials();
    int prinOrigSize = setPrin.size();
    int pubOrigSize = setPubCreds.size();
    int prvOrigSize = setPrvCreds.size();

    System.out.println("------ addAll() -----");

    // Add the new members, then check the resulting size of the
    // Subject attributes to verify they've increased by the proper
    // amounts.
    if ((validTestCollection(methAdd, setPrin, morePrincs) != true) ||
        (setPrin.size() != prinOrigSize + morePrincs.size()))
    {
        throw new RuntimeException("Failed addAll() on principals");
    }
    if ((validTestCollection(methAdd, setPubCreds,
            morePubVals) != true) ||
        (setPubCreds.size() != pubOrigSize + morePubVals.size()))
    {
        throw new RuntimeException("Failed addAll() on public creds");
    }
    if ((validTestCollection(methAdd, setPrvCreds,
            morePrvVals) != true) ||
        (setPrvCreds.size() != prvOrigSize + morePrvVals.size()))
    {
        throw new RuntimeException("Failed addAll() on private creds");
    }
    System.out.println("Positive addAll() test passed");

    // Now add null elements into each container, then retest
    morePrincs.add(null);
    morePubVals.add(null);
    morePrvVals.add(null);

    System.out.println("* Testing addAll w/ null values on Principals");
    nullTestCollection(methAdd, mtSubj.getPrincipals(), null);
    nullTestCollection(methAdd, mtSubj.getPrincipals(), morePrincs);

    System.out.println("* Testing addAll w/ null values on Public Creds");
    nullTestCollection(methAdd, mtSubj.getPublicCredentials(), null);
    nullTestCollection(methAdd, mtSubj.getPublicCredentials(),
            morePubVals);

    System.out.println("* Testing addAll w/ null values on Private Creds");
    nullTestCollection(methAdd, mtSubj.getPrivateCredentials(), null);
    nullTestCollection(methAdd, mtSubj.getPrivateCredentials(),
            morePrvVals);
}
 
Example 6
Source File: SubjectNullTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void testRemoveAll() {
    // Create a well formed subject and additional collections
    Subject mtSubj = makeSubj(false, false, false);
    Set<Principal> remPrincs = new HashSet<>();
    Set<Object> remPubVals = new HashSet<>();
    Set<Object> remPrvVals = new HashSet<>();

    remPrincs.add(new KerberosPrincipal("mtwain/[email protected]"));
    remPubVals.add("mtwain");
    remPrvVals.add("5Cl3M3nz");

    // Run one success test for each Subject family to verify the
    // overloaded method works as intended.
    Set<Principal> setPrin = mtSubj.getPrincipals();
    Set<Object> setPubCreds = mtSubj.getPublicCredentials();
    Set<Object> setPrvCreds = mtSubj.getPrivateCredentials();
    int prinOrigSize = setPrin.size();
    int pubOrigSize = setPubCreds.size();
    int prvOrigSize = setPrvCreds.size();

    System.out.println("------ removeAll() -----");

    // Remove the specified members, then check the resulting size of the
    // Subject attributes to verify they've decreased by the proper
    // amounts.
    if ((validTestCollection(methRemove, setPrin, remPrincs) != true) ||
        (setPrin.size() != prinOrigSize - remPrincs.size()))
    {
        throw new RuntimeException("Failed removeAll() on principals");
    }
    if ((validTestCollection(methRemove, setPubCreds,
            remPubVals) != true) ||
        (setPubCreds.size() != pubOrigSize - remPubVals.size()))
    {
        throw new RuntimeException("Failed removeAll() on public creds");
    }
    if ((validTestCollection(methRemove, setPrvCreds,
            remPrvVals) != true) ||
        (setPrvCreds.size() != prvOrigSize - remPrvVals.size()))
    {
        throw new RuntimeException("Failed removeAll() on private creds");
    }
    System.out.println("Positive removeAll() test passed");

    // Now add null elements into each container, then retest
    remPrincs.add(null);
    remPubVals.add(null);
    remPrvVals.add(null);

    System.out.println("* Testing removeAll w/ null values on Principals");
    nullTestCollection(methRemove, mtSubj.getPrincipals(), null);
    nullTestCollection(methRemove, mtSubj.getPrincipals(), remPrincs);

    System.out.println(
            "* Testing removeAll w/ null values on Public Creds");
    nullTestCollection(methRemove, mtSubj.getPublicCredentials(), null);
    nullTestCollection(methRemove, mtSubj.getPublicCredentials(),
            remPubVals);

    System.out.println(
            "* Testing removeAll w/ null values on Private Creds");
    nullTestCollection(methRemove, mtSubj.getPrivateCredentials(), null);
    nullTestCollection(methRemove, mtSubj.getPrivateCredentials(),
            remPrvVals);
}
 
Example 7
Source File: SubjectNullTests.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void testRetainAll() {
    // Create a well formed subject and additional collections
    Subject mtSubj = makeSubj(false, false, false);
    Set<Principal> remPrincs = new HashSet<>(Arrays.asList(tmplAddPrincs));
    Set<Object> remPubVals = new HashSet<>(Arrays.asList(tmplAddPubVals));
    Set<Object> remPrvVals = new HashSet<>(Arrays.asList(tmplAddPrvVals));

    // Add in values that exist within the Subject
    remPrincs.add(princVals[2]);
    remPubVals.add(pubVals[2]);
    remPrvVals.add(privVals[2]);

    // Run one success test for each Subject family to verify the
    // overloaded method works as intended.
    Set<Principal> setPrin = mtSubj.getPrincipals();
    Set<Object> setPubCreds = mtSubj.getPublicCredentials();
    Set<Object> setPrvCreds = mtSubj.getPrivateCredentials();
    int prinOrigSize = setPrin.size();
    int pubOrigSize = setPubCreds.size();
    int prvOrigSize = setPrvCreds.size();

    System.out.println("------ retainAll() -----");

    // Retain the specified members (those that exist in the Subject)
    // and validate the results.
    if (validTestCollection(methRetain, setPrin, remPrincs) == false ||
        setPrin.size() != 1 || setPrin.contains(princVals[2]) == false)
    {
        throw new RuntimeException("Failed retainAll() on principals");
    }

    if (validTestCollection(methRetain, setPubCreds,
            remPubVals) == false ||
        setPubCreds.size() != 1 ||
        setPubCreds.contains(pubVals[2]) == false)
    {
        throw new RuntimeException("Failed retainAll() on public creds");
    }
    if (validTestCollection(methRetain, setPrvCreds,
            remPrvVals) == false ||
        setPrvCreds.size() != 1 ||
        setPrvCreds.contains(privVals[2]) == false)
    {
        throw new RuntimeException("Failed retainAll() on private creds");
    }
    System.out.println("Positive retainAll() test passed");

    // Now add null elements into each container, then retest
    remPrincs.add(null);
    remPubVals.add(null);
    remPrvVals.add(null);

    System.out.println("* Testing retainAll w/ null values on Principals");
    nullTestCollection(methRetain, mtSubj.getPrincipals(), null);
    nullTestCollection(methRetain, mtSubj.getPrincipals(), remPrincs);

    System.out.println(
            "* Testing retainAll w/ null values on Public Creds");
    nullTestCollection(methRetain, mtSubj.getPublicCredentials(), null);
    nullTestCollection(methRetain, mtSubj.getPublicCredentials(),
            remPubVals);

    System.out.println(
            "* Testing retainAll w/ null values on Private Creds");
    nullTestCollection(methRetain, mtSubj.getPrivateCredentials(), null);
    nullTestCollection(methRetain, mtSubj.getPrivateCredentials(),
            remPrvVals);
}
 
Example 8
Source File: SubjectTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private Object[] getSerializationData() {
    Subject subject = new Subject();
    return new Object[] { subject, subject.getPrincipals(),
            subject.getPrivateCredentials(), subject.getPublicCredentials() };
}