AI2 Component  (Version nb184)
ClientLoginHelper.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import android.accounts.Account;
10 import android.accounts.AccountManager;
11 import android.accounts.AccountManagerFuture;
12 import android.accounts.AuthenticatorException;
13 import android.accounts.OperationCanceledException;
14 import android.app.Activity;
15 import android.os.Bundle;
16 import android.os.Looper;
17 import android.util.Log;
18 
19 import org.apache.http.Header;
20 import org.apache.http.HttpResponse;
21 import org.apache.http.client.ClientProtocolException;
22 import org.apache.http.client.HttpClient;
23 import org.apache.http.client.methods.HttpUriRequest;
24 import org.apache.http.impl.client.DefaultHttpClient;
25 
26 import java.io.IOException;
27 
38 public class ClientLoginHelper implements IClientLoginHelper {
39  private static final String LOG_TAG = "ClientLoginHelper";
40  private static final String ACCOUNT_TYPE = "com.google";
41  private static final String AUTHORIZATION_HEADER_PREFIX = "GoogleLogin auth=";
42 
43  private String service;
44  private HttpClient client;
45  private Activity activity;
46  private AccountManager accountManager;
47  private AccountChooser accountChooser;
48 
49  private String authToken;
50  private boolean initialized = false;
51 
59  public ClientLoginHelper(Activity activity, String service, String prompt, HttpClient client) {
60  this.service = service;
61  this.client = (client == null) ? new DefaultHttpClient() : client;
62  this.activity = activity;
63  this.accountManager = AccountManager.get(activity);
64  this.accountChooser = new AccountChooser(activity, service, prompt, service);
65  }
66 
71  private void initialize() throws ClientProtocolException {
72  if (!initialized) {
73  Log.i(LOG_TAG, "initializing");
74  if (isUiThread()) {
75  throw new IllegalArgumentException("Can't initialize login helper from UI thread");
76  }
77  authToken = getAuthToken();
78  initialized = true;
79  }
80  }
81 
82  private boolean isUiThread() {
83  return Looper.getMainLooper().getThread().equals(Thread.currentThread());
84  }
85 
91  @Override
92  public HttpResponse execute(HttpUriRequest request)
93  throws ClientProtocolException, IOException {
94  initialize();
95  addGoogleAuthHeader(request, authToken);
96  HttpResponse response = client.execute(request);
97  if (response.getStatusLine().getStatusCode() == 401) {
98  Log.i(LOG_TAG, "Invalid token: " + authToken);
99  accountManager.invalidateAuthToken(ACCOUNT_TYPE, authToken);
100  authToken = getAuthToken();
101  removeGoogleAuthHeaders(request);
102  addGoogleAuthHeader(request, authToken);
103  Log.i(LOG_TAG, "new token: " + authToken);
104  response = client.execute(request);
105  }
106  return response;
107  }
108 
109 
116  @Override
117  public void forgetAccountName() {
118  accountChooser.forgetAccountName();
119  }
120 
121  private static void addGoogleAuthHeader(HttpUriRequest request, String token) {
122  if (token != null) {
123  Log.i(LOG_TAG, "adding auth token token: " + token);
124  request.addHeader("Authorization", AUTHORIZATION_HEADER_PREFIX + token);
125  }
126  }
127 
128  private static void removeGoogleAuthHeaders(HttpUriRequest request) {
129  for (Header header : request.getAllHeaders()) {
130  if (header.getName().equalsIgnoreCase("Authorization") &&
131  header.getValue().startsWith(AUTHORIZATION_HEADER_PREFIX)) {
132  Log.i(LOG_TAG, "Removing header:" + header);
133  request.removeHeader(header);
134  }
135  }
136  }
137 
142  public String getAuthToken() throws ClientProtocolException {
143  Account account = accountChooser.findAccount();
144  if (account != null) {
145  AccountManagerFuture<Bundle> future;
146  future = accountManager.getAuthToken(account, service, null, activity, null, null);
147  Log.i(LOG_TAG, "Have account, auth token: " + future);
148  Bundle result;
149  try {
150  result = future.getResult();
151  return result.getString(AccountManager.KEY_AUTHTOKEN);
152  } catch (AuthenticatorException e) {
153  e.printStackTrace();
154  } catch (IOException e) {
155  e.printStackTrace();
156  } catch (OperationCanceledException e) {
157  e.printStackTrace();
158  }
159  }
160  throw new ClientProtocolException("Can't get valid authentication token");
161  }
162 }
com.google.appinventor.components.runtime.util.AccountChooser.forgetAccountName
void forgetAccountName()
Definition: AccountChooser.java:168
com.google.appinventor.components.runtime.util.ClientLoginHelper.ClientLoginHelper
ClientLoginHelper(Activity activity, String service, String prompt, HttpClient client)
Definition: ClientLoginHelper.java:59
com.google.appinventor.components.runtime.util.ClientLoginHelper.forgetAccountName
void forgetAccountName()
Definition: ClientLoginHelper.java:117
com.google.appinventor.components.runtime.util.ClientLoginHelper.execute
HttpResponse execute(HttpUriRequest request)
Definition: ClientLoginHelper.java:92
com.google.appinventor.components.runtime.util.IClientLoginHelper
Definition: IClientLoginHelper.java:23
com.google.appinventor.components.runtime.util.AccountChooser.findAccount
Account findAccount()
Definition: AccountChooser.java:67
com.google.appinventor.components.runtime.util.ClientLoginHelper.getAuthToken
String getAuthToken()
Definition: ClientLoginHelper.java:142
com.google.appinventor.components.runtime.util.AccountChooser
Definition: AccountChooser.java:41
com.google.appinventor.components.runtime.util.ClientLoginHelper
Definition: ClientLoginHelper.java:38