--- /dev/null
+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;
+ }
+}
--- /dev/null
+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<String, Void, Intent>() {
+
+ @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();
+ }
+}
--- /dev/null
+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();
+ }
+}
--- /dev/null
+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";
+}
--- /dev/null
+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<String, String> complexParam = new HashMap<String, String>();
+ 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<String, String>) resp).get("textcode");
+ System.out.println("textcode: " + textcode);
+ if (textcode.equals("SUCCESS")) {
+ Object payload = ((Map<String, String>) resp).get("payload");
+ System.out.println("payload: " + payload);
+ String authtoken = ((Map<String, String>) payload).get("authtoken");
+ System.out.println("authtoken: " + authtoken);
+ Integer authtime = ((Map<String, Integer>) payload).get("authtime");
+ System.out.println("authtime: " + authtime);
+ return authtoken;
+ } else if (textcode.equals("LOGIN_FAILED")) {
+ String desc = ((Map<String, String>) resp).get("desc");
+ System.out.println("desc: "+desc);
+ if (!TextUtils.isEmpty(desc)) {
+ throw new Exception(desc);
+ }
+ }
+
+ throw new Exception("Login failed");
+ }
+}
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
- <classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
- <classpathentry kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
+ <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="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="output" path="bin/classes"/>
+++ /dev/null
-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;
- }
-}
+++ /dev/null
-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<String, Void, Intent>() {
-
- @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();
- }
-}
+++ /dev/null
-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();
- }
-}
+++ /dev/null
-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";
-}
+++ /dev/null
-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<String, String> complexParam = new HashMap<String, String>();
- 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<String, String>) resp).get("textcode");
- System.out.println("textcode: " + textcode);
- if (textcode.equals("SUCCESS")) {
- Object payload = ((Map<String, String>) resp).get("payload");
- System.out.println("payload: " + payload);
- String authtoken = ((Map<String, String>) payload).get("authtoken");
- System.out.println("authtoken: " + authtoken);
- Integer authtime = ((Map<String, Integer>) payload).get("authtime");
- System.out.println("authtime: " + authtime);
- return authtoken;
- } else if (textcode.equals("LOGIN_FAILED")) {
- String desc = ((Map<String, String>) resp).get("desc");
- System.out.println("desc: "+desc);
- if (!TextUtils.isEmpty(desc)) {
- throw new Exception(desc);
- }
- }
-
- throw new Exception("Login failed");
- }
-}