15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2014 The Android Open Source Project
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * you may not use this file except in compliance with the License.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at
73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * See the License for the specific language governing permissions and
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * limitations under the License.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)package android.bluetooth.client.pbap;
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.os.Handler;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.util.Log;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import javax.obex.Authenticator;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import javax.obex.PasswordAuthentication;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BluetoothPbapObexAuthenticator implements Authenticator {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    private final static String TAG = "BluetoothPbapObexAuthenticator";
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private String mSessionKey;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private boolean mReplied;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    private final Handler mCallback;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public BluetoothPbapObexAuthenticator(Handler callback) {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mCallback = callback;
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public synchronized void setReply(String key) {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        Log.d(TAG, "setReply key=" + key);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mSessionKey = key;
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        mReplied = true;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        notify();
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @Override
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public PasswordAuthentication onAuthenticationChallenge(String description,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            boolean isUserIdRequired, boolean isFullAccess) {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        PasswordAuthentication pa = null;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
53        mReplied = false;
54
55        Log.d(TAG, "onAuthenticationChallenge: sending request");
56        mCallback.obtainMessage(BluetoothPbapObexSession.OBEX_SESSION_AUTHENTICATION_REQUEST)
57                .sendToTarget();
58
59        synchronized (this) {
60            while (!mReplied) {
61                try {
62                    Log.v(TAG, "onAuthenticationChallenge: waiting for response");
63                    this.wait();
64                } catch (InterruptedException e) {
65                    Log.e(TAG, "Interrupted while waiting for challenge response");
66                }
67            }
68        }
69
70        if (mSessionKey != null && mSessionKey.length() != 0) {
71            Log.v(TAG, "onAuthenticationChallenge: mSessionKey=" + mSessionKey);
72            pa = new PasswordAuthentication(null, mSessionKey.getBytes());
73        } else {
74            Log.v(TAG, "onAuthenticationChallenge: mSessionKey is empty, timeout/cancel occured");
75        }
76
77        return pa;
78    }
79
80    @Override
81    public byte[] onAuthenticationResponse(byte[] userName) {
82        /* required only in case PCE challenges PSE which we don't do now */
83        return null;
84    }
85
86}
87