+ pluginhost test project

This commit is contained in:
Philipp Crocoll
2014-04-22 06:27:13 +02:00
parent d1cc47057d
commit 4697dbf41c
204 changed files with 5037 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Keepass2AndroidPluginSDK</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.source=1.6

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false

View File

@@ -0,0 +1,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="keepass2android.pluginsdk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</application>
</manifest>

View File

@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
</lint>

View File

@@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View File

@@ -0,0 +1,15 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-19
android.library=true

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@@ -0,0 +1,11 @@
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>

View File

@@ -0,0 +1,12 @@
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>

View File

@@ -0,0 +1,5 @@
<resources>
<string name="app_name">Keepass2Android Plugin SDK</string>
</resources>

View File

@@ -0,0 +1,20 @@
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>

View File

@@ -0,0 +1,109 @@
package keepass2android.pluginsdk;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
public class AccessManager
{
private static final String PREF_KEY_SCOPE = "scope";
private static final String PREF_KEY_TOKEN = "token";
public static String stringArrayToString(ArrayList<String> values) {
JSONArray a = new JSONArray();
for (int i = 0; i < values.size(); i++) {
a.put(values.get(i));
}
if (!values.isEmpty()) {
return a.toString();
} else {
return null;
}
}
public static ArrayList<String> stringToStringArray(String s) {
ArrayList<String> strings = new ArrayList<String>();
if ((s != null) && (s != "")) {
try {
JSONArray a = new JSONArray(s);
for (int i = 0; i < a.length(); i++) {
String url = a.optString(i);
strings.add(url);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return strings;
}
public static void storeAccessToken(Context ctx, String hostPackage, String accessToken, ArrayList<String> scopes)
{
SharedPreferences prefs = getPrefsForHost(ctx, hostPackage);
//
if (accessToken.equals(prefs.getString(PREF_KEY_TOKEN, "")))
{
//token already available
return;
}
Editor edit = prefs.edit();
edit.putString(PREF_KEY_TOKEN, accessToken);
edit.putString(PREF_KEY_SCOPE, stringArrayToString(scopes));
edit.commit();
}
private static SharedPreferences getPrefsForHost(Context ctx,
String hostPackage) {
SharedPreferences prefs = ctx.getSharedPreferences("KP2A.PluginAccess."+hostPackage, Context.MODE_PRIVATE);
return prefs;
}
public static String tryGetAccessToken(Context ctx, String hostPackage, ArrayList<String> scopes) {
SharedPreferences prefs = getPrefsForHost(ctx, hostPackage);
ArrayList<String> currentScope = stringToStringArray(prefs.getString(PREF_KEY_SCOPE, ""));
if (isSubset(scopes, currentScope))
{
return prefs.getString(PREF_KEY_TOKEN, null);
}
else
{
return null;
}
}
public static boolean isSubset(ArrayList<String> requiredScopes,
ArrayList<String> availableScopes) {
for (String r: requiredScopes){
if (availableScopes.indexOf(r)<0)
return false;
}
return true;
}
public static void removeAccessToken(Context ctx, String hostPackage,
String accessToken) {
SharedPreferences prefs = getPrefsForHost(ctx, hostPackage);
if (prefs.getString(PREF_KEY_TOKEN, "").equals(accessToken))
{
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
}
}

View File

@@ -0,0 +1,95 @@
package keepass2android.pluginsdk;
import java.util.ArrayList;
import android.R.anim;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Broadcast flow between Host and Plugin
* ======================================
*
* The host is responsible for deciding when to initiate the session. It
* should initiate the session as soon as plugins are required or when a plugin
* has been updated through the OS.
* It will then send a broadcast to request the currently required scope.
* The plugin then sends a broadcast to the app which scope is required. If an
* access token is already available, it's sent along with the requset.
*
* If a previous permission has been revoked (or the app settings cleared or the
* permissions have been extended or the token is invalid for any other reason)
* the host will answer with a Revoked-Permission broadcast (i.e. the plugin is
* unconnected.)
*
* Unconnected plugins must be permitted by the user (requiring user action).
* When the user grants access, the plugin will receive an access token for
* the host. This access token is valid for the requested scope. If the scope
* changes (e.g after an update of the plugin), the access token becomes invalid.
*
*/
public abstract class PluginAccessBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
String action = intent.getAction();
android.util.Log.d("KP2A.pluginsdk", "received broadcast with action="+action);
if (action.equals(Strings.ACTION_TRIGGER_REQUEST_ACCESS))
{
requestAccess(ctx, intent);
} else if (action.equals(Strings.ACTION_RECEIVE_ACCESS))
{
receiveAccess(ctx, intent);
} else if (action.equals(Strings.ACTION_REVOKE_ACCESS))
{
revokeAccess(ctx, intent);
} else
{
//TODO handle unexpected action
}
}
private void revokeAccess(Context ctx, Intent intent) {
String senderPackage = intent.getStringExtra(Strings.EXTRA_SENDER);
String accessToken = intent.getStringExtra(Strings.EXTRA_ACCESS_TOKEN);
//this intent must send the invalid(ated) token to prevent malicious apps
//from revoking access of all plugins.
AccessManager.removeAccessToken(ctx, senderPackage, accessToken);
}
private void receiveAccess(Context ctx, Intent intent) {
String senderPackage = intent.getStringExtra(Strings.EXTRA_SENDER);
String accessToken = intent.getStringExtra(Strings.EXTRA_ACCESS_TOKEN);
AccessManager.storeAccessToken(ctx, senderPackage, accessToken, getScopes());
}
public void requestAccess(Context ctx, Intent intent) {
String senderPackage = intent.getStringExtra(Strings.EXTRA_SENDER);
String requestToken = intent.getStringExtra(Strings.EXTRA_REQUEST_TOKEN);
Intent rpi = new Intent(Strings.ACTION_REQUEST_ACCESS);
rpi.setPackage(senderPackage);
rpi.putExtra(Strings.EXTRA_SENDER, ctx.getPackageName());
rpi.putExtra(Strings.EXTRA_REQUEST_TOKEN, requestToken);
String token = AccessManager.tryGetAccessToken(ctx, senderPackage, getScopes());
if (token != null)
{
rpi.putExtra(Strings.EXTRA_ACCESS_TOKEN, token);
}
rpi.putStringArrayListExtra(Strings.EXTRA_SCOPES, getScopes());
ctx.sendBroadcast(rpi);
}
/**
*
* @return the list of required scopes for this plugin.
*/
abstract public ArrayList<String> getScopes();
}

View File

@@ -0,0 +1,19 @@
package keepass2android.pluginsdk;
public class Strings {
public static final String SCOPE_DATABASE_ACTIONS = "keepass2android.SCOPE_DATABASE_ACTIONS";
public static final String SCOPE_CURRENT_ENTRY = "keepass2android.SCOPE_CURRENT_ENTRY";
public static final String EXTRA_SCOPES = "keepass2android.EXTRA_SCOPES";
public static final String EXTRA_SENDER = "keepass2android.EXTRA_SENDER";
public static final String EXTRA_REQUEST_TOKEN = "keepass2android.EXTRA_REQUEST_TOKEN";
public static final String ACTION_TRIGGER_REQUEST_ACCESS = "keepass2android.ACTION_TRIGGER_REQUEST_ACCESS";
public static final String ACTION_REQUEST_ACCESS = "keepass2android.ACTION_REQUEST_ACCESS";
public static final String ACTION_RECEIVE_ACCESS = "keepass2android.ACTION_RECEIVE_ACCESS";
public static final String ACTION_REVOKE_ACCESS = "keepass2android.ACTION_REVOKE_ACCESS";
public static final String EXTRA_ACCESS_TOKEN = "EXTRA_ACCESS_TOKEN";
//static final String SCOPE_DATABASE_ACTIONS = "keepass2android.SCOPE_DATABASE_ACTIONS";
}