×
Namespaces

Variants
Actions

Using Bluetooth Obex server authentication in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Compatibility
Platform(s): S60 3rd Edition, FP2
Symbian
S60 3rd Edition FP2
Platform Security
Capabilities: )
Article
Keywords: javax.obex.SessionNotifier, javax.obex.RequestHandler, javax.obex.Authenticator, BtObexMessengerSrv.run, javax.obex.SessionNotifier.acceptAndOpen, javax.obex.RequestHandler.onConnect, javax.obex.RequestHandler.onAuthenticationFailure, javax.obex.Authenticator.onAuthenticationChallenge, javax.obex.Authenticator.onAuthenticationResponse
Created: vltsoy (29 Oct 2008)
Last edited: hamishwillee (17 Jun 2013)

Contents

Overview

The code snippet demonstrates Bluetooth OBEX over RFCOMM server with Authenticator support example. This example shows how to authenticate a client.

Client must send "user"/"user" as username/password in response. Server also can be authenticated from client side with "user"/"user" username/password pair.

If client sends incorrect username/password connection fails. This example shows only Authenticator using specific steps. The server itself is implemented in BtObexMessengerSrv snippet.

Source file: BtObexMessengerSrvAuthenticator.java

import javax.microedition.lcdui.CommandListener;
import javax.microedition.io.StreamConnectionNotifier;
import javax.obex.Operation;
import javax.obex.Authenticator;
import javax.obex.HeaderSet;
import javax.obex.PasswordAuthentication;
import javax.obex.ResponseCodes;
import javax.obex.ServerRequestHandler;
import javax.obex.SessionNotifier;
 
/**
* BtObexSrvAuthenicator MIDlet is an Obex over RFCOMM server.
* It acts as a message server.
* This example shows how OBEX Authenticator can be used.
* @see JSR82-specification for more info on server advertising.
*/

public class BtObexSrvAuthenicator extends MIDlet implements CommandListener,
Runnable {
/**
* Stores a reference to incoming connection notifier.
*/

private SessionNotifier sessionNotifier;
/**
* Used for handling client requests(GET and PUT).
* @see RequestHandler
* @see ServerRequestHandler
*/

private RequestHandler handler;
/**
* Instance of the Authenticator implementor.
* @see SrvAuthenticator
*/

private Authenticator auth;
/**
* Initializes SrvAuthenticator instance.
*/

public BtObexSrvAuthenicator() {
//any additional initialization steps here
 
auth = new SrvAuthenticator( "user".getBytes(), "user".getBytes() );
}
 
//CODE from BtObexMessengerSrv here
 
/**
* From Runnable.
* The method is being run when a connection listening thread was created
* and started(in startObexServer). It runs acceptAndOpen method.
* When a connection accepted doConnReplay is being executed. After that a
* cmdStop command is trigger to stop the server.
* @see StreamConnectionNotifier
* @see BtObexMessengerSrv#doConnReply()
*/

public void run() {
try {
printToFrm( "Waiting for connections..." );
sessionNotifier.acceptAndOpen(
handler,
auth );
printToFrm( "Incomming connection..." );
//waiting for messages
 
 
} catch ( Exception e ) {
printToFrm( "Connection waiting thread interropted!" );
printToFrm( e.toString() );
}
}
 
//CODE from BtObexMessengerSrv here
 
/**
* Defines an event listener that will respond to OBEX requests made to
* the server.
* @see ServerRequestHandler
*/

private class RequestHandler extends ServerRequestHandler {
boolean auth;
/**
* Default constructor.
*/

public RequestHandler() {
auth = false;
}
 
//CODE from BtObexMessengerSrv here
 
/**
* From ServerRequestHandler.
* Called when a CONNECT request is received.
* @param request- contains the headers sent by the client;
* request will never be null
* @param reply - the headers that should be sent in the reply;
* reply will never be null
* @return
*/

public int onConnect(HeaderSet request, HeaderSet reply) {
printToFrm( "Client is connecting..." );
frmMain.addCommand( cmdSend );
if( auth == false ) {
printToFrm( "Authenticating user!!!" );
reply.createAuthenticationChallenge( "ObexMessage",
true, true );
auth = true;
return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
}
auth = false;
printToFrm( "User authenticated!!!" );
return ResponseCodes.OBEX_HTTP_OK;
}
/**
* Called when this object attempts to authenticate a client and the
* authentication request fails because the response digest in the
* authentication response header was wrong.
* @param userName the user name returned in the authentication response.
* @see Authenticator
*/

public void onAuthenticationFailure(byte[] userName) {
printToFrm( "User not authenticated!!!" );
auth = false;
commandAction( cmdStop, frmMain );
}
}
/**
* Implements Authenticator interface.
* Interface provides a way to respond to authentication challenge and
* authentication response headers.
* @see Authenticator
*/

private class SrvAuthenticator implements
Authenticator {
/**
* Holds user value that will be used on authentication challenge.
*/

protected byte[] user;
/**
* Holds password value that will be used on authentication challenge.
*/

protected byte[] passw;
/**
* Default constructor.
* Sets user and passwd field values.
*/

public SrvAuthenticator( byte[] usr, byte[] pwd ) {
user = usr;
passw = pwd;
}
/**
* From Authenticator.
* Called when a server receives an authentication challenge header.
* Function returns a PasswordAuthentication object with values from
* user and passw fields.
* @param description Not used here. For more info see Javadoc for
* Authenticator.
* @param isUserIdRequired Not used here. For more info see Javadoc for
* Authenticator.
* @param isFullAccess Not used here. For more info see Javadoc for
* Authenticator.
* @return PasswordAuthentication object with values from
* user and passw fields.
*/

public PasswordAuthentication onAuthenticationChallenge(
String description,
boolean isUserIdRequired,
boolean isFullAccess ) {
return new PasswordAuthentication( user,
passw );
}
/**
* From Authenticator.
* Called when a client or server receives an authentication response
* header. This method will provide the user name and expect the
* correct password to be returned.
* @param usr the user name provided in the authentication response.
* @return the correct password for the user name provided.
*/

public byte[] onAuthenticationResponse(byte[] usr) {
return passw;
}
 
}
}

Postconditions

After Start command is pressed server is being started.

When client is connecting server sends an authentication header. If the authentication procedure fails client disconnects and server stops.

If the authentication succeeded a connection will be established and app starts acting as a OBEX server. OBEX server itself is implemented in BtObexMessengerSrv snippet.

Client app is implemented in BtObexCliAuthenticatorMidlet snippet.

See also

BtObexCliAuthenticatorMidlet snippet

BtObexMessengerSrv snippet

This page was last modified on 17 June 2013, at 04:51.
32 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×