Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(670)

Side by Side Diff: remoting/android/java/src/org/chromium/chromoting/SessionAuthenticator.java

Issue 981783003: Pull authentication UX code out of JniInterface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix-jni-initialization
Patch Set: Move ThirdParty token stuff into SessionAuthenticator Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chromoting;
6
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.content.SharedPreferences;
12 import android.os.Build;
13 import android.view.KeyEvent;
14 import android.view.View;
15 import android.widget.CheckBox;
16 import android.widget.TextView;
17 import android.widget.Toast;
18
19 import org.chromium.chromoting.jni.JniInterface;
20
21 /**
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.
24 */
25 public class SessionAuthenticator {
26 /**
27 * Application context used for getting user preferences, displaying UI, and fetching
28 * third-party tokens.
29 */
30 private Chromoting mApplicationContext;
31
32 /** Object for fetching OAuth2 access tokens from third party authorization servers. */
33 private ThirdPartyTokenFetcher mTokenFetcher;
34
35 public SessionAuthenticator(Chromoting context, HostInfo host) {
36 mApplicationContext = context;
37
38 ThirdPartyTokenFetcher.Callback callback = new ThirdPartyTokenFetcher.Ca llback() {
39 @Override
40 public void onTokenFetched(String code, String accessToken) {
41 // The native client sends the OAuth authorization code to the h ost as the token so
42 // that the host can obtain the shared secret from the third par ty authorization
43 // server.
44 String token = code;
45
46 // The native client uses the OAuth access token as the shared s ecret to
47 // authenticate itself with the host using spake.
48 String sharedSecret = accessToken;
49
50 JniInterface.onThirdPartyTokenFetched(token, sharedSecret);
51 }
52 };
53 mTokenFetcher = new ThirdPartyTokenFetcher(context, host.getTokenUrlPatt erns(), callback);
Sergey Ulanov 2015/03/16 19:56:09 Does this need to be in the constructor? Can it be
Lambros 2015/03/18 01:01:14 Done.
Lambros 2015/03/18 01:01:14 Done.
54 }
55
56 public void displayAuthenticationPrompt(boolean pairingSupported) {
57 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont ext);
58 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti cate));
59 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_ android));
60 pinPrompt.setIcon(android.R.drawable.ic_lock_lock);
61
62 final View pinEntry =
63 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia log, null);
64 pinPrompt.setView(pinEntry);
65
66 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d ialog_text);
67 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d ialog_check);
68
69 if (!pairingSupported) {
70 pinCheckBox.setChecked(false);
71 pinCheckBox.setVisibility(View.GONE);
72 }
73
74 pinPrompt.setPositiveButton(
75 R.string.connect_button, new DialogInterface.OnClickListener() {
76 @Override
77 public void onClick(DialogInterface dialog, int which) {
78 if (JniInterface.isConnected()) {
79 JniInterface.handleAuthenticationResponse(
80 String.valueOf(pinTextView.getText()),
81 pinCheckBox.isChecked(), Build.MODEL);
82 } else {
83 String message =
84 mApplicationContext.getString(R.string.error _network_error);
85 Toast.makeText(mApplicationContext, message, Toast.L ENGTH_LONG).show();
86 }
87 }
88 });
89
90 pinPrompt.setNegativeButton(
91 R.string.cancel, new DialogInterface.OnClickListener() {
92 @Override
93 public void onClick(DialogInterface dialog, int which) {
94 JniInterface.disconnectFromHost();
95 }
96 });
97
98 final AlertDialog pinDialog = pinPrompt.create();
99
100 pinTextView.setOnEditorActionListener(
101 new TextView.OnEditorActionListener() {
102 @Override
103 public boolean onEditorAction(TextView v, int actionId, KeyE vent event) {
104 // The user pressed enter on the keypad (equivalent to t he connect button).
105 pinDialog.getButton(AlertDialog.BUTTON_POSITIVE).perform Click();
106 pinDialog.dismiss();
107 return true;
108 }
109 });
110
111 pinDialog.setOnCancelListener(
112 new DialogInterface.OnCancelListener() {
113 @Override
114 public void onCancel(DialogInterface dialog) {
115 // The user backed out of the dialog (equivalent to the cancel button).
116 pinDialog.getButton(AlertDialog.BUTTON_NEGATIVE).perform Click();
117 }
118 });
119
120 pinDialog.show();
121 }
122
123 /** Saves newly-received pairing credentials to permanent storage. */
124 public void commitPairingCredentials(String host, String id, String secret) {
125 // Empty |id| indicates that pairing needs to be removed.
126 if (id.isEmpty()) {
127 mApplicationContext.getPreferences(Activity.MODE_PRIVATE).edit()
128 .remove(host + "_id")
129 .remove(host + "_secret")
130 .apply();
131 } else {
132 mApplicationContext.getPreferences(Activity.MODE_PRIVATE).edit()
133 .putString(host + "_id", id)
134 .putString(host + "_secret", secret)
135 .apply();
136 }
137 }
138
139 public void fetchThirdPartyToken(String tokenUrl, String clientId, String sc ope) {
140 assert mTokenFetcher != null;
141 mTokenFetcher.fetchToken(tokenUrl, clientId, scope);
142 }
143
144 public void handleTokenFetched(Intent intent) {
Sergey Ulanov 2015/03/16 19:56:09 I think it would be better to call this method onN
Lambros 2015/03/18 01:01:14 Done.
145 if (mTokenFetcher != null) {
146 if (mTokenFetcher.handleTokenFetched(intent)) {
147 mTokenFetcher = null;
148 }
149 }
150 }
151
152 /** Returns the pairing ID for the given host, or an empty string if not set . */
153 public String getPairingId(String hostId) {
154 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE);
155 return prefs.getString(hostId + "_id", "");
156 }
157
158 /** Returns the pairing secret for the given host, or an empty string if not set. */
159 public String getPairingSecret(String hostId) {
160 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE);
161 return prefs.getString(hostId + "_secret", "");
162 }
163 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698