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();
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations