From: kenstir Date: Sun, 17 Nov 2013 19:09:17 +0000 (-0500) Subject: checkpoint: moving auth package inside Evergreen app tree. Dealing with external... X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=ee2b89ed02855da3b4f8f4ccf6d942bf41478204;p=working%2FEvergreen.git checkpoint: moving auth package inside Evergreen app tree. Dealing with external Android library projects in eclipse has been a giant hassle and I don't really need it. --- diff --git a/Open-ILS/src/Android/src/org/evergreen_ils/auth/Authenticator.java b/Open-ILS/src/Android/src/org/evergreen_ils/auth/Authenticator.java new file mode 100644 index 0000000000..db0b242bed --- /dev/null +++ b/Open-ILS/src/Android/src/org/evergreen_ils/auth/Authenticator.java @@ -0,0 +1,114 @@ +package org.evergreen_ils.auth; + +import android.accounts.AbstractAccountAuthenticator; +import android.accounts.Account; +import android.accounts.AccountAuthenticatorResponse; +import android.accounts.AccountManager; +import android.accounts.NetworkErrorException; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.text.TextUtils; +import android.util.Log; + +public class Authenticator extends AbstractAccountAuthenticator { + + private final String TAG = "eg.auth"; + private Context context; + + public Authenticator(Context context) { + super(context); + this.context = context; + } + + @Override + public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException { + Log.d(TAG, "addaccount "+accountType+" "+authTokenType); + final Intent intent = new Intent(context, AuthenticatorActivity.class); + intent.putExtra(Const.AUTHTOKEN_TYPE, authTokenType); + intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); + + Bundle result = new Bundle(); + result.putParcelable(AccountManager.KEY_INTENT, intent); + return result; + } + + @Override + public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { + Log.d(TAG, "getAuthToken "+account.name); + + // If the caller requested an authToken type we don't support, then + // return an error + if (!authTokenType.equals(Const.AUTHTOKEN_TYPE)) { + final Bundle result = new Bundle(); + result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType"); + return result; + } + + final AccountManager am = AccountManager.get(context); + String authToken = am.peekAuthToken(account, authTokenType); + Log.d(TAG, "peekAuthToken returned " + authToken); + if (TextUtils.isEmpty(authToken)) { + final String password = am.getPassword(account); + if (password != null) { + try { + Log.d(TAG, "attempting to sign in with existing password"); + authToken = EvergreenAuthenticate.signIn(context, account.name, password); + } catch (Exception e) { + Log.d(TAG, "caught exception "+e.getMessage()); + final Bundle result = new Bundle(); + result.putString(AccountManager.KEY_ERROR_MESSAGE, e.getMessage()); + return result; + } + } + } + + // If we get an authToken - we return it + if (!TextUtils.isEmpty(authToken)) { + final Bundle result = new Bundle(); + result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); + result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); + result.putString(AccountManager.KEY_AUTHTOKEN, authToken); + return result; + } + + // If we get here, then we couldn't access the user's password - so we + // need to re-prompt them for their credentials. We do that by creating + // an intent to display our AuthenticatorActivity. + final Intent intent = new Intent(context, AuthenticatorActivity.class); + intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); + intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, account.type); + intent.putExtra(AuthenticatorActivity.ARG_AUTH_TYPE, authTokenType); + intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_NAME, account.name); + final Bundle bundle = new Bundle(); + bundle.putParcelable(AccountManager.KEY_INTENT, intent); + return bundle; + } + + @Override + public String getAuthTokenLabel(String authTokenType) { + return Const.AUTHTOKEN_TYPE_LABEL; + } + + @Override + public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException { + final Bundle result = new Bundle(); + result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); + return result; + } + + @Override + public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { + return null; + } + + @Override + public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException { + return null; + } + + @Override + public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { + return null; + } +} diff --git a/Open-ILS/src/Android/src/org/evergreen_ils/auth/AuthenticatorActivity.java b/Open-ILS/src/Android/src/org/evergreen_ils/auth/AuthenticatorActivity.java new file mode 100644 index 0000000000..24355614bf --- /dev/null +++ b/Open-ILS/src/Android/src/org/evergreen_ils/auth/AuthenticatorActivity.java @@ -0,0 +1,156 @@ +package org.evergreen_ils.auth; + +import android.accounts.AccountAuthenticatorActivity; +import android.accounts.AccountManager; +import android.accounts.Account; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +public class AuthenticatorActivity extends AccountAuthenticatorActivity { + + private final String TAG = "eg.auth"; + + public final static String ARG_ACCOUNT_TYPE = "ACCOUNT_TYPE"; + public final static String ARG_AUTH_TYPE = "AUTH_TYPE"; + public final static String ARG_ACCOUNT_NAME = "ACCOUNT_NAME"; + public final static String ARG_IS_ADDING_NEW_ACCOUNT = "IS_ADDING_ACCOUNT"; + + public static final String KEY_ERROR_MESSAGE = "ERR_MSG"; + + public final static String PARAM_USER_PASS = "USER_PASS"; + + private final int REQ_SIGNUP = 1; + + private AccountManager accountManager; + private String authTokenType; + + /** + * Called when the activity is first created. + */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_login); + accountManager = AccountManager.get(getBaseContext()); + + String accountName = getIntent().getStringExtra(ARG_ACCOUNT_NAME); + authTokenType = getIntent().getStringExtra(ARG_AUTH_TYPE); + if (authTokenType == null) + authTokenType = Const.AUTHTOKEN_TYPE; + + if (accountName != null) { + ((TextView) findViewById(R.id.accountName)).setText(accountName); + } + + findViewById(R.id.submit).setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View v) { + submit(); + } + }); + /* + * findViewById(R.id.signUp).setOnClickListener(new + * View.OnClickListener() { + * + * @Override public void onClick(View v) { // Since there can only be + * one AuthenticatorActivity, we call the sign up activity, get his + * results, // and return them in setAccountAuthenticatorResult(). See + * finishLogin(). Intent signup = new Intent(getBaseContext(), + * SignUpActivity.class); signup.putExtras(getIntent().getExtras()); + * startActivityForResult(signup, REQ_SIGNUP); } }); + */ + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + + // The sign up activity returned that the user has successfully created + // an account + if (requestCode == REQ_SIGNUP && resultCode == RESULT_OK) { + finishLogin(data); + } else + super.onActivityResult(requestCode, resultCode, data); + } + + public void submit() { + + final String username = ((TextView) findViewById(R.id.accountName)).getText().toString(); + final String password = ((TextView) findViewById(R.id.accountPassword)).getText().toString(); + //final String account_type = getIntent().getStringExtra(ARG_ACCOUNT_TYPE); + + new AsyncTask() { + + @Override + protected Intent doInBackground(String... params) { + + Log.d(TAG, "Started authenticating"); + + String authtoken = null; + Bundle data = new Bundle(); + try { + authtoken = EvergreenAuthenticate.signIn(AuthenticatorActivity.this, username, password); + + data.putString(AccountManager.KEY_ACCOUNT_NAME, username); + data.putString(AccountManager.KEY_ACCOUNT_TYPE, Const.ACCOUNT_TYPE); + data.putString(AccountManager.KEY_AUTHTOKEN, authtoken); + data.putString(PARAM_USER_PASS, password); + + } catch (Exception e) { + data.putString(KEY_ERROR_MESSAGE, e.getMessage()); + } + + final Intent res = new Intent(); + res.putExtras(data); + return res; + } + + @Override + protected void onPostExecute(Intent intent) { + if (intent.hasExtra(KEY_ERROR_MESSAGE)) { + Toast.makeText(getBaseContext(), + intent.getStringExtra(KEY_ERROR_MESSAGE), + Toast.LENGTH_SHORT).show(); + } else { + finishLogin(intent); + } + } + }.execute(); + } + + private void finishLogin(Intent intent) { + Log.d(TAG, "finishLogin"); + + String accountName = intent + .getStringExtra(AccountManager.KEY_ACCOUNT_NAME); + String accountPassword = intent.getStringExtra(PARAM_USER_PASS); + final Account account = new Account(accountName, + intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE)); + + if (getIntent().getBooleanExtra(ARG_IS_ADDING_NEW_ACCOUNT, false)) { + Log.d(TAG, "finishLogin > addAccountExplicitly"); + String authtoken = intent + .getStringExtra(AccountManager.KEY_AUTHTOKEN); + String authtokenType = authTokenType; + + // Creating the account on the device and setting the auth token we + // got + // (Not setting the auth token will cause another call to the server + // to authenticate the user) + accountManager.addAccountExplicitly(account, accountPassword, null); + accountManager.setAuthToken(account, authtokenType, authtoken); + } else { + Log.d(TAG, "finishLogin > setPassword"); + accountManager.setPassword(account, accountPassword); + } + + setAccountAuthenticatorResult(intent.getExtras()); + setResult(RESULT_OK, intent); + finish(); + } +} diff --git a/Open-ILS/src/Android/src/org/evergreen_ils/auth/AuthenticatorService.java b/Open-ILS/src/Android/src/org/evergreen_ils/auth/AuthenticatorService.java new file mode 100644 index 0000000000..3a54fe86c3 --- /dev/null +++ b/Open-ILS/src/Android/src/org/evergreen_ils/auth/AuthenticatorService.java @@ -0,0 +1,14 @@ +package org.evergreen_ils.auth; + +import org.evergreen_ils.auth.Authenticator; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +public class AuthenticatorService extends Service { + @Override + public IBinder onBind(Intent arg0) { + return new Authenticator(this).getIBinder(); + } +} diff --git a/Open-ILS/src/Android/src/org/evergreen_ils/auth/Const.java b/Open-ILS/src/Android/src/org/evergreen_ils/auth/Const.java new file mode 100644 index 0000000000..0f114a6e60 --- /dev/null +++ b/Open-ILS/src/Android/src/org/evergreen_ils/auth/Const.java @@ -0,0 +1,7 @@ +package org.evergreen_ils.auth; + +public class Const { + public static final String ACCOUNT_TYPE = "org.evergreen-ils.opac"; + public static final String AUTHTOKEN_TYPE = "opac"; + public static final String AUTHTOKEN_TYPE_LABEL = "Online Public Access Catalog"; +} diff --git a/Open-ILS/src/Android/src/org/evergreen_ils/auth/EvergreenAuthenticate.java b/Open-ILS/src/Android/src/org/evergreen_ils/auth/EvergreenAuthenticate.java new file mode 100644 index 0000000000..3831d57076 --- /dev/null +++ b/Open-ILS/src/Android/src/org/evergreen_ils/auth/EvergreenAuthenticate.java @@ -0,0 +1,112 @@ +package org.evergreen_ils.auth; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.Map; + +import org.opensrf.Method; +import org.opensrf.net.http.GatewayRequest; +import org.opensrf.net.http.HttpConnection; +import org.opensrf.net.http.HttpRequest; + +import android.content.Context; +import android.text.TextUtils; +import android.util.Log; + +public class EvergreenAuthenticate { + private final static String TAG = "eg.auth"; + public final static String SERVICE_AUTH = "open-ils.auth"; + public final static String METHOD_AUTH_INIT = "open-ils.auth.authenticate.init"; + public final static String METHOD_AUTH_COMPLETE = "open-ils.auth.authenticate.complete"; + + private static String md5(String s) { + try { + MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); + digest.update(s.getBytes()); + byte messageDigest[] = digest.digest(); + + // Create Hex String + StringBuffer hexString = new StringBuffer(); + for (int i = 0; i < messageDigest.length; i++) { + String hex = Integer.toHexString(0xFF & messageDigest[i]); + if (hex.length() == 1) { + // could use a for loop, but we're only dealing with a + // single byte + hexString.append('0'); + } + hexString.append(hex); + } + return hexString.toString(); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + + return ""; + } + + public static Object doRequest(HttpConnection conn, String service, String methodName, Object[] params) throws Exception { + Method method = new Method(methodName); + + Log.d(TAG, "doRequest Method :" + methodName + ":"); + for (int i = 0; i < params.length; i++) { + method.addParam(params[i]); + Log.d(TAG, "Param " + i + ": " + params[i]); + } + + // sync request + HttpRequest req = new GatewayRequest(conn, service, method).send(); + Object resp; + + while ((resp = req.recv()) != null) { + Log.d(TAG, "Sync Response: " + resp); + Object response = (Object) resp; + return response; + } + return null; + } + + @SuppressWarnings("unchecked") + public static String signIn(Context context, String username, String password) throws Exception { + Log.d(TAG, "signIn "+username); + + HttpConnection conn = new HttpConnection(context.getString(R.string.gateway_url)); + + // step 1: get seed + Object resp = doRequest(conn, SERVICE_AUTH, METHOD_AUTH_INIT, new Object[] { username }); + if (resp == null) + throw new Exception("Unable to contact login service"); + String seed = resp.toString(); + + // step 2: complete auth with seed + password + HashMap complexParam = new HashMap(); + complexParam.put("type", "opac"); + complexParam.put("username", username); + complexParam.put("password", md5(seed + md5(password))); + resp = doRequest(conn, SERVICE_AUTH, METHOD_AUTH_COMPLETE, new Object[] { complexParam }); + if (resp == null) + throw new Exception("Unable to complete login"); + + // parse response + String textcode = ((Map) resp).get("textcode"); + System.out.println("textcode: " + textcode); + if (textcode.equals("SUCCESS")) { + Object payload = ((Map) resp).get("payload"); + System.out.println("payload: " + payload); + String authtoken = ((Map) payload).get("authtoken"); + System.out.println("authtoken: " + authtoken); + Integer authtime = ((Map) payload).get("authtime"); + System.out.println("authtime: " + authtime); + return authtoken; + } else if (textcode.equals("LOGIN_FAILED")) { + String desc = ((Map) resp).get("desc"); + System.out.println("desc: "+desc); + if (!TextUtils.isEmpty(desc)) { + throw new Exception(desc); + } + } + + throw new Exception("Login failed"); + } +} diff --git a/Open-ILS/src/AndroidAuthenticator/.classpath b/Open-ILS/src/AndroidAuthenticator/.classpath index 993434283f..51769745b2 100644 --- a/Open-ILS/src/AndroidAuthenticator/.classpath +++ b/Open-ILS/src/AndroidAuthenticator/.classpath @@ -1,8 +1,8 @@ - - + + diff --git a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/Authenticator.java b/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/Authenticator.java deleted file mode 100644 index db0b242bed..0000000000 --- a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/Authenticator.java +++ /dev/null @@ -1,114 +0,0 @@ -package org.evergreen_ils.auth; - -import android.accounts.AbstractAccountAuthenticator; -import android.accounts.Account; -import android.accounts.AccountAuthenticatorResponse; -import android.accounts.AccountManager; -import android.accounts.NetworkErrorException; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; -import android.text.TextUtils; -import android.util.Log; - -public class Authenticator extends AbstractAccountAuthenticator { - - private final String TAG = "eg.auth"; - private Context context; - - public Authenticator(Context context) { - super(context); - this.context = context; - } - - @Override - public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException { - Log.d(TAG, "addaccount "+accountType+" "+authTokenType); - final Intent intent = new Intent(context, AuthenticatorActivity.class); - intent.putExtra(Const.AUTHTOKEN_TYPE, authTokenType); - intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); - - Bundle result = new Bundle(); - result.putParcelable(AccountManager.KEY_INTENT, intent); - return result; - } - - @Override - public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { - Log.d(TAG, "getAuthToken "+account.name); - - // If the caller requested an authToken type we don't support, then - // return an error - if (!authTokenType.equals(Const.AUTHTOKEN_TYPE)) { - final Bundle result = new Bundle(); - result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType"); - return result; - } - - final AccountManager am = AccountManager.get(context); - String authToken = am.peekAuthToken(account, authTokenType); - Log.d(TAG, "peekAuthToken returned " + authToken); - if (TextUtils.isEmpty(authToken)) { - final String password = am.getPassword(account); - if (password != null) { - try { - Log.d(TAG, "attempting to sign in with existing password"); - authToken = EvergreenAuthenticate.signIn(context, account.name, password); - } catch (Exception e) { - Log.d(TAG, "caught exception "+e.getMessage()); - final Bundle result = new Bundle(); - result.putString(AccountManager.KEY_ERROR_MESSAGE, e.getMessage()); - return result; - } - } - } - - // If we get an authToken - we return it - if (!TextUtils.isEmpty(authToken)) { - final Bundle result = new Bundle(); - result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); - result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); - result.putString(AccountManager.KEY_AUTHTOKEN, authToken); - return result; - } - - // If we get here, then we couldn't access the user's password - so we - // need to re-prompt them for their credentials. We do that by creating - // an intent to display our AuthenticatorActivity. - final Intent intent = new Intent(context, AuthenticatorActivity.class); - intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response); - intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, account.type); - intent.putExtra(AuthenticatorActivity.ARG_AUTH_TYPE, authTokenType); - intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_NAME, account.name); - final Bundle bundle = new Bundle(); - bundle.putParcelable(AccountManager.KEY_INTENT, intent); - return bundle; - } - - @Override - public String getAuthTokenLabel(String authTokenType) { - return Const.AUTHTOKEN_TYPE_LABEL; - } - - @Override - public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException { - final Bundle result = new Bundle(); - result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); - return result; - } - - @Override - public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { - return null; - } - - @Override - public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException { - return null; - } - - @Override - public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException { - return null; - } -} diff --git a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/AuthenticatorActivity.java b/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/AuthenticatorActivity.java deleted file mode 100644 index 24355614bf..0000000000 --- a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/AuthenticatorActivity.java +++ /dev/null @@ -1,156 +0,0 @@ -package org.evergreen_ils.auth; - -import android.accounts.AccountAuthenticatorActivity; -import android.accounts.AccountManager; -import android.accounts.Account; -import android.content.Intent; -import android.os.AsyncTask; -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.widget.TextView; -import android.widget.Toast; - -public class AuthenticatorActivity extends AccountAuthenticatorActivity { - - private final String TAG = "eg.auth"; - - public final static String ARG_ACCOUNT_TYPE = "ACCOUNT_TYPE"; - public final static String ARG_AUTH_TYPE = "AUTH_TYPE"; - public final static String ARG_ACCOUNT_NAME = "ACCOUNT_NAME"; - public final static String ARG_IS_ADDING_NEW_ACCOUNT = "IS_ADDING_ACCOUNT"; - - public static final String KEY_ERROR_MESSAGE = "ERR_MSG"; - - public final static String PARAM_USER_PASS = "USER_PASS"; - - private final int REQ_SIGNUP = 1; - - private AccountManager accountManager; - private String authTokenType; - - /** - * Called when the activity is first created. - */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_login); - accountManager = AccountManager.get(getBaseContext()); - - String accountName = getIntent().getStringExtra(ARG_ACCOUNT_NAME); - authTokenType = getIntent().getStringExtra(ARG_AUTH_TYPE); - if (authTokenType == null) - authTokenType = Const.AUTHTOKEN_TYPE; - - if (accountName != null) { - ((TextView) findViewById(R.id.accountName)).setText(accountName); - } - - findViewById(R.id.submit).setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - submit(); - } - }); - /* - * findViewById(R.id.signUp).setOnClickListener(new - * View.OnClickListener() { - * - * @Override public void onClick(View v) { // Since there can only be - * one AuthenticatorActivity, we call the sign up activity, get his - * results, // and return them in setAccountAuthenticatorResult(). See - * finishLogin(). Intent signup = new Intent(getBaseContext(), - * SignUpActivity.class); signup.putExtras(getIntent().getExtras()); - * startActivityForResult(signup, REQ_SIGNUP); } }); - */ - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - - // The sign up activity returned that the user has successfully created - // an account - if (requestCode == REQ_SIGNUP && resultCode == RESULT_OK) { - finishLogin(data); - } else - super.onActivityResult(requestCode, resultCode, data); - } - - public void submit() { - - final String username = ((TextView) findViewById(R.id.accountName)).getText().toString(); - final String password = ((TextView) findViewById(R.id.accountPassword)).getText().toString(); - //final String account_type = getIntent().getStringExtra(ARG_ACCOUNT_TYPE); - - new AsyncTask() { - - @Override - protected Intent doInBackground(String... params) { - - Log.d(TAG, "Started authenticating"); - - String authtoken = null; - Bundle data = new Bundle(); - try { - authtoken = EvergreenAuthenticate.signIn(AuthenticatorActivity.this, username, password); - - data.putString(AccountManager.KEY_ACCOUNT_NAME, username); - data.putString(AccountManager.KEY_ACCOUNT_TYPE, Const.ACCOUNT_TYPE); - data.putString(AccountManager.KEY_AUTHTOKEN, authtoken); - data.putString(PARAM_USER_PASS, password); - - } catch (Exception e) { - data.putString(KEY_ERROR_MESSAGE, e.getMessage()); - } - - final Intent res = new Intent(); - res.putExtras(data); - return res; - } - - @Override - protected void onPostExecute(Intent intent) { - if (intent.hasExtra(KEY_ERROR_MESSAGE)) { - Toast.makeText(getBaseContext(), - intent.getStringExtra(KEY_ERROR_MESSAGE), - Toast.LENGTH_SHORT).show(); - } else { - finishLogin(intent); - } - } - }.execute(); - } - - private void finishLogin(Intent intent) { - Log.d(TAG, "finishLogin"); - - String accountName = intent - .getStringExtra(AccountManager.KEY_ACCOUNT_NAME); - String accountPassword = intent.getStringExtra(PARAM_USER_PASS); - final Account account = new Account(accountName, - intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE)); - - if (getIntent().getBooleanExtra(ARG_IS_ADDING_NEW_ACCOUNT, false)) { - Log.d(TAG, "finishLogin > addAccountExplicitly"); - String authtoken = intent - .getStringExtra(AccountManager.KEY_AUTHTOKEN); - String authtokenType = authTokenType; - - // Creating the account on the device and setting the auth token we - // got - // (Not setting the auth token will cause another call to the server - // to authenticate the user) - accountManager.addAccountExplicitly(account, accountPassword, null); - accountManager.setAuthToken(account, authtokenType, authtoken); - } else { - Log.d(TAG, "finishLogin > setPassword"); - accountManager.setPassword(account, accountPassword); - } - - setAccountAuthenticatorResult(intent.getExtras()); - setResult(RESULT_OK, intent); - finish(); - } -} diff --git a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/AuthenticatorService.java b/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/AuthenticatorService.java deleted file mode 100644 index 3a54fe86c3..0000000000 --- a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/AuthenticatorService.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.evergreen_ils.auth; - -import org.evergreen_ils.auth.Authenticator; - -import android.app.Service; -import android.content.Intent; -import android.os.IBinder; - -public class AuthenticatorService extends Service { - @Override - public IBinder onBind(Intent arg0) { - return new Authenticator(this).getIBinder(); - } -} diff --git a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/Const.java b/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/Const.java deleted file mode 100644 index 0f114a6e60..0000000000 --- a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/Const.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.evergreen_ils.auth; - -public class Const { - public static final String ACCOUNT_TYPE = "org.evergreen-ils.opac"; - public static final String AUTHTOKEN_TYPE = "opac"; - public static final String AUTHTOKEN_TYPE_LABEL = "Online Public Access Catalog"; -} diff --git a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/EvergreenAuthenticate.java b/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/EvergreenAuthenticate.java deleted file mode 100644 index 3831d57076..0000000000 --- a/Open-ILS/src/AndroidAuthenticator/src/org/evergreen_ils/auth/EvergreenAuthenticate.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.evergreen_ils.auth; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.HashMap; -import java.util.Map; - -import org.opensrf.Method; -import org.opensrf.net.http.GatewayRequest; -import org.opensrf.net.http.HttpConnection; -import org.opensrf.net.http.HttpRequest; - -import android.content.Context; -import android.text.TextUtils; -import android.util.Log; - -public class EvergreenAuthenticate { - private final static String TAG = "eg.auth"; - public final static String SERVICE_AUTH = "open-ils.auth"; - public final static String METHOD_AUTH_INIT = "open-ils.auth.authenticate.init"; - public final static String METHOD_AUTH_COMPLETE = "open-ils.auth.authenticate.complete"; - - private static String md5(String s) { - try { - MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); - digest.update(s.getBytes()); - byte messageDigest[] = digest.digest(); - - // Create Hex String - StringBuffer hexString = new StringBuffer(); - for (int i = 0; i < messageDigest.length; i++) { - String hex = Integer.toHexString(0xFF & messageDigest[i]); - if (hex.length() == 1) { - // could use a for loop, but we're only dealing with a - // single byte - hexString.append('0'); - } - hexString.append(hex); - } - return hexString.toString(); - - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - - return ""; - } - - public static Object doRequest(HttpConnection conn, String service, String methodName, Object[] params) throws Exception { - Method method = new Method(methodName); - - Log.d(TAG, "doRequest Method :" + methodName + ":"); - for (int i = 0; i < params.length; i++) { - method.addParam(params[i]); - Log.d(TAG, "Param " + i + ": " + params[i]); - } - - // sync request - HttpRequest req = new GatewayRequest(conn, service, method).send(); - Object resp; - - while ((resp = req.recv()) != null) { - Log.d(TAG, "Sync Response: " + resp); - Object response = (Object) resp; - return response; - } - return null; - } - - @SuppressWarnings("unchecked") - public static String signIn(Context context, String username, String password) throws Exception { - Log.d(TAG, "signIn "+username); - - HttpConnection conn = new HttpConnection(context.getString(R.string.gateway_url)); - - // step 1: get seed - Object resp = doRequest(conn, SERVICE_AUTH, METHOD_AUTH_INIT, new Object[] { username }); - if (resp == null) - throw new Exception("Unable to contact login service"); - String seed = resp.toString(); - - // step 2: complete auth with seed + password - HashMap complexParam = new HashMap(); - complexParam.put("type", "opac"); - complexParam.put("username", username); - complexParam.put("password", md5(seed + md5(password))); - resp = doRequest(conn, SERVICE_AUTH, METHOD_AUTH_COMPLETE, new Object[] { complexParam }); - if (resp == null) - throw new Exception("Unable to complete login"); - - // parse response - String textcode = ((Map) resp).get("textcode"); - System.out.println("textcode: " + textcode); - if (textcode.equals("SUCCESS")) { - Object payload = ((Map) resp).get("payload"); - System.out.println("payload: " + payload); - String authtoken = ((Map) payload).get("authtoken"); - System.out.println("authtoken: " + authtoken); - Integer authtime = ((Map) payload).get("authtime"); - System.out.println("authtime: " + authtime); - return authtoken; - } else if (textcode.equals("LOGIN_FAILED")) { - String desc = ((Map) resp).get("desc"); - System.out.println("desc: "+desc); - if (!TextUtils.isEmpty(desc)) { - throw new Exception(desc); - } - } - - throw new Exception("Login failed"); - } -}