SubjectDomainCombiner.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.auth;
  18. import java.security.DomainCombiner;
  19. import java.security.Principal;
  20. import java.security.ProtectionDomain;
  21. import java.util.Set;
  22. /**
  23. * Merges permissions based on code source and code signers with permissions
  24. * granted to the specified {@link Subject}.
  25. */
  26. public class SubjectDomainCombiner implements DomainCombiner {
  27. // subject to be associated
  28. private Subject subject;
  29. // permission required to get a subject object
  30. private static final AuthPermission _GET = new AuthPermission(
  31. "getSubjectFromDomainCombiner"); //$NON-NLS-1$
  32. /**
  33. * Creates a domain combiner for the entity provided in {@code subject}.
  34. *
  35. * @param subject
  36. * the entity to which this domain combiner is associated.
  37. */
  38. public SubjectDomainCombiner(Subject subject) {
  39. super();
  40. if (subject == null) {
  41. throw new NullPointerException();
  42. }
  43. this.subject = subject;
  44. }
  45. /**
  46. * Returns the entity to which this domain combiner is associated.
  47. *
  48. * @return the entity to which this domain combiner is associated.
  49. */
  50. public Subject getSubject() {
  51. SecurityManager sm = System.getSecurityManager();
  52. if (sm != null) {
  53. sm.checkPermission(_GET);
  54. }
  55. return subject;
  56. }
  57. /**
  58. * Merges the {@code ProtectionDomain} with the {@code Principal}s
  59. * associated with the subject of this {@code SubjectDomainCombiner}.
  60. *
  61. * @param currentDomains
  62. * the {@code ProtectionDomain}s associated with the context of
  63. * the current thread. The domains must be sorted according to
  64. * the execution order, the most recent residing at the
  65. * beginning.
  66. * @param assignedDomains
  67. * the {@code ProtectionDomain}s from the parent thread based on
  68. * code source and signers.
  69. * @return a single {@code ProtectionDomain} array computed from the two
  70. * provided arrays, or {@code null}.
  71. * @see ProtectionDomain
  72. */
  73. public ProtectionDomain[] combine(ProtectionDomain[] currentDomains,
  74. ProtectionDomain[] assignedDomains) {
  75. // get array length for combining protection domains
  76. int len = 0;
  77. if (currentDomains != null) {
  78. len += currentDomains.length;
  79. }
  80. if (assignedDomains != null) {
  81. len += assignedDomains.length;
  82. }
  83. if (len == 0) {
  84. return null;
  85. }
  86. ProtectionDomain[] pd = new ProtectionDomain[len];
  87. // for each current domain substitute set of principal with subject's
  88. int cur = 0;
  89. if (currentDomains != null) {
  90. Set<Principal> s = subject.getPrincipals();
  91. Principal[] p = s.toArray(new Principal[s.size()]);
  92. for (cur = 0; cur < currentDomains.length; cur++) {
  93. ProtectionDomain newPD;
  94. newPD = new ProtectionDomain(currentDomains[cur].getCodeSource(),
  95. currentDomains[cur].getPermissions(), currentDomains[cur]
  96. .getClassLoader(), p);
  97. pd[cur] = newPD;
  98. }
  99. }
  100. // copy assigned domains
  101. if (assignedDomains != null) {
  102. System.arraycopy(assignedDomains, 0, pd, cur, assignedDomains.length);
  103. }
  104. return pd;
  105. }
  106. }