| 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.Client; | 19 import org.chromium.chromoting.jni.JniInterface; |
| 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 | |
| 35 /** Provides the tokenUrlPatterns for this host during fetchThirdPartyTokens
(). */ | 32 /** Provides the tokenUrlPatterns for this host during fetchThirdPartyTokens
(). */ |
| 36 private HostInfo mHost; | 33 private HostInfo mHost; |
| 37 | 34 |
| 38 /** Object for fetching OAuth2 access tokens from third party authorization
servers. */ | 35 /** Object for fetching OAuth2 access tokens from third party authorization
servers. */ |
| 39 private ThirdPartyTokenFetcher mTokenFetcher; | 36 private ThirdPartyTokenFetcher mTokenFetcher; |
| 40 | 37 |
| 41 public SessionAuthenticator(Chromoting context, Client client, HostInfo host
) { | 38 public SessionAuthenticator(Chromoting context, HostInfo host) { |
| 42 mApplicationContext = context; | 39 mApplicationContext = context; |
| 43 mClient = client; | |
| 44 mHost = host; | 40 mHost = host; |
| 45 } | 41 } |
| 46 | 42 |
| 47 public void displayAuthenticationPrompt(boolean pairingSupported) { | 43 public void displayAuthenticationPrompt(boolean pairingSupported) { |
| 48 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont
ext); | 44 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont
ext); |
| 49 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti
cate)); | 45 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti
cate)); |
| 50 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_
android)); | 46 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_
android)); |
| 51 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); | 47 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); |
| 52 | 48 |
| 53 final View pinEntry = | 49 final View pinEntry = |
| 54 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia
log, null); | 50 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia
log, null); |
| 55 pinPrompt.setView(pinEntry); | 51 pinPrompt.setView(pinEntry); |
| 56 | 52 |
| 57 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d
ialog_text); | 53 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d
ialog_text); |
| 58 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d
ialog_check); | 54 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d
ialog_check); |
| 59 | 55 |
| 60 if (!pairingSupported) { | 56 if (!pairingSupported) { |
| 61 pinCheckBox.setChecked(false); | 57 pinCheckBox.setChecked(false); |
| 62 pinCheckBox.setVisibility(View.GONE); | 58 pinCheckBox.setVisibility(View.GONE); |
| 63 } | 59 } |
| 64 | 60 |
| 65 pinPrompt.setPositiveButton( | 61 pinPrompt.setPositiveButton( |
| 66 R.string.connect_button, new DialogInterface.OnClickListener() { | 62 R.string.connect_button, new DialogInterface.OnClickListener() { |
| 67 @Override | 63 @Override |
| 68 public void onClick(DialogInterface dialog, int which) { | 64 public void onClick(DialogInterface dialog, int which) { |
| 69 if (mClient.isConnected()) { | 65 if (JniInterface.isConnected()) { |
| 70 mClient.handleAuthenticationResponse( | 66 JniInterface.handleAuthenticationResponse( |
| 71 String.valueOf(pinTextView.getText()), | 67 String.valueOf(pinTextView.getText()), |
| 72 pinCheckBox.isChecked(), Build.MODEL); | 68 pinCheckBox.isChecked(), Build.MODEL); |
| 73 } else { | 69 } else { |
| 74 String message = | 70 String message = |
| 75 mApplicationContext.getString(R.string.error
_network_error); | 71 mApplicationContext.getString(R.string.error
_network_error); |
| 76 Toast.makeText(mApplicationContext, message, Toast.L
ENGTH_LONG).show(); | 72 Toast.makeText(mApplicationContext, message, Toast.L
ENGTH_LONG).show(); |
| 77 } | 73 } |
| 78 } | 74 } |
| 79 }); | 75 }); |
| 80 | 76 |
| 81 pinPrompt.setNegativeButton( | 77 pinPrompt.setNegativeButton( |
| 82 R.string.cancel, new DialogInterface.OnClickListener() { | 78 R.string.cancel, new DialogInterface.OnClickListener() { |
| 83 @Override | 79 @Override |
| 84 public void onClick(DialogInterface dialog, int which) { | 80 public void onClick(DialogInterface dialog, int which) { |
| 85 mClient.destroy(); | 81 JniInterface.disconnectFromHost(); |
| 86 } | 82 } |
| 87 }); | 83 }); |
| 88 | 84 |
| 89 final AlertDialog pinDialog = pinPrompt.create(); | 85 final AlertDialog pinDialog = pinPrompt.create(); |
| 90 | 86 |
| 91 pinTextView.setOnEditorActionListener( | 87 pinTextView.setOnEditorActionListener( |
| 92 new TextView.OnEditorActionListener() { | 88 new TextView.OnEditorActionListener() { |
| 93 @Override | 89 @Override |
| 94 public boolean onEditorAction(TextView v, int actionId, KeyE
vent event) { | 90 public boolean onEditorAction(TextView v, int actionId, KeyE
vent event) { |
| 95 // The user pressed enter on the keypad (equivalent to t
he connect button). | 91 // 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... |
| 135 public void onTokenFetched(String code, String accessToken) { | 131 public void onTokenFetched(String code, String accessToken) { |
| 136 // The native client sends the OAuth authorization code to the h
ost as the token so | 132 // The native client sends the OAuth authorization code to the h
ost as the token so |
| 137 // that the host can obtain the shared secret from the third par
ty authorization | 133 // that the host can obtain the shared secret from the third par
ty authorization |
| 138 // server. | 134 // server. |
| 139 String token = code; | 135 String token = code; |
| 140 | 136 |
| 141 // The native client uses the OAuth access token as the shared s
ecret to | 137 // The native client uses the OAuth access token as the shared s
ecret to |
| 142 // authenticate itself with the host using spake. | 138 // authenticate itself with the host using spake. |
| 143 String sharedSecret = accessToken; | 139 String sharedSecret = accessToken; |
| 144 | 140 |
| 145 mClient.onThirdPartyTokenFetched(token, sharedSecret); | 141 JniInterface.onThirdPartyTokenFetched(token, sharedSecret); |
| 146 } | 142 } |
| 147 }; | 143 }; |
| 148 mTokenFetcher = new ThirdPartyTokenFetcher(mApplicationContext, mHost.ge
tTokenUrlPatterns(), | 144 mTokenFetcher = new ThirdPartyTokenFetcher(mApplicationContext, mHost.ge
tTokenUrlPatterns(), |
| 149 callback); | 145 callback); |
| 150 mTokenFetcher.fetchToken(tokenUrl, clientId, scope); | 146 mTokenFetcher.fetchToken(tokenUrl, clientId, scope); |
| 151 } | 147 } |
| 152 | 148 |
| 153 public void onNewIntent(Intent intent) { | 149 public void onNewIntent(Intent intent) { |
| 154 if (mTokenFetcher != null) { | 150 if (mTokenFetcher != null) { |
| 155 if (mTokenFetcher.handleTokenFetched(intent)) { | 151 if (mTokenFetcher.handleTokenFetched(intent)) { |
| 156 mTokenFetcher = null; | 152 mTokenFetcher = null; |
| 157 } | 153 } |
| 158 } | 154 } |
| 159 } | 155 } |
| 160 | 156 |
| 161 /** Returns the pairing ID for the given host, or an empty string if not set
. */ | 157 /** Returns the pairing ID for the given host, or an empty string if not set
. */ |
| 162 public String getPairingId(String hostId) { | 158 public String getPairingId(String hostId) { |
| 163 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); | 159 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); |
| 164 return prefs.getString(hostId + "_id", ""); | 160 return prefs.getString(hostId + "_id", ""); |
| 165 } | 161 } |
| 166 | 162 |
| 167 /** Returns the pairing secret for the given host, or an empty string if not
set. */ | 163 /** Returns the pairing secret for the given host, or an empty string if not
set. */ |
| 168 public String getPairingSecret(String hostId) { | 164 public String getPairingSecret(String hostId) { |
| 169 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); | 165 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO
DE_PRIVATE); |
| 170 return prefs.getString(hostId + "_secret", ""); | 166 return prefs.getString(hostId + "_secret", ""); |
| 171 } | 167 } |
| 172 } | 168 } |
| OLD | NEW |