DelegationPermission.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.kerberos;
  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.security.BasicPermission;
  23. import java.security.Permission;
  24. import java.security.PermissionCollection;
  25. import org.apache.harmony.auth.internal.nls.Messages;
  26. public final class DelegationPermission extends BasicPermission implements Serializable {
  27. private static final long serialVersionUID = 883133252142523922L;
  28. // initialization of a target name
  29. private static String init(String name) {
  30. String trName = name.trim();
  31. int length = trName.length();
  32. // length MUST be at least 7 characters
  33. if (length < 7) {
  34. throw new IllegalArgumentException(Messages.getString("auth.20")); //$NON-NLS-1$
  35. }
  36. int index = name.indexOf('"', 2);
  37. if (trName.charAt(0) != '"' || index == -1 || (index + 6) > trName.length()
  38. || trName.charAt(index + 1) != ' ' || trName.charAt(index + 2) != '"'
  39. || trName.charAt(trName.length() - 1) != '"') {
  40. throw new IllegalArgumentException(Messages.getString("auth.20")); //$NON-NLS-1$
  41. }
  42. return trName;
  43. }
  44. public DelegationPermission(String principals) {
  45. super(init(principals));
  46. }
  47. public DelegationPermission(String principals, String action) {
  48. super(init(principals), action);
  49. }
  50. @Override
  51. public boolean equals(Object obj) {
  52. if (obj == this) {
  53. return true;
  54. }
  55. if (obj == null || obj.getClass() != this.getClass()) {
  56. return false;
  57. }
  58. return this.getName().equals(((DelegationPermission) obj).getName());
  59. }
  60. @Override
  61. public boolean implies(Permission permission) {
  62. return equals(permission);
  63. }
  64. @Override
  65. public int hashCode() {
  66. return getName().hashCode();
  67. }
  68. @Override
  69. public PermissionCollection newPermissionCollection() {
  70. return new KrbDelegationPermissionCollection();
  71. }
  72. private void writeObject(ObjectOutputStream s) throws IOException, ClassNotFoundException {
  73. s.defaultWriteObject();
  74. }
  75. private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
  76. s.defaultReadObject();
  77. init(getName());
  78. }
  79. }