Skip to content

Commit

Permalink
Reverting the Android core change back to 1.+
Browse files Browse the repository at this point in the history
  • Loading branch information
rotembr committed Aug 19, 2016
1 parent 1c90b7c commit ba0200e
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 311 deletions.
16 changes: 8 additions & 8 deletions src/android/CDVMFPAuthenticationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ibm.mobilefirstplatform.clientsdk.android.core.api.BMSClient;
import com.ibm.mobilefirstplatform.clientsdk.android.logger.api.Logger;
import com.ibm.mobilefirstplatform.clientsdk.android.security.mca.api.AuthenticationContext;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.AuthenticationContext;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand All @@ -16,8 +16,8 @@
*/
public class CDVMFPAuthenticationContext extends CordovaPlugin {
private String errorEmptyArg = "Expected non-empty string argument.";
private static final Logger acLogger = Logger.getLogger(Logger.INTERNAL_PREFIX + "CDVMFPAuthContext");
private static final Logger acLogger = Logger.getInstance(Logger.INTERNAL_PREFIX + "CDVMFPAuthContext");

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
boolean ans = true;
Expand All @@ -31,9 +31,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
ans = false;
}
return ans;

}

/**
* Use the native SDK API to submits authentication challenge response.
*
Expand All @@ -57,7 +57,7 @@ public void run() {
}
});
}

/**
* Use the native SDK API to informs about authentication success.
*
Expand All @@ -80,7 +80,7 @@ public void run() {
}
});
}

/**
* Use the native SDK API to informs about authentication failure.
*
Expand All @@ -104,5 +104,5 @@ public void run() {
}
});
}

}
141 changes: 47 additions & 94 deletions src/android/CDVMFPAuthorizationManager.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
Copyright 2015 IBM Corp.
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.
*/
Copyright 2015 IBM Corp.
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 com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.core;

import android.content.Context;
Expand All @@ -18,11 +18,9 @@
import com.ibm.mobilefirstplatform.clientsdk.android.logger.api.*;

import com.ibm.mobilefirstplatform.clientsdk.android.security.api.AuthorizationManager;
import com.ibm.mobilefirstplatform.clientsdk.android.security.mca.api.MCAAuthorizationManager;

import com.ibm.mobilefirstplatform.clientsdk.android.security.api.AppIdentity;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.DeviceIdentity;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.UserIdentity;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.identity.AppIdentity;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.identity.DeviceIdentity;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.identity.UserIdentity;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
Expand All @@ -31,27 +29,20 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Created by rotembr on 10/22/15.
*/
public class CDVMFPAuthorizationManager extends CordovaPlugin {
private static final Logger amLogger = Logger.getLogger(Logger.INTERNAL_PREFIX + "CDVMFPAuthorizationManager");

private static final Logger amLogger = Logger.getInstance(Logger.INTERNAL_PREFIX + "CDVMFPAuthorizationManager");

private static final String PersistencePolicyAlways = "ALWAYS";
private static final String PersistencePolicyNever = "NEVER";

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
boolean ans = true;
if ("initialize".equals(action)) {
this.initialize(args, callbackContext);
} else if ("obtainAuthorizationHeader".equals(action)) {
if ("obtainAuthorizationHeader".equals(action)) {
this.obtainAuthorizationHeader(callbackContext);
} else if ("clearAuthorizationData".equals(action)) {
this.clearAuthorizationData(callbackContext);
Expand All @@ -76,55 +67,21 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
}
return ans;
}



/**
* Use the native SDK API to initialize the authorization manager with tenantId.
*
* @param args JSONArray that contains the MCA service tenantId.
* @param callbackContext
*/
private void initialize(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
final Context currentContext = this.cordova.getActivity();
cordova.getThreadPool().execute(new Runnable() {
public void run() {
String tenantId = null;
String region = null;
try {
tenantId = args.getString(0);
if(args.length() > 1) { // in case we get region as well
region = args.getString(1);
}
} catch (JSONException e) {
e.printStackTrace();
amLogger.error("Error in parsing input args");
callbackContext.error("Cant parse the input args as String.");
}

if(region != null){
MCAAuthorizationManager.createInstance(currentContext, tenantId, region);
amLogger.debug("Authorization Manager initialize with tenantId: " + tenantId.toString() + " and region: " + region.toString());
}else{
MCAAuthorizationManager.createInstance(currentContext, tenantId);
amLogger.debug("Authorization Manager initialize with tenantId: " + tenantId.toString());
}
callbackContext.success();
}
});
}


/**
* Use the native SDK API to invoke process for obtaining authorization header.
* @param callbackContext Callback that will indicate whether the request succeeded or failed
*/
private void obtainAuthorizationHeader(final CallbackContext callbackContext) throws JSONException {

final Context currentContext = this.cordova.getActivity();

cordova.getThreadPool().execute(new Runnable() {
public void run() {
MCAAuthorizationManager.getInstance().obtainAuthorization(currentContext, new ResponseListener() {

AuthorizationManager.getInstance().obtainAuthorizationHeader(currentContext, new ResponseListener() {
@Override
public void onSuccess(Response response) {
try {
Expand All @@ -135,7 +92,7 @@ public void onSuccess(Response response) {
callbackContext.error(e.getMessage());
}
}

@Override
public void onFailure(Response failResponse, Throwable t, JSONObject extendedInfo) {
try {
Expand All @@ -147,10 +104,10 @@ public void onFailure(Response failResponse, Throwable t, JSONObject extendedInf
}
}
});

}
});

}
/**
* Use the native SDK API to clear the local stored authorization data.
Expand All @@ -159,31 +116,27 @@ public void onFailure(Response failResponse, Throwable t, JSONObject extendedInf
private void clearAuthorizationData(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
MCAAuthorizationManager.getInstance().clearAuthorizationData();
AuthorizationManager.getInstance().clearAuthorizationData();
amLogger.debug("Authorization data cleared.");
callbackContext.success();
}
});
}

/**
* Use the native SDK API to check if the params came from response that requires authorization.
*
* @param args JSONArray that contains the statusCode of the response, and responseAuthorizationHeader.
* @param callbackContext Callback that will get the result,true if status is 401 or 403 and The value of the header contains 'Bearer' AND 'realm="imfAuthentication"'
*/
private void isAuthorizationRequired(final JSONArray args, final CallbackContext callbackContext) throws JSONException {

cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
int statusCode = args.getInt(0);
String responseAuthorizationHeader = args.getString(1);
Map<String,List<String>> headers = new HashMap<String, List<String>>();
List<String> authorizationHeader = new LinkedList<String>();
authorizationHeader.add(responseAuthorizationHeader);
headers.put(AuthorizationManager.WWW_AUTHENTICATE_HEADER_NAME,authorizationHeader);
boolean answer = MCAAuthorizationManager.getInstance().isAuthorizationRequired(statusCode, headers);
boolean answer = AuthorizationManager.getInstance().isAuthorizationRequired(statusCode, responseAuthorizationHeader);
amLogger.debug("isAuthorizationRequired return " + answer);
callbackContext.success(String.valueOf(answer));
} catch (JSONException e) {
Expand All @@ -193,7 +146,7 @@ public void run() {
}
});
}

/**
* Use the native SDK API get the locally stored authorization header or null if the value is not exist.
*
Expand All @@ -202,13 +155,13 @@ public void run() {
private void getCachedAuthorizationHeader(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
String header = MCAAuthorizationManager.getInstance().getCachedAuthorizationHeader();
String header = AuthorizationManager.getInstance().getCachedAuthorizationHeader();
amLogger.debug("Cached authorization header: " + header);
callbackContext.success(header);
}
});
}

/**
* Use the native SDK API to get the current authorization persistence policy
*
Expand All @@ -217,13 +170,13 @@ public void run() {
private void getAuthorizationPersistencePolicy(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
MCAAuthorizationManager.PersistencePolicy policy = MCAAuthorizationManager.getInstance().getAuthorizationPersistencePolicy();
AuthorizationManager.PersistencePolicy policy = AuthorizationManager.getInstance().getAuthorizationPersistencePolicy();
amLogger.debug("PersistencePolicy:" + policy.toString());
callbackContext.success(policy.toString());
}
});
}

/**
* Use the native SDK API to change the sate of the current authorization persistence policy.
*
Expand All @@ -241,23 +194,23 @@ public void run() {
}
boolean success = true;
if (newPolicy.equals(PersistencePolicyNever)) {
MCAAuthorizationManager.getInstance().setAuthorizationPersistencePolicy(MCAAuthorizationManager.PersistencePolicy.NEVER);
AuthorizationManager.getInstance().setAuthorizationPersistencePolicy(AuthorizationManager.PersistencePolicy.NEVER);
} else if (newPolicy.equals(PersistencePolicyAlways)) {
MCAAuthorizationManager.getInstance().setAuthorizationPersistencePolicy(MCAAuthorizationManager.PersistencePolicy.ALWAYS);
AuthorizationManager.getInstance().setAuthorizationPersistencePolicy(AuthorizationManager.PersistencePolicy.ALWAYS);
} else {
success = false;
amLogger.debug("Policy cann't be recognized:" + newPolicy.toString());
callbackContext.error("The specified persistence policy is not supported.");
}

if (success) {
amLogger.debug("PersistencePolicy set to:" + newPolicy.toString());
callbackContext.success();
}
}
});
}

/**
* Use the native SDK API to get the authorized user identity.
*
Expand All @@ -266,13 +219,13 @@ public void run() {
private void getUserIdentity(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
UserIdentity userIdentity = MCAAuthorizationManager.getInstance().getUserIdentity();
UserIdentity userIdentity = AuthorizationManager.getInstance().getUserIdentity();
amLogger.debug("userIdentity: " + userIdentity.toString());
callbackContext.success(userIdentity.toString());
}
});
}

/**
* Use the native SDK API to get the application identity.
*
Expand All @@ -281,13 +234,13 @@ public void run() {
private void getAppIdentity(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
AppIdentity appIdentity = MCAAuthorizationManager.getInstance().getAppIdentity();
AppIdentity appIdentity = AuthorizationManager.getInstance().getAppIdentity();
amLogger.debug("appIdentity: " + appIdentity.toString());
callbackContext.success(appIdentity.toString());
}
});
}

/**
* Use the native SDK API to get the device identity.
*
Expand All @@ -296,7 +249,7 @@ public void run() {
private void getDeviceIdentity(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
DeviceIdentity deviceIdentity = MCAAuthorizationManager.getInstance().getDeviceIdentity();
DeviceIdentity deviceIdentity = AuthorizationManager.getInstance().getDeviceIdentity();
amLogger.debug("deviceIdentity: " + deviceIdentity.toString());
callbackContext.success(deviceIdentity.toString());
}
Expand All @@ -314,7 +267,7 @@ private void logout(final CallbackContext callbackContext) throws JSONException
cordova.getThreadPool().execute(new Runnable() {
public void run() {

MCAAuthorizationManager.getInstance().logout(currentContext, new ResponseListener() {
AuthorizationManager.getInstance().logout(currentContext, new ResponseListener() {
@Override
public void onSuccess(Response response) {
try {
Expand Down
Loading

0 comments on commit ba0200e

Please sign in to comment.