Search in sources :

Example 6 with AccessRequest

use of org.forgerock.openam.radius.common.AccessRequest 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 7 with AccessRequest

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

the class RadiusRequestTest method getAttribute.

/**
     * Test the <code>RadiusRequest#getAttribute</code> method.
     *
     * @see org.forgerock.openam.radius.server.RadiusRequest#getAttribute
     */
@Test
public void getAttribute() {
    // Given
    UserNameAttribute una = new UserNameAttribute("testUser");
    AccessRequest packet = new AccessRequest();
    packet.addAttribute(una);
    RadiusRequest request = new RadiusRequest(packet);
    // When
    UserNameAttribute attribute = (UserNameAttribute) request.getAttribute(UserNameAttribute.class);
    // then
    assertThat(attribute).isSameAs(una);
}
Also used : AccessRequest(org.forgerock.openam.radius.common.AccessRequest) UserNameAttribute(org.forgerock.openam.radius.common.UserNameAttribute) Test(org.testng.annotations.Test)

Example 8 with AccessRequest

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

the class RadiusRequestTest method getUsername.

/**
     * Test the <code>RadiusRequest#getUsername</code> method.
     *
     * @see org.forgerock.openam.radius.server.RadiusRequest#getUsername
     */
@Test
public void getUsername() {
    // Given
    String userName = "testUser";
    AccessRequest packet = new AccessRequest((short) 1, mock(Authenticator.class));
    UserNameAttribute userNameAttribute = new UserNameAttribute(userName);
    packet.addAttribute(userNameAttribute);
    RadiusRequest request = new RadiusRequest(packet);
    // when
    String returnedUserName = request.getUsername();
    // Then
    assertThat(returnedUserName).isEqualTo(userName);
}
Also used : AccessRequest(org.forgerock.openam.radius.common.AccessRequest) UserNameAttribute(org.forgerock.openam.radius.common.UserNameAttribute) Authenticator(org.forgerock.openam.radius.common.Authenticator) Test(org.testng.annotations.Test)

Example 9 with AccessRequest

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

the class RadiusConn method authenticate.

/**
     * Authenticates the username and password against the remote servers.
     *
     * @param name     the username.
     * @param password the password.
     * @throws IOException              if there is a problem.
     * @throws NoSuchAlgorithmException if there is a problem.
     * @throws RejectException          if there is a problem.
     * @throws ChallengeException       if there is a problem.
     */
public void authenticate(String name, String password) throws IOException, NoSuchAlgorithmException, RejectException, ChallengeException {
    AccessRequest req = createAccessRequest();
    req.addAttribute(new UserNameAttribute(name));
    req.addAttribute(new UserPasswordAttribute(req.getAuthenticator(), secret, password));
    req.addAttribute(new NASIPAddressAttribute(InetAddress.getLocalHost()));
    req.addAttribute(new NASPortAttribute(socket.getLocalPort()));
    sendPacket(req);
}
Also used : AccessRequest(org.forgerock.openam.radius.common.AccessRequest) UserNameAttribute(org.forgerock.openam.radius.common.UserNameAttribute) NASPortAttribute(org.forgerock.openam.radius.common.packet.NASPortAttribute) NASIPAddressAttribute(org.forgerock.openam.radius.common.packet.NASIPAddressAttribute) UserPasswordAttribute(org.forgerock.openam.radius.common.UserPasswordAttribute)

Example 10 with AccessRequest

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

the class RadiusConn method replyChallenge.

/**
     * Sends an access-request to the server in response to a challenge request.
     *
     * @param name     the username.
     * @param password the password.
     * @param ce       the challenge exception providing access to the original challenge response.
     * @throws IOException              if there is a problem.
     * @throws NoSuchAlgorithmException if there is a problem.
     * @throws RejectException          if there is a problem.
     * @throws ChallengeException       if there is a problem.
     */
public void replyChallenge(String name, String password, ChallengeException ce) throws IOException, NoSuchAlgorithmException, RejectException, ChallengeException {
    StateAttribute state = (StateAttribute) ce.getAttributeSet().getAttributeByType(AttributeType.STATE);
    if (state == null) {
        throw new IOException("State not found in challenge");
    }
    AccessRequest req = createAccessRequest();
    // needed in challenge
    req.addAttribute(state);
    if (name != null) {
        req.addAttribute(new UserNameAttribute(name));
    }
    req.addAttribute(new UserPasswordAttribute(req.getAuthenticator(), secret, password));
    req.addAttribute(new NASIPAddressAttribute(InetAddress.getLocalHost()));
    req.addAttribute(new NASPortAttribute(socket.getLocalPort()));
    sendPacket(req);
}
Also used : StateAttribute(org.forgerock.openam.radius.common.StateAttribute) AccessRequest(org.forgerock.openam.radius.common.AccessRequest) UserNameAttribute(org.forgerock.openam.radius.common.UserNameAttribute) NASPortAttribute(org.forgerock.openam.radius.common.packet.NASPortAttribute) IOException(java.io.IOException) NASIPAddressAttribute(org.forgerock.openam.radius.common.packet.NASIPAddressAttribute) UserPasswordAttribute(org.forgerock.openam.radius.common.UserPasswordAttribute)

Aggregations

AccessRequest (org.forgerock.openam.radius.common.AccessRequest)10 UserNameAttribute (org.forgerock.openam.radius.common.UserNameAttribute)6 Test (org.testng.annotations.Test)5 Authenticator (org.forgerock.openam.radius.common.Authenticator)4 Packet (org.forgerock.openam.radius.common.Packet)4 UserPasswordAttribute (org.forgerock.openam.radius.common.UserPasswordAttribute)4 NASIPAddressAttribute (org.forgerock.openam.radius.common.packet.NASIPAddressAttribute)3 NASPortAttribute (org.forgerock.openam.radius.common.packet.NASPortAttribute)3 IOException (java.io.IOException)2 RequestAuthenticator (org.forgerock.openam.radius.common.RequestAuthenticator)2 StateAttribute (org.forgerock.openam.radius.common.StateAttribute)2 AuthContext (com.sun.identity.authentication.AuthContext)1 Status (com.sun.identity.authentication.AuthContext.Status)1 PagePropertiesCallback (com.sun.identity.authentication.spi.PagePropertiesCallback)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 DatagramChannel (java.nio.channels.DatagramChannel)1 SecureRandom (java.security.SecureRandom)1 Properties (java.util.Properties)1 Callback (javax.security.auth.callback.Callback)1