Search in sources :

Example 1 with ReplyMessageAttribute

use of org.forgerock.openam.radius.common.ReplyMessageAttribute in project OpenAM by OpenRock.

the class ConsoleClient method run.

/**
     * Calls the server in a thread.
     */
@Override
public void run() {
    try {
        final DatagramChannel chan = DatagramChannel.open();
        // request id
        short reqId = 1;
        final SecureRandom random = new SecureRandom();
        final InetSocketAddress serverAddr = new InetSocketAddress(this.host, this.port);
        final NASIPAddressAttribute nasAddr = new NASIPAddressAttribute(InetAddress.getLocalHost());
        final NASPortAttribute nasPort = new NASPortAttribute(chan.socket().getLocalPort());
        StateAttribute state = null;
        // String username = "boydmr"; // TODO: restore
        final String username = getUserInputFor("Username", null);
        // String passwordOrAnswer = "password"; // TODO: restore
        String passwordOrAnswer = getUserInputFor("Password", null);
        System.out.println();
        boolean finished = false;
        // ready for writing
        final ByteBuffer bufIn = ByteBuffer.allocate(4096);
        while (!finished) {
            final RequestAuthenticator reqAuthR = new RequestAuthenticator(random, this.secret);
            final AccessRequest req = new AccessRequest(reqId++, reqAuthR);
            req.addAttribute(new UserNameAttribute(username));
            req.addAttribute(new UserPasswordAttribute(req.getAuthenticator(), this.secret, passwordOrAnswer));
            req.addAttribute(nasAddr);
            req.addAttribute(nasPort);
            if (state != null) {
                req.addAttribute(state);
            }
            final ByteBuffer reqBuf = ByteBuffer.wrap(req.getOctets());
            if (logTraffic) {
                System.out.println("Packet To " + host + ":" + port);
                System.out.println(RadiusRequestContext.getPacketRepresentation(req));
            }
            chan.send(reqBuf, serverAddr);
            // now handle responses possibly sending additional requests
            chan.receive(bufIn);
            // prepare buffer for reading out
            bufIn.flip();
            final Packet res = PacketFactory.toPacket(bufIn);
            // prepare buffer for next response
            bufIn.clear();
            if (logTraffic) {
                System.out.println("Packet From " + host + ":" + port);
                System.out.println(RadiusRequestContext.getPacketRepresentation(res));
            }
            if (res instanceof AccessReject) {
                System.out.println("---> Sorry. Not Authenticated.");
                System.out.println();
                finished = true;
            } else if (res instanceof AccessAccept) {
                System.out.println("---> SUCCESS! You've Authenticated!");
                System.out.println();
                finished = true;
            } else if (res instanceof AccessChallenge) {
                final AccessChallenge chng = (AccessChallenge) res;
                state = (StateAttribute) getAttribute(StateAttribute.class, res);
                final ReplyMessageAttribute msg = (ReplyMessageAttribute) getAttribute(ReplyMessageAttribute.class, res);
                String message = null;
                if (msg != null) {
                    message = msg.getMessage();
                }
                passwordOrAnswer = getUserInputFor("Answer", message);
                System.out.println();
            }
        }
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Also used : StateAttribute(org.forgerock.openam.radius.common.StateAttribute) Packet(org.forgerock.openam.radius.common.Packet) RequestAuthenticator(org.forgerock.openam.radius.common.RequestAuthenticator) AccessRequest(org.forgerock.openam.radius.common.AccessRequest) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) SecureRandom(java.security.SecureRandom) NASIPAddressAttribute(org.forgerock.openam.radius.common.packet.NASIPAddressAttribute) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) ReplyMessageAttribute(org.forgerock.openam.radius.common.ReplyMessageAttribute) UserNameAttribute(org.forgerock.openam.radius.common.UserNameAttribute) AccessReject(org.forgerock.openam.radius.common.AccessReject) NASPortAttribute(org.forgerock.openam.radius.common.packet.NASPortAttribute) AccessChallenge(org.forgerock.openam.radius.common.AccessChallenge) UserPasswordAttribute(org.forgerock.openam.radius.common.UserPasswordAttribute) AccessAccept(org.forgerock.openam.radius.common.AccessAccept)

Example 2 with ReplyMessageAttribute

use of org.forgerock.openam.radius.common.ReplyMessageAttribute in project OpenAM by OpenRock.

the class OpenAMAuthHandler method getNextCallbackReplyMsg.

/**
     * Generates reply message for the current callback to be embedded in a challenge response to gather an answer for
     * that callback. If an unknown/unexpected callback type is incurred the process is terminated with a reject
     * response.
     *
     * @param respHandler
     * @param holder
     * @return
     */
private ReplyMessageAttribute getNextCallbackReplyMsg(RadiusResponse response, ContextHolder holder) {
    LOG.message("Entering getNextCallbackReplyMsg()");
    ReplyMessageAttribute msg = null;
    final Callback[] callbacks = holder.getCallbacks();
    if (callbacks == null) {
        return null;
    }
    final Callback cb = callbacks[holder.getIdxOfCurrentCallback()];
    String header = "";
    final PagePropertiesCallback pagePropCallback = holder.getCallbackSetProps();
    if (pagePropCallback != null && !"".equals(pagePropCallback.getHeader())) {
        header = pagePropCallback.getHeader() + " ";
    }
    if (cb instanceof NameCallback) {
        LOG.message("getNextCallbackReplyMsg(); - processing NameCallback.");
        msg = new ReplyMessageAttribute(header + ((NameCallback) cb).getPrompt());
    } else if (cb instanceof PasswordCallback) {
        LOG.message("getNextCallbackReplyMsg(); - processing PasswordCallback.");
        msg = new ReplyMessageAttribute(header + ((PasswordCallback) cb).getPrompt());
    } else if (cb instanceof ChoiceCallback) {
        LOG.message("getNextCallbackReplyMsg(); - processing ChoiceCallback.");
        final ChoiceCallback cc = (ChoiceCallback) cb;
        final StringBuilder sb = new StringBuilder();
        sb.append(header);
        sb.append(cc.getPrompt());
        if (cc.allowMultipleSelections()) {
            // ugh. we'll have to figure out how to translate this suitably in view of sentence structure for
            // a given locale.
            // TODO: LOCALIZE
            sb.append(" (Separate Selected Numbers by Spaces");
            if (cc.getDefaultChoice() >= 0) {
                sb.append(". Default is " + cc.getDefaultChoice());
            }
            sb.append(".)");
        }
        sb.append('\n');
        final String[] choices = cc.getChoices();
        for (int j = 0; j < choices.length; j++) {
            final String choice = choices[j];
            if (j != 0) {
                sb.append(",\n");
            }
            sb.append(j);
            sb.append(" = ");
            sb.append(choice);
        }
        msg = new ReplyMessageAttribute(sb.toString());
    } else if (cb instanceof ConfirmationCallback) {
        LOG.message("getNextCallbackReplyMsg(); - processing ConformationCallback.");
        final ConfirmationCallback cc = (ConfirmationCallback) cb;
        final StringBuilder sb = new StringBuilder();
        sb.append(header);
        sb.append(cc.getPrompt());
        if (cc.getDefaultOption() >= 0) {
            // ugh. ditto on above translation concern
            sb.append(" (Default is ");
            sb.append(cc.getDefaultOption());
            sb.append(".)");
        }
        sb.append('\n');
        final String[] options = cc.getOptions();
        for (int j = 0; j < options.length; j++) {
            final String option = options[j];
            if (j != 0) {
                sb.append(",\n");
            }
            sb.append(j);
            sb.append(" = ");
            sb.append(option);
        }
        msg = new ReplyMessageAttribute(sb.toString());
    } else {
        // unknown and unexpected type
        LOG.error("Radius can not support " + cb.getClass().getSimpleName() + " used by module " + holder.getChainModuleIndex() + " with name " + holder.getModuleName() + " in chain '" + this.authChain + "'. Denying Access.");
        rejectAccessAndTerminateProcess(response, holder);
    }
    LOG.message("Entering getNextCallbackReplyMsg() returning '" + msg + "'");
    return msg;
}
Also used : ChoiceCallback(javax.security.auth.callback.ChoiceCallback) ReplyMessageAttribute(org.forgerock.openam.radius.common.ReplyMessageAttribute) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) HttpCallback(com.sun.identity.authentication.spi.HttpCallback) RedirectCallback(com.sun.identity.authentication.spi.RedirectCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 3 with ReplyMessageAttribute

use of org.forgerock.openam.radius.common.ReplyMessageAttribute in project OpenAM by OpenRock.

the class OpenAMAuthHandler method gatherUserInput.

private void gatherUserInput(RadiusResponse response, ContextHolder holder, String answer, StateAttribute state) {
    LOG.message("Entering gatherUserInput();");
    // a challenge response, get the next set loaded, and then start sending a challenges for that set.
    while (holder.getAuthPhase() == ContextHolder.AuthPhase.GATHERING_INPUT) {
        if (holder.getCallbacks() == null) {
            LOG.message("--- callbacks == null in gatherUserInput");
            // either just starting process or just finished submitting a set of callback input values
            if (!isNextCallbackSetAvailable(response, holder)) {
                // no further input from user needed or error occurred
                if (holder.getAuthPhase() == ContextHolder.AuthPhase.TERMINATED) {
                    return;
                }
                LOG.message("--- NextCallbackSet not-available in gatherUserInput - move to finalization");
                holder.setAuthPhase(ContextHolder.AuthPhase.FINALIZING);
                return;
            }
        } else {
            LOG.warning("--- callbacks[" + holder.getCallbacks().length + "] in gatherUserInput - ");
            // we are gathering for current set.
            // answers
            final boolean injected = injectAnswerForCallback(response, holder, answer);
            if (!injected) {
                // couldn't inject and already sent reject response so exit out
                return;
            }
        }
        // new callbacks available or still gathering input for the current set. if all callbacks have values
        // then submit and loop around again to get next set else send challenge response to gather input for the
        // next callback
        final Callback[] callbacks = holder.getCallbacks();
        if (holder.getIdxOfCurrentCallback() > callbacks.length - 1) {
            LOG.warning("--- holder.idxOfCurrentCallback " + holder.getIdxOfCurrentCallback() + " > holder.callbacks.length-1 " + (holder.getCallbacks().length - 1) + " in gatherUserInput - submitting/set callbacks=null");
            try {
                holder.getAuthContext().submitRequirements(callbacks);
            } catch (final Throwable t) {
                LOG.error("Exception thrown while submitting callbacks. Rejecting access.", t);
                rejectAccessAndTerminateProcess(response, holder);
                return;
            }
            holder.setCallbacks(null);
        } else {
            final ReplyMessageAttribute msg = getNextCallbackReplyMsg(response, holder);
            if (msg == null) {
                // failed to inject and already sent a reject msg so stop processing at this point.
                return;
            }
            // if we get here then we have a challenge response message ready to send
            final AccessChallenge challenge = new AccessChallenge();
            if (state == null) {
                // as when starting authentication
                state = new StateAttribute(holder.getCacheKey());
            }
            challenge.addAttribute(state);
            challenge.addAttribute(msg);
            response.setResponsePacket(challenge);
            // exit out and await response to challenge response
            return;
        }
    }
}
Also used : StateAttribute(org.forgerock.openam.radius.common.StateAttribute) ReplyMessageAttribute(org.forgerock.openam.radius.common.ReplyMessageAttribute) PagePropertiesCallback(com.sun.identity.authentication.spi.PagePropertiesCallback) HttpCallback(com.sun.identity.authentication.spi.HttpCallback) RedirectCallback(com.sun.identity.authentication.spi.RedirectCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) ChoiceCallback(javax.security.auth.callback.ChoiceCallback) NameCallback(javax.security.auth.callback.NameCallback) ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) Callback(javax.security.auth.callback.Callback) AccessChallenge(org.forgerock.openam.radius.common.AccessChallenge)

Aggregations

ReplyMessageAttribute (org.forgerock.openam.radius.common.ReplyMessageAttribute)3 HttpCallback (com.sun.identity.authentication.spi.HttpCallback)2 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)2 RedirectCallback (com.sun.identity.authentication.spi.RedirectCallback)2 Callback (javax.security.auth.callback.Callback)2 ChoiceCallback (javax.security.auth.callback.ChoiceCallback)2 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)2 NameCallback (javax.security.auth.callback.NameCallback)2 PasswordCallback (javax.security.auth.callback.PasswordCallback)2 AccessChallenge (org.forgerock.openam.radius.common.AccessChallenge)2 StateAttribute (org.forgerock.openam.radius.common.StateAttribute)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 DatagramChannel (java.nio.channels.DatagramChannel)1 SecureRandom (java.security.SecureRandom)1 AccessAccept (org.forgerock.openam.radius.common.AccessAccept)1 AccessReject (org.forgerock.openam.radius.common.AccessReject)1 AccessRequest (org.forgerock.openam.radius.common.AccessRequest)1 Packet (org.forgerock.openam.radius.common.Packet)1