Search in sources :

Example 1 with AuthPermission

use of org.apache.harmony.javax.security.auth.AuthPermission in project AsmackService by rtreffer.

the class LoginContext method init.

// Does all the machinery needed for the initialization.
private void init(String name, Subject subject, final CallbackHandler cbHandler, Configuration config) throws LoginException {
    userProvidedSubject = (this.subject = subject) != null;
    //
    if (name == null) {
        //$NON-NLS-1$
        throw new LoginException("auth.00");
    }
    if (config == null) {
        config = Configuration.getAccessibleConfiguration();
    } else {
        userProvidedConfig = true;
    }
    SecurityManager sm = System.getSecurityManager();
    if (sm != null && !userProvidedConfig) {
        //$NON-NLS-1$
        sm.checkPermission(new AuthPermission("createLoginContext." + name));
    }
    AppConfigurationEntry[] entries = config.getAppConfigurationEntry(name);
    if (entries == null) {
        if (sm != null && !userProvidedConfig) {
            //$NON-NLS-1$
            sm.checkPermission(new AuthPermission("createLoginContext.other"));
        }
        //$NON-NLS-1$
        entries = config.getAppConfigurationEntry("other");
        if (entries == null) {
            //$NON-NLS-1$
            throw new LoginException("auth.35 " + name);
        }
    }
    modules = new Module[entries.length];
    for (int i = 0; i < modules.length; i++) {
        modules[i] = new Module(entries[i]);
    }
    /*
         * as some of the operations to be executed (i.e. get*ClassLoader,
         * getProperty, class loading) are security-checked, then combine all of
         * them into a single doPrivileged() call.
         */
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {

            public Void run() throws Exception {
                // First, set the 'contextClassLoader'
                contextClassLoader = Thread.currentThread().getContextClassLoader();
                if (contextClassLoader == null) {
                    contextClassLoader = ClassLoader.getSystemClassLoader();
                }
                // then, checks whether the cbHandler is set
                if (cbHandler == null) {
                    // well, let's try to find it
                    String klassName = Security.getProperty(DEFAULT_CALLBACK_HANDLER_PROPERTY);
                    if (klassName == null || klassName.length() == 0) {
                        return null;
                    }
                    Class<?> klass = Class.forName(klassName, true, contextClassLoader);
                    callbackHandler = (CallbackHandler) klass.newInstance();
                } else {
                    callbackHandler = cbHandler;
                }
                return null;
            }
        });
    } catch (PrivilegedActionException ex) {
        Throwable cause = ex.getCause();
        //$NON-NLS-1$
        throw (LoginException) new LoginException("auth.36").initCause(cause);
    }
    if (userProvidedConfig) {
        userContext = AccessController.getContext();
    } else if (callbackHandler != null) {
        userContext = AccessController.getContext();
        callbackHandler = new ContextedCallbackHandler(callbackHandler);
    }
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) PrivilegedActionException(java.security.PrivilegedActionException) AuthPermission(org.apache.harmony.javax.security.auth.AuthPermission) PrivilegedActionException(java.security.PrivilegedActionException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) LoginModule(org.apache.harmony.javax.security.auth.spi.LoginModule)

Aggregations

IOException (java.io.IOException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 AuthPermission (org.apache.harmony.javax.security.auth.AuthPermission)1 LoginModule (org.apache.harmony.javax.security.auth.spi.LoginModule)1