1 7 package org.jboss.security; 8 9 import java.security.AccessController ; 10 import java.security.AllPermission ; 11 import java.security.CodeSource ; 12 import java.security.KeyStore ; 13 import java.security.PermissionCollection ; 14 import java.security.Permissions ; 15 import javax.security.auth.Subject ; 16 import javax.security.auth.login.AppConfigurationEntry ; 17 18 23 public class AppPolicy 24 { 25 26 public static final PermissionCollection NO_PERMISSIONS = new Permissions (); 27 28 private static PermissionCollection ALL_PERMISSIONS; 29 30 33 private static AppPolicy defaultAppPolicy; 34 35 static 37 { 38 AllPermission all = new AllPermission (); 40 ALL_PERMISSIONS = all.newPermissionCollection(); 41 ALL_PERMISSIONS.add(all); 42 defaultAppPolicy = new AppPolicy("other"); 44 } 45 46 private String appName; 47 private KeyStore keyStore; 48 49 52 private AuthorizationInfo permissionInfo; 53 54 57 private AuthenticationInfo loginInfo; 58 59 public KeyStore getKeyStore() 60 { 61 return keyStore; 62 } 63 public void setKeyStore(KeyStore keyStore) 64 { 65 this.keyStore = keyStore; 66 } 67 68 public static void setDefaultAppPolicy(AppPolicy policy) 69 { 70 if( policy == null ) 71 throw new IllegalArgumentException ("The policy argument cannot be null"); 72 defaultAppPolicy = policy; 73 } 74 public static AppPolicy getDefaultAppPolicy() 75 { 76 return defaultAppPolicy; 77 } 78 79 public AppPolicy(String appName) 80 { 81 this.appName = appName; 82 } 83 84 public AuthenticationInfo getLoginInfo() 85 { 86 AccessController.checkPermission(AuthenticationInfo.GET_CONFIG_ENTRY_PERM); 87 return loginInfo; 88 } 89 public void setLoginInfo(AuthenticationInfo loginInfo) 90 { 91 AccessController.checkPermission(AuthenticationInfo.SET_CONFIG_ENTRY_PERM); 92 this.loginInfo = loginInfo; 93 } 94 public AuthorizationInfo getPermissionInfo() 95 { 96 return permissionInfo; 97 } 98 public void setPermissionInfo(AuthorizationInfo permissionInfo) 99 { 100 this.permissionInfo = permissionInfo; 101 } 102 103 public AppConfigurationEntry [] getAppConfigurationEntry() 104 { 105 AppConfigurationEntry [] appConfig = null; 106 if( loginInfo != null ) 107 appConfig = loginInfo.getAppConfigurationEntry(); 108 if( appConfig == null && this != defaultAppPolicy ) 109 appConfig = defaultAppPolicy.getAppConfigurationEntry(); 110 AppConfigurationEntry [] copy = null; 111 if( appConfig != null ) 112 { 113 copy = new AppConfigurationEntry [appConfig.length]; 114 for(int c = 0; c < copy.length; c ++) 115 { 116 AppConfigurationEntry e0 = appConfig[c]; 117 AppConfigurationEntry e1 = new AppConfigurationEntry ( 118 e0.getLoginModuleName(), 119 e0.getControlFlag(), 120 e0.getOptions() 121 ); 122 copy[c] = e1; 123 } 124 } 125 return copy; 126 } 127 public PermissionCollection getPermissions(Subject subject, CodeSource codesource) 128 { 129 PermissionCollection perms = NO_PERMISSIONS; 130 AuthorizationInfo info = getPermissionInfo(); 131 if( info == null ) 132 info = defaultAppPolicy.getPermissionInfo(); 133 if( info != null ) 134 { 135 perms = info.getPermissions(subject, codesource); 136 } 137 138 return perms; 139 } 140 141 public String toString() 142 { 143 StringBuffer buffer = new StringBuffer (appName); 144 buffer.append('\n'); 145 buffer.append("AuthenticationInfo:\n"); 146 if( loginInfo != null ) 147 buffer.append(loginInfo); 148 buffer.append("AuthorizationInfo:\n"); 149 if( permissionInfo != null ) 150 buffer.append(permissionInfo); 151 return buffer.toString(); 152 } 153 154 } 155 | Popular Tags |