| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.app.AlertDialog; | 8 import android.app.AlertDialog; |
| 9 import android.content.DialogInterface; | 9 import android.content.DialogInterface; |
| 10 import android.content.Intent; | 10 import android.content.Intent; |
| 11 import android.content.SharedPreferences; | 11 import android.content.SharedPreferences; |
| 12 import android.os.Build; | 12 import android.os.Build; |
| 13 import android.view.KeyEvent; | 13 import android.view.KeyEvent; |
| 14 import android.view.View; | 14 import android.view.View; |
| 15 import android.widget.CheckBox; | 15 import android.widget.CheckBox; |
| 16 import android.widget.TextView; | 16 import android.widget.TextView; |
| 17 import android.widget.Toast; | 17 import android.widget.Toast; |
| 18 | 18 |
| 19 import org.chromium.chromoting.jni.JniInterface; | 19 import org.chromium.chromoting.jni.Client; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * This class performs the user-interaction needed to authenticate the session c
onnection. This | 22 * This class performs the user-interaction needed to authenticate the session c
onnection. This |
| 23 * includes showing the PIN prompt and requesting tokens for third-party authent
ication. | 23 * includes showing the PIN prompt and requesting tokens for third-party authent
ication. |
| 24 */ | 24 */ |
| 25 public class SessionAuthenticator { | 25 public class SessionAuthenticator { |
| 26 /** | 26 /** |
| 27 * Application context used for getting user preferences, displaying UI, and
fetching | 27 * Application context used for getting user preferences, displaying UI, and
fetching |
| 28 * third-party tokens. | 28 * third-party tokens. |
| 29 */ | 29 */ |
| 30 private Chromoting mApplicationContext; | 30 private Chromoting mApplicationContext; |
| 31 | 31 |
| 32 /** Client connection being authenticated. */ |
| 33 private final Client mClient; |
| 34 |
| 32 /** Provides the tokenUrlPatterns for this host during fetchThirdPartyTokens
(). */ | 35 /** Provides the tokenUrlPatterns for this host during fetchThirdPartyTokens
(). */ |
| 33 private HostInfo mHost; | 36 private HostInfo mHost; |
| 34 | 37 |
| 35 /** Object for fetching OAuth2 access tokens from third party authorization
servers. */ | 38 /** Object for fetching OAuth2 access tokens from third party authorization
servers. */ |
| 36 private ThirdPartyTokenFetcher mTokenFetcher; | 39 private ThirdPartyTokenFetcher mTokenFetcher; |
| 37 | 40 |
| 38 public SessionAuthenticator(Chromoting context, HostInfo host) { | 41 public SessionAuthenticator(Chromoting context, Client client, HostInfo host
) { |
| 39 mApplicationContext = context; | 42 mApplicationContext = context; |
| 43 mClient = client; |
| 40 mHost = host; | 44 mHost = host; |
| 41 } | 45 } |
| 42 | 46 |
| 43 public void displayAuthenticationPrompt(boolean pairingSupported) { | 47 public void displayAuthenticationPrompt(boolean pairingSupported) { |
| 44 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont
ext); | 48 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont
ext); |
| 45 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti
cate)); | 49 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti
cate)); |
| 46 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_
android)); | 50 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_
android)); |
| 47 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); | 51 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); |
| 48 | 52 |
| 49 final View pinEntry = | 53 final View pinEntry = |
| 50 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia
log, null); | 54 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia
log, null); |
| 51 pinPrompt.setView(pinEntry); | 55 pinPrompt.setView(pinEntry); |
| 52 | 56 |
| 53 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d
ialog_text); | 57 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d
ialog_text); |
| 54 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d
ialog_check); | 58 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d
ialog_check); |
| 55 | 59 |
| 56 if (!pairingSupported) { | 60 if (!pairingSupported) { |
| 57 pinCheckBox.setChecked(false); | 61 pinCheckBox.setChecked(false); |
| 58 pinCheckBox.setVisibility(View.GONE); | 62 pinCheckBox.setVisibility(View.GONE); |
| 59 } | 63 } |
| 60 | 64 |
| 61 pinPrompt.setPositiveButton( | 65 pinPrompt.setPositiveButton( |
| 62 R.string.connect_button, new DialogInterface.OnClickListener() { | 66 R.string.connect_button, new DialogInterface.OnClickListener() { |
| 63 @Override | 67 @Override |
| 64 public void onClick(DialogInterface dialog, int which) { | 68 public void onClick(DialogInterface dialog, int which) { |
| 65 if (JniInterface.isConnected()) { | 69 if (mClient.isConnected()) { |
| 66 JniInterface.handleAuthenticationResponse( | 70 mClient.handleAuthenticationResponse( |
| 67 String.valueOf(pinTextView.getText()), | 71 String.valueOf(pinTextView.getText()), |
| 68 pinCheckBox.isChecked(), Build.MODEL); | 72 pinCheckBox.isChecked(), Build.MODEL); |
| 69 } else { | 73 } else { |
| 70 String message = | 74 String message = |
| 71 mApplicationContext.getString(R.string.error
_network_error); | 75 mApplicationContext.getString(R.string.error
_network_error); |
| 72 Toast.makeText(mApplicationContext, message, Toast.L
ENGTH_LONG).show(); | 76 Toast.makeText(mApplicationContext, message, Toast.L
ENGTH_LONG).show(); |
| 73 } | 77 } |
| 74 } | 78 } |
| 75 }); | 79 }); |
| 76 | 80 |
| 77 pinPrompt.setNegativeButton( | 81 pinPrompt.setNegativeButton( |
| 78 R.string.cancel, new DialogInterface.OnClickListener() { | 82 R.string.cancel, new DialogInterface.OnClickListener() { |
| 79 @Override | 83 @Override |
| 80 public void onClick(DialogInterface dialog, int which) { | 84 public void onClick(DialogInterface dialog, int which) { |
| 81 JniInterface.disconnectFromHost(); | 85 mClient.destroy(); |
| 82 } | 86 } |
| 83 }); | 87 }); |
| 84 | 88 |
| 85 final AlertDialog pinDialog = pinPrompt.create(); | 89 final AlertDialog pinDialog = pinPrompt.create(); |
| 86 | 90 |
| 87 pinTextView.setOnEditorActionListener( | 91 pinTextView.setOnEditorActionListener( |
| 88 new TextView.OnEditorActionListener() { | 92 new TextView.OnEditorActionListener() { |
| 89 @Override | 93 @Override |
| 90 public boolean onEditorAction(TextView v, int actionId, KeyE
vent event) { | 94 public boolean onEditorAction(TextView v, int actionId, KeyE
vent event) { |
| 91 // The user pressed enter on the keypad (equivalent to t
he connect button). | 95 // The user pressed enter on the keypad (equivalent to t
he connect button). |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 public void onTokenFetched(String code, String accessToken) { | 135 public void onTokenFetched(String code, String accessToken) { |
| 132 // The native client sends the OAuth authorization code to the h
ost as the token so | 136 // The native client sends the OAuth authorization code to the h
ost as the token so |
| 133 // that the host can obtain the shared secret from the third par
ty authorization | 137 // that the host can obtain the shared secret from the third par
ty authorization |
| 134 // server. | 138 // server. |
| 135 String token = code; | 139 String token = code; |
| 136 | 140 |
| 137 // The native client uses the OAuth access token as the shared s
ecret to | 141 // The native client uses the OAuth access token as the shared s
ecret to |
| 138 // authenticate itself with the host using spake. | 142 // authenticate itself with the host using spake. |
| 139 String sharedSecret = accessToken; | 143 String sharedSecret = accessToken; |
| 140 | 144 |
| 141 JniInterface.onThirdPartyTokenFetched(token, sharedSecret); | 145 mClient.onThirdPartyTokenFetched(token, sharedSecret); |
| 142 } | 146 } |
| 143 }; | 147 }; |
| 144 mTokenFetcher = new ThirdPartyTokenFetcher(mApplicationContext, mHost.ge
tTokenUrlPatterns(), | 148 mTokenFetcher = new ThirdPartyTokenFetcher(mApplicationContext, mHost.ge
tTokenUrlPatterns(), |
| 145 callback); | 149 callback); |
| 146 mTokenFetcher.fetchToken(tokenUrl, clientId, scope); | 150 mTokenFetcher.fetchToken(tokenUrl, clientId, scope); |
| 147 } | 151 } |
| 148 | 152 |
| 149 public void onNewIntent(Intent intent) { | 153 public void onNewIntent(Intent intent) { |
| 150 if (mTokenFetcher != null) { | 154 if (mTokenFetcher != null) { |
| 151 if (mTokenFetcher.handleTokenFetched(intent)) { | 155 if (mTokenFetcher.handleTokenFetched(intent)) { |
| 152 mTokenFetcher = null; | 156 mTokenFetcher = null; |
| 153 } | 157 } |
| 154 } | 158 } |
| 155 } | 159 } |
| 156 | 160 |
| 157 /** Returns the pairing ID for the given host, or an empty string if not set
. */ | 161 /** Returns the pairing ID for the given host, or an empty string if not set
. */ |
| 158 public String getPairingId(String hostId) { | 162 public String getPairingId(String hostId) { |
| 159 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); | 163 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); |
| 160 return prefs.getString(hostId + "_id", ""); | 164 return prefs.getString(hostId + "_id", ""); |
| 161 } | 165 } |
| 162 | 166 |
| 163 /** Returns the pairing secret for the given host, or an empty string if not
set. */ | 167 /** Returns the pairing secret for the given host, or an empty string if not
set. */ |
| 164 public String getPairingSecret(String hostId) { | 168 public String getPairingSecret(String hostId) { |
| 165 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); | 169 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); |
| 166 return prefs.getString(hostId + "_secret", ""); | 170 return prefs.getString(hostId + "_secret", ""); |
| 167 } | 171 } |
| 168 } | 172 } |
| OLD | NEW |