Home | History | Annotate | Download | only in map
      1 /*
      2 * Copyright (C) 2013 Samsung System LSI
      3 * Licensed under the Apache License, Version 2.0 (the "License");
      4 * you may not use this file except in compliance with the License.
      5 * You may obtain a copy of the License at
      6 *
      7 *      http://www.apache.org/licenses/LICENSE-2.0
      8 *
      9 * Unless required by applicable law or agreed to in writing, software
     10 * distributed under the License is distributed on an "AS IS" BASIS,
     11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 * See the License for the specific language governing permissions and
     13 * limitations under the License.
     14 */
     15 package com.android.bluetooth.map;
     16 
     17 import android.os.Handler;
     18 import android.os.Message;
     19 import android.util.Log;
     20 
     21 import javax.obex.Authenticator;
     22 import javax.obex.PasswordAuthentication;
     23 
     24 /**
     25  * BluetoothMapAuthenticator is a used by BluetoothObexServer for obex
     26  * authentication procedure.
     27  */
     28 public class BluetoothMapAuthenticator implements Authenticator {
     29     private static final String TAG = "BluetoothMapAuthenticator";
     30 
     31     private boolean mChallenged;
     32 
     33     private boolean mAuthCancelled;
     34 
     35     private String mSessionKey;
     36 
     37     private Handler mCallback;
     38 
     39     public BluetoothMapAuthenticator(final Handler callback) {
     40         mCallback = callback;
     41         mChallenged = false;
     42         mAuthCancelled = false;
     43         mSessionKey = null;
     44     }
     45 
     46     public final synchronized void setChallenged(final boolean bool) {
     47         mChallenged = bool;
     48     }
     49 
     50     public final synchronized void setCancelled(final boolean bool) {
     51         mAuthCancelled = bool;
     52     }
     53 
     54     public final synchronized void setSessionKey(final String string) {
     55         mSessionKey = string;
     56     }
     57 
     58     private void waitUserConfirmation() {
     59         Message msg = Message.obtain(mCallback);
     60         msg.what = BluetoothMapService.MSG_OBEX_AUTH_CHALL;
     61         msg.sendToTarget();
     62         synchronized (this) {
     63             while (!mChallenged && !mAuthCancelled) {
     64                 try {
     65                     wait();
     66                 } catch (InterruptedException e) {
     67                     Log.e(TAG, "Interrupted while waiting on isChallenged");
     68                 }
     69             }
     70         }
     71     }
     72 
     73     public PasswordAuthentication onAuthenticationChallenge(final String description,
     74             final boolean isUserIdRequired, final boolean isFullAccess) {
     75         waitUserConfirmation();
     76         if (mSessionKey.trim().length() != 0) {
     77             PasswordAuthentication pa = new PasswordAuthentication(null, mSessionKey.getBytes());
     78             return pa;
     79         }
     80         return null;
     81     }
     82 
     83     // TODO: Reserved for future use only, in case MSE challenge MCE
     84     public byte[] onAuthenticationResponse(final byte[] userName) {
     85         byte[] b = null;
     86         return b;
     87     }
     88 }
     89