VancedMicroG/src/org/microg/gms/auth/loginservice/GoogleLoginService.java

172 lines
7.5 KiB
Java
Raw Normal View History

2015-02-01 22:27:46 +00:00
/*
* Copyright 2013-2015 µg Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.microg.gms.auth.loginservice;
import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
2015-02-01 22:27:46 +00:00
import android.accounts.NetworkErrorException;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
2015-03-09 23:06:49 +00:00
import android.util.Base64;
2015-02-03 22:08:55 +00:00
import android.util.Log;
import com.google.android.gms.R;
2015-02-10 02:33:30 +00:00
import org.microg.gms.auth.AskPermissionActivity;
2015-02-03 22:08:55 +00:00
import org.microg.gms.auth.AuthManager;
import org.microg.gms.auth.AuthResponse;
import org.microg.gms.auth.login.LoginActivity;
2015-03-06 19:08:47 +00:00
import org.microg.gms.common.PackageUtils;
2015-02-03 22:08:55 +00:00
import java.util.Arrays;
import java.util.List;
2015-02-01 22:27:46 +00:00
2015-03-09 23:06:49 +00:00
import static android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT;
import static android.accounts.AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE;
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
import static android.accounts.AccountManager.KEY_ANDROID_PACKAGE_NAME;
import static android.accounts.AccountManager.KEY_AUTHTOKEN;
import static android.accounts.AccountManager.KEY_BOOLEAN_RESULT;
import static android.accounts.AccountManager.KEY_CALLER_UID;
import static android.accounts.AccountManager.KEY_INTENT;
2015-02-01 22:27:46 +00:00
public class GoogleLoginService extends Service {
2015-03-01 13:54:31 +00:00
private static final String TAG = "GmsAuthLoginSvc";
2015-02-03 22:08:55 +00:00
private String accountType;
@Override
public void onCreate() {
super.onCreate();
accountType = getString(R.string.google_account_type);
}
2015-02-01 22:27:46 +00:00
@Override
public IBinder onBind(Intent intent) {
2015-03-09 23:06:49 +00:00
if (intent.getAction().equals(ACTION_AUTHENTICATOR_INTENT)) {
2015-02-01 22:27:46 +00:00
return new AbstractAccountAuthenticator(this) {
@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
2015-02-03 22:08:55 +00:00
Log.d(TAG, "editProperties: " + accountType);
2015-02-01 22:27:46 +00:00
return null;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
2015-02-03 22:08:55 +00:00
if (accountType.equals(GoogleLoginService.this.accountType)) {
return GoogleLoginService.this.addAccount(response, authTokenType, requiredFeatures, options);
}
2015-02-01 22:27:46 +00:00
return null;
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
2015-02-03 22:08:55 +00:00
Log.d(TAG, "confirmCredentials: " + account + ", " + options);
2015-02-01 22:27:46 +00:00
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
2015-02-03 22:08:55 +00:00
return GoogleLoginService.this.getAuthToken(response, account, authTokenType, options);
2015-02-01 22:27:46 +00:00
}
@Override
public String getAuthTokenLabel(String authTokenType) {
2015-02-03 22:08:55 +00:00
Log.d(TAG, "getAuthTokenLabel: " + authTokenType);
2015-02-01 22:27:46 +00:00
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
Log.d(TAG, "updateCredentials: " + account + ", " + authTokenType + ", " + options);
2015-02-01 22:27:46 +00:00
return null;
}
@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
return GoogleLoginService.this.hasFeatures(account, features);
2015-02-01 22:27:46 +00:00
}
}.getIBinder();
}
return null;
}
2015-02-03 22:08:55 +00:00
private Bundle hasFeatures(Account account, String[] features) {
Log.d(TAG, "hasFeatures: " + account + ", " + Arrays.toString(features));
AccountManager accountManager = AccountManager.get(this);
List<String> services = Arrays.asList(accountManager.getUserData(account, "services").split(","));
boolean res = true;
for (String feature : features) {
if (feature.startsWith("service_") && !services.contains(feature.substring(8)))
res = false;
}
Bundle result = new Bundle();
result.putBoolean(KEY_BOOLEAN_RESULT, res);
return result;
}
2015-02-03 22:08:55 +00:00
private Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) {
options.keySet();
Log.d(TAG, "getAuthToken: " + account + ", " + authTokenType + ", " + options);
2015-03-09 23:06:49 +00:00
String app = options.getString(KEY_ANDROID_PACKAGE_NAME);
PackageUtils.checkPackageUid(this, app, options.getInt(KEY_CALLER_UID), options.getInt(KEY_CALLER_UID));
AuthManager authManager = new AuthManager(this, account.name, app, authTokenType);
try {
AuthResponse res = authManager.requestAuth(true);
if (res.auth != null) {
Log.d(TAG, "getAuthToken: " + res.auth);
Bundle result = new Bundle();
result.putString(KEY_ACCOUNT_TYPE, account.type);
result.putString(KEY_ACCOUNT_NAME, account.name);
result.putString(KEY_AUTHTOKEN, res.auth);
return result;
} else {
Bundle result = new Bundle();
Intent i = new Intent(this, AskPermissionActivity.class);
i.putExtras(options);
i.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
i.putExtra(KEY_ANDROID_PACKAGE_NAME, app);
i.putExtra(KEY_ACCOUNT_TYPE, account.type);
i.putExtra(KEY_ACCOUNT_NAME, account.name);
i.putExtra(KEY_AUTHTOKEN, authTokenType);
if (res.consentDataBase64 != null)
i.putExtra(AskPermissionActivity.EXTRA_CONSENT_DATA, Base64.decode(res.consentDataBase64, Base64.DEFAULT));
result.putParcelable(KEY_INTENT, i);
return result;
2015-02-03 22:08:55 +00:00
}
2015-03-09 23:06:49 +00:00
} catch (Exception e) {
Log.w(TAG, e);
return null;
2015-02-03 22:08:55 +00:00
}
}
private Bundle addAccount(AccountAuthenticatorResponse response, String authTokenType, String[] requiredFeatures, Bundle options) {
final Intent i = new Intent(GoogleLoginService.this, LoginActivity.class);
i.putExtras(options);
i.putExtra(LoginActivity.EXTRA_TMPL, LoginActivity.TMPL_NEW_ACCOUNT);
2015-03-09 23:06:49 +00:00
i.putExtra(KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
2015-02-03 22:08:55 +00:00
final Bundle result = new Bundle();
2015-03-09 23:06:49 +00:00
result.putParcelable(KEY_INTENT, i);
2015-02-03 22:08:55 +00:00
return result;
}
2015-02-01 22:27:46 +00:00
}