Search in sources :

Example 1 with BASE64Decoder

use of com.sun.messaging.jmq.util.BASE64Decoder in project openmq by eclipse-ee4j.

the class JSONWebSocket method processData.

@Override
protected void processData(String text) throws Exception {
    if (DEBUG) {
        logger.log(logger.INFO, toString() + ".processData(text=" + text + ")");
    }
    try {
        JsonReader jsonReader = Json.createReader(new StringReader(text));
        JsonObject jo = jsonReader.readObject();
        String command = jo.getString(JsonMessage.Key.COMMAND);
        JsonObject headers = jo.getJsonObject(JsonMessage.Key.HEADERS);
        JsonObject body = jo.getJsonObject(JsonMessage.Key.BODY);
        StompFrameMessage frame = StompFrameMessageImpl.getFactory().newStompFrameMessage(StompFrameMessage.Command.valueOf(command), logger);
        Iterator<String> itr = headers.keySet().iterator();
        String key;
        String val;
        while (itr.hasNext()) {
            key = itr.next();
            val = headers.getString(key);
            if (val != null) {
                frame.addHeader(key, val);
            }
        }
        if (body != null) {
            JsonString btype = body.getJsonString(JsonMessage.BodySubKey.TYPE);
            if (btype == null || btype.getString().equals(JsonMessage.BODY_TYPE_TEXT)) {
                JsonString msg = body.getJsonString(JsonMessage.BodySubKey.TEXT);
                if (msg != null) {
                    frame.setBody(msg.getString().getBytes("UTF-8"));
                }
            } else if (btype.getString().equals(JsonMessage.BODY_TYPE_BYTES)) {
                JsonString enc = body.getJsonString("encoder");
                if (enc == null || enc.getString().equals(JsonMessage.ENCODER_BASE64)) {
                    JsonString msg = body.getJsonString(JsonMessage.BodySubKey.TEXT);
                    if (msg != null) {
                        byte[] bytes = null;
                        if (base64Class == null) {
                            BASE64Decoder decoder = new BASE64Decoder();
                            bytes = decoder.decodeBuffer(msg.getString());
                        } else {
                            Method gm = base64Class.getMethod("getDecoder", (new Class[] {}));
                            Object o = gm.invoke(null);
                            Method dm = o.getClass().getMethod("decode", (new Class[] { String.class }));
                            bytes = (byte[]) dm.invoke(o, msg.getString());
                        }
                        frame.setBody(bytes);
                        frame.addHeader(StompFrameMessage.CommonHeader.CONTENTLENGTH, String.valueOf(bytes.length));
                    }
                } else {
                    throw new IOException("encoder " + enc + " not supported");
                }
            } else {
                throw new IOException("body type:" + btype + " not supported");
            }
        }
        dispatchMessage((StompFrameMessageImpl) frame);
    } catch (Exception e) {
        logger.logStack(logger.ERROR, e.getMessage(), e);
        sendFatalError(e);
    }
}
Also used : StompFrameMessage(com.sun.messaging.bridge.api.StompFrameMessage) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) JsonObject(jakarta.json.JsonObject) JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) JsonString(jakarta.json.JsonString) Method(java.lang.reflect.Method) IOException(java.io.IOException) BASE64Decoder(com.sun.messaging.jmq.util.BASE64Decoder) IOException(java.io.IOException)

Example 2 with BASE64Decoder

use of com.sun.messaging.jmq.util.BASE64Decoder in project openmq by eclipse-ee4j.

the class JMQBasicAuthenticationHandler method handleResponse.

/**
 * @param authResponse the authentication response data. This is the AUTHENCATE_RESPONSE packet body.
 * @param sequence packet sequence number
 *
 * @return next request data if any; null if no more request. The request data will be sent as packet body in
 * AUTHENTICATE_REQUEST
 */
@Override
public synchronized byte[] handleResponse(byte[] authResponse, int sequence) throws LoginException {
    if (repository == null && logout) {
        throw new LoginException(Globals.getBrokerResources().getKString(BrokerResources.X_CONNECTION_LOGGEDOUT));
    }
    if (repository != null) {
        repository.close();
    }
    Subject subject = null;
    acc = null;
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(authResponse);
        DataInputStream dis = new DataInputStream(bis);
        String username = dis.readUTF();
        BASE64Decoder decoder = new BASE64Decoder();
        String pass = dis.readUTF();
        String password = new String(decoder.decodeBuffer(pass), "UTF8");
        dis.close();
        String rep = authProps.getProperty(AccessController.PROP_AUTHENTICATION_PREFIX + getType() + AccessController.PROP_USER_REPOSITORY_SUFFIX);
        if (rep == null || rep.trim().equals("")) {
            throw new LoginException(Globals.getBrokerResources().getKString(BrokerResources.X_USER_REPOSITORY_NOT_DEFINED, getType()));
        }
        String className = authProps.getProperty(AccessController.PROP_USER_REPOSITORY_PREFIX + rep + ".class");
        if (className == null) {
            throw new LoginException(Globals.getBrokerResources().getKString(BrokerResources.X_USER_REPOSITORY_CLASS_NOT_DEFINED, rep, getType()));
        }
        repository = (UserRepository) Class.forName(className).getDeclaredConstructor().newInstance();
        repository.open(getType(), authProps, cacheData);
        subject = repository.findMatch(username, password, null, getMatchType());
        cacheData = repository.getCacheData();
        if (subject == null) {
            FailedLoginException ex = new FailedLoginException(Globals.getBrokerResources().getKString(BrokerResources.X_FORBIDDEN, username));
            ex.setUser(username);
            throw ex;
        }
        acc = new JMQAccessControlContext(new MQUser(username), subject, authProps);
        return null;
    } catch (ClassNotFoundException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "ClassNotFoundException: " + e.getMessage()));
    } catch (IOException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "IOException: " + e.getMessage()));
    } catch (InstantiationException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "InstantiationException: " + e.getMessage()));
    } catch (IllegalAccessException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "IllegalAccessException: " + e.getMessage()));
    } catch (ClassCastException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "ClassCastException: " + e.getMessage()));
    } catch (NoSuchMethodException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "NoSuchMethodException: " + e.getMessage()));
    } catch (InvocationTargetException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "InvocationTargetException: " + e.getMessage()));
    }
}
Also used : MQUser(com.sun.messaging.jmq.auth.jaas.MQUser) Subject(javax.security.auth.Subject) InvocationTargetException(java.lang.reflect.InvocationTargetException) FailedLoginException(com.sun.messaging.jmq.auth.api.FailedLoginException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(com.sun.messaging.jmq.auth.api.FailedLoginException) BASE64Decoder(com.sun.messaging.jmq.util.BASE64Decoder)

Example 3 with BASE64Decoder

use of com.sun.messaging.jmq.util.BASE64Decoder in project openmq by eclipse-ee4j.

the class JMQAdminKeyAuthenticationHandler method handleResponse.

/**
 * @param authResponse the authentication response data. This is the AUTHENCATE_RESPONSE packet body.
 * @param sequence packet sequence number
 *
 * @return next request data if any; null if no more request. The request data will be sent as packet body in
 * AUTHENTICATE_REQUEST
 */
@Override
public byte[] handleResponse(byte[] authResponse, int sequence) throws LoginException {
    Subject subject = null;
    acc = null;
    if (authProps == null) {
        throw new LoginException(Globals.getBrokerResources().getKString(BrokerResources.X_ILLEGAL_AUTHSTATE, getType()));
    }
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(authResponse);
        DataInputStream dis = new DataInputStream(bis);
        String username = dis.readUTF();
        BASE64Decoder decoder = new BASE64Decoder();
        String pass = dis.readUTF();
        String password = new String(decoder.decodeBuffer(pass), "UTF8");
        dis.close();
        String adminkey = authProps.getProperty(AccessController.PROP_ADMINKEY);
        if (DEBUG) {
            logger.log(Logger.DEBUG, AccessController.PROP_ADMINKEY + ":" + adminkey + ":" + " password:" + password + ":");
        }
        if (adminkey != null) {
            if (username.equals(ADMINKEYNAME) && password.equals(adminkey)) {
                final String tempUserName = username;
                subject = (Subject) java.security.AccessController.doPrivileged(new PrivilegedAction<Object>() {

                    @Override
                    public Object run() {
                        Subject tempSubject = new Subject();
                        tempSubject.getPrincipals().add(new MQUser(tempUserName));
                        tempSubject.getPrincipals().add(new MQAdminGroup(ADMINKEYNAME));
                        return tempSubject;
                    }
                });
                /*
                     * // subject = new Subject(); // subject.getPrincipals().add(new MQUser(username)); // subject.getPrincipals().add(new
                     * MQAdminGroup(ADMINKEYNAME));
                     */
                acc = new JMQAccessControlContext(new MQUser(username), subject, authProps);
                return null;
            }
            FailedLoginException ex = new FailedLoginException(Globals.getBrokerResources().getKString(BrokerResources.X_FORBIDDEN, username));
            ex.setUser(username);
            throw ex;
        }
        throw new LoginException(Globals.getBrokerResources().getKString(BrokerResources.X_ADMINKEY_NOT_EXIST));
    } catch (IOException e) {
        throw new LoginException(Globals.getBrokerResources().getString(BrokerResources.X_INTERNAL_EXCEPTION, "IOException: " + e.getMessage()));
    }
}
Also used : MQUser(com.sun.messaging.jmq.auth.jaas.MQUser) Subject(javax.security.auth.Subject) FailedLoginException(com.sun.messaging.jmq.auth.api.FailedLoginException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(com.sun.messaging.jmq.auth.api.FailedLoginException) MQAdminGroup(com.sun.messaging.jmq.auth.jaas.MQAdminGroup) BASE64Decoder(com.sun.messaging.jmq.util.BASE64Decoder)

Aggregations

BASE64Decoder (com.sun.messaging.jmq.util.BASE64Decoder)3 FailedLoginException (com.sun.messaging.jmq.auth.api.FailedLoginException)2 MQUser (com.sun.messaging.jmq.auth.jaas.MQUser)2 Subject (javax.security.auth.Subject)2 LoginException (javax.security.auth.login.LoginException)2 StompFrameMessage (com.sun.messaging.bridge.api.StompFrameMessage)1 MQAdminGroup (com.sun.messaging.jmq.auth.jaas.MQAdminGroup)1 JsonObject (jakarta.json.JsonObject)1 JsonReader (jakarta.json.JsonReader)1 JsonString (jakarta.json.JsonString)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1