Skip to content

Commit

Permalink
Merge branch 'master' of github.com:AzureAD/azure-activedirectory-lib…
Browse files Browse the repository at this point in the history
…rary-for-android
  • Loading branch information
omercs committed Oct 22, 2014
2 parents ea58c61 + d872ffb commit 91e7e09
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 26 deletions.
29 changes: 27 additions & 2 deletions src/src/com/microsoft/aad/adal/AuthenticationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ protected void onCreate(Bundle savedInstanceState) {
mCallingUID = info.getUIDForPackage(mCallingPackage);
String signatureDigest = info.getCurrentSignatureForPackage(mCallingPackage);
mStartUrl = getBrokerStartUrl(mStartUrl, mCallingPackage, signatureDigest);
mRedirectUrl = PackageHelper.getBrokerRedirectUrl(mCallingPackage, signatureDigest);

if (!isCallerBrokerInstaller()) {
Logger.v(TAG, "Caller needs to be verified using special redirectUri");
mRedirectUrl = PackageHelper.getBrokerRedirectUrl(mCallingPackage, signatureDigest);
}
Logger.v(TAG,
"OnCreate redirectUrl:" + mRedirectUrl + " startUrl:" + mStartUrl
+ " calling package:" + mCallingPackage + " signatureDigest:"
Expand All @@ -277,6 +281,27 @@ public void run() {
}
}

private boolean isCallerBrokerInstaller() {
// Allow intune's signature check
PackageHelper info = new PackageHelper(AuthenticationActivity.this);
String packageName = getCallingPackage();
if (!StringExtensions.IsNullOrBlank(packageName)) {

if (packageName.equals(AuthenticationSettings.INSTANCE.getBrokerPackageName())) {
Logger.v(TAG, "isCallerBrokerInstaller: same package as broker " + packageName);
return true;
}

String signature = info.getCurrentSignatureForPackage(packageName);
Logger.v(TAG, "isCallerBrokerInstaller: Check signature for " + packageName
+ " signature:" + signature + " brokerSignature:"
+ AuthenticationSettings.INSTANCE.getBrokerSignature());
return signature.equals(AuthenticationSettings.INSTANCE.getBrokerSignature());
}

return false;
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down Expand Up @@ -505,7 +530,7 @@ protected void onResume() {
mSpinner.setMessage(this.getText(this.getResources().getIdentifier("app_loading", "string",
this.getPackageName())));
}

@Override
protected void onRestart() {
Logger.d(TAG, "AuthenticationActivity onRestart");
Expand Down
4 changes: 2 additions & 2 deletions src/src/com/microsoft/aad/adal/AuthenticationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private void initialize(Context appContext, String authority, ITokenCacheStore t
throw new IllegalArgumentException("authority");
}
mBrokerProxy = new BrokerProxy(appContext);
if (!defaultCache && mBrokerProxy.canSwitchToBroker()) {
if (!defaultCache && !mBrokerProxy.canUseLocalCache()) {
throw new UnsupportedOperationException("Local cache is not supported for broker usage");
}
mContext = appContext;
Expand Down Expand Up @@ -1765,6 +1765,6 @@ public static String getVersionName() {
// Package manager does not report for ADAL
// AndroidManifest files are not merged, so it is returning hard coded
// value
return "1.0.3";
return "1.0.4";
}
}
19 changes: 19 additions & 0 deletions src/src/com/microsoft/aad/adal/BrokerProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,29 @@ public BrokerProxy(final Context ctx) {
*/
@Override
public boolean canSwitchToBroker() {
String packageName = mContext.getPackageName();
return !AuthenticationSettings.INSTANCE.getSkipBroker() && verifyManifestPermissions()
&& !packageName.equalsIgnoreCase(AuthenticationSettings.INSTANCE.getBrokerPackageName())
&& verifyAuthenticator(mAcctManager) && verifyAccount();
}

@Override
public boolean canUseLocalCache(){
boolean brokerSwitch = canSwitchToBroker();
if(!brokerSwitch){
Logger.v(TAG, "It does not use broker");
return true;
}

String packageName = mContext.getPackageName();
if(verifySignature(packageName)){
Logger.v(TAG, "Broker installer can use local cache");
return true;
}

return false;
}

private boolean verifyAccount() {
Logger.v(TAG, "Verify account count");
// only call authenticator if there is an account
Expand Down
2 changes: 2 additions & 0 deletions src/src/com/microsoft/aad/adal/IBrokerProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface IBrokerProxy {
* Account manager
*/
boolean canSwitchToBroker();

boolean canUseLocalCache();

void removeAccounts();

Expand Down
34 changes: 30 additions & 4 deletions src/src/com/microsoft/aad/adal/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,20 @@ public void setExternalLogger(ILogger customLogger) {
this.mExternalLogger = customLogger;
}

private static String addVersion(String message) {
if(message != null){
return message + " ver:" + AuthenticationContext.getVersionName();
}

return " ver:" + AuthenticationContext.getVersionName();
}

public void debug(String tag, String message) {
if (mLogLevel.compareTo(LogLevel.Debug) < 0 || StringExtensions.IsNullOrBlank(message))
if (mLogLevel.compareTo(LogLevel.Debug) < 0 || StringExtensions.IsNullOrBlank(message)) {
return;
}

message = addVersion(message);

if (mAndroidLogEnabled) {
Log.d(tag, message);
Expand All @@ -126,13 +137,16 @@ public void debug(String tag, String message) {
}

public void verbose(String tag, String message, String additionalMessage, ADALError errorCode) {
if (mLogLevel.compareTo(LogLevel.Verbose) < 0)
if (mLogLevel.compareTo(LogLevel.Verbose) < 0) {
return;
}

if (mAndroidLogEnabled) {
Log.v(tag, getLogMessage(message, additionalMessage, errorCode));
}

message = addVersion(message);

if (mExternalLogger != null) {
try {
mExternalLogger.Log(tag, message, additionalMessage, LogLevel.Verbose, errorCode);
Expand All @@ -144,13 +158,16 @@ public void verbose(String tag, String message, String additionalMessage, ADALEr
}

public void inform(String tag, String message, String additionalMessage, ADALError errorCode) {
if (mLogLevel.compareTo(LogLevel.Info) < 0)
if (mLogLevel.compareTo(LogLevel.Info) < 0) {
return;
}

if (mAndroidLogEnabled) {
Log.i(tag, getLogMessage(message, additionalMessage, errorCode));
}

message = addVersion(message);

if (mExternalLogger != null) {
try {
mExternalLogger.Log(tag, message, additionalMessage, LogLevel.Info, errorCode);
Expand All @@ -162,13 +179,16 @@ public void inform(String tag, String message, String additionalMessage, ADALErr
}

public void warn(String tag, String message, String additionalMessage, ADALError errorCode) {
if (mLogLevel.compareTo(LogLevel.Warn) < 0)
if (mLogLevel.compareTo(LogLevel.Warn) < 0) {
return;
}

if (mAndroidLogEnabled) {
Log.w(tag, getLogMessage(message, additionalMessage, errorCode));
}

message = addVersion(message);

if (mExternalLogger != null) {
try {
mExternalLogger.Log(tag, message, additionalMessage, LogLevel.Warn, errorCode);
Expand All @@ -184,6 +204,8 @@ public void error(String tag, String message, String additionalMessage, ADALErro
Log.e(tag, getLogMessage(message, additionalMessage, errorCode));
}

message = addVersion(message);

if (mExternalLogger != null) {
try {
mExternalLogger.Log(tag, message, additionalMessage, LogLevel.Error, errorCode);
Expand All @@ -200,6 +222,8 @@ public void error(String tag, String message, String additionalMessage, ADALErro
Log.e(tag, getLogMessage(message, additionalMessage, errorCode), err);
}

message = addVersion(message);

if (mExternalLogger != null) {
try {
mExternalLogger.Log(tag, message, additionalMessage, LogLevel.Error, errorCode);
Expand All @@ -217,11 +241,13 @@ private static String getLogMessage(String message, String additionalMessage,
msg.append(getCodeName(errorCode)).append(":");
}
if (message != null) {
message = addVersion(message);
msg.append(message);
}
if (additionalMessage != null) {
msg.append(" ").append(additionalMessage);
}

return msg.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public void testReceiver() throws NoSuchFieldException, IllegalArgumentException
// Test onReceive call with correct request id
signal.await(CONTEXT_REQUEST_TIME_OUT, TimeUnit.MILLISECONDS);
assertTrue("log the message for correct Intent",
response.message.equals(broadcastCancelMsg1));
response.message.startsWith(broadcastCancelMsg1));

// update requestId to match the AuthenticationRequest
final CountDownLatch signal2 = new CountDownLatch(1);
Expand All @@ -521,7 +521,7 @@ public void testReceiver() throws NoSuchFieldException, IllegalArgumentException
// verify that it received intent
signal2.await(CONTEXT_REQUEST_TIME_OUT, TimeUnit.MILLISECONDS);
assertTrue("log the message for correct Intent",
response2.message.equals(broadcastCancelMsg2));
response2.message.startsWith(broadcastCancelMsg2));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testServerInvalidJsonResponse() throws ClassNotFoundException,

assertNull("Exception should not throw", response.exception);
assertFalse("not valid instance", response.result);
assertTrue("Exception msg is logged", logTrack.message.equals("Json parsing error"));
assertTrue("Exception msg is logged", logTrack.message.startsWith("Json parsing error"));
}

public void testIsValidAuthorityNegative_InvalidUrl() throws MalformedURLException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void testLoadingFromInvalidCacheFile() {
Logger.getInstance().setExternalLogger(logger);
ITokenCacheStore store = new FileTokenCacheStore(targetContex, FILE_DEFAULT_NAME);

assertEquals("Verify message", "Existing cache format is wrong", logger.logMessage);
assertEquals("Verify message ", "Existing cache format is wrong ver:" + AuthenticationContext.getVersionName(), logger.logMessage);
}

public void testGetItem() {
Expand Down
25 changes: 13 additions & 12 deletions tests/Functional/src/com/microsoft/aad/adal/test/LoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.lang.reflect.Method;

import com.microsoft.aad.adal.ADALError;
import com.microsoft.aad.adal.AuthenticationContext;
import com.microsoft.aad.adal.AuthenticationSettings;
import com.microsoft.aad.adal.Logger;
import com.microsoft.aad.adal.Logger.ILogger;
import com.microsoft.aad.adal.Logger.LogLevel;
Expand Down Expand Up @@ -67,7 +69,7 @@ public void Log(String tag, String message, String additionalMessage, LogLevel l
Logger.d("test", "testmessage");

assertEquals("same log tag", "test", response.tag);
assertEquals("same log message", "testmessage", response.message);
assertTrue("same log message", response.message.startsWith("testmessage"));
response.reset();

// set to warn
Expand Down Expand Up @@ -101,7 +103,7 @@ public void Log(String tag, String message, String additionalMessage, LogLevel l
Logger.v(null, null, null, ADALError.AUTH_FAILED_BAD_STATE);

assertNull("null log tag", response.tag);
assertNull("null log message", response.message);
assertEquals(" ver:" + AuthenticationContext.getVersionName(), response.message);
assertNull("null log detail message", response.additionalMessage);
assertEquals("same log error code", ADALError.AUTH_FAILED_BAD_STATE, response.errorCode);
response.reset();
Expand All @@ -115,7 +117,7 @@ public void Log(String tag, String message, String additionalMessage, LogLevel l

Logger.d(null, "someMessage");
assertNull("null log tag since not logging this", response.tag);
assertEquals("null log message", "someMessage", response.message);
assertTrue("log message", response.message.startsWith("someMessage"));
assertNull("null log detail message", response.additionalMessage);
response.reset();

Expand All @@ -130,7 +132,7 @@ public void Log(String tag, String message, String additionalMessage, LogLevel l
Logger.w(null, null, null, ADALError.AUTH_FAILED_BAD_STATE);

assertNull("null log tag", response.tag);
assertNull("null log message", response.message);
assertNotNull("log message has version", response.message);
assertNull("null log detail message", response.additionalMessage);
assertEquals("same log error code", ADALError.AUTH_FAILED_BAD_STATE, response.errorCode);
response.reset();
Expand All @@ -139,7 +141,7 @@ public void Log(String tag, String message, String additionalMessage, LogLevel l
Logger.i(null, null, null, ADALError.AUTH_FAILED_BAD_STATE);

assertNull("null log tag", response.tag);
assertNull("null log message", response.message);
assertNotNull("log message", response.message);
assertNull("null log detail message", response.additionalMessage);
assertEquals("same log error code", ADALError.AUTH_FAILED_BAD_STATE, response.errorCode);
response.reset();
Expand All @@ -148,7 +150,7 @@ public void Log(String tag, String message, String additionalMessage, LogLevel l
Logger.e(null, null, null, ADALError.AUTH_FAILED_BAD_STATE);

assertNull("null log tag", response.tag);
assertNull("null log message", response.message);
assertNotNull("null log message", response.message);
assertNull("null log detail message", response.additionalMessage);
assertEquals("same log error code", ADALError.AUTH_FAILED_BAD_STATE, response.errorCode);
response.reset();
Expand All @@ -172,7 +174,7 @@ public void Log(String tag, String message, String additionalMessage, LogLevel l
Logger.v(null, "testMessage", null, ADALError.AUTH_FAILED_BAD_STATE);

assertTrue("Expected to come here", true);
assertEquals("same log message", "testMessage", response.message);
assertTrue("same log message", response.message.startsWith("testMessage"));
}

public void testLogMessage() throws IllegalArgumentException, ClassNotFoundException,
Expand All @@ -183,19 +185,18 @@ public void testLogMessage() throws IllegalArgumentException, ClassNotFoundExcep

String msg = (String)m.invoke(null, "logMsg", "logAdditionalMsg", ADALError.AUTH_FAILED);

assertEquals("Empty message is expected", ADALError.AUTH_FAILED.name()+":logMsg logAdditionalMsg", msg);
assertTrue("Verify msg", msg.startsWith(ADALError.AUTH_FAILED.name()));

msg = (String)m.invoke(null, "logMsg", null, ADALError.AUTH_FAILED);

assertEquals("Empty message is expected", ADALError.AUTH_FAILED.name()+":logMsg", msg);
assertTrue("Verify message", msg.startsWith(ADALError.AUTH_FAILED.name() + ":logMsg"));
}

private void verifyLogMessage(final TestLogResponse response) {
assertEquals("same log tag", "test", response.tag);
assertEquals("same log message", "testmessage", response.message);
assertTrue("same log message", response.message.startsWith("testmessage"));
assertEquals("same log detail message", "additionalMessage", response.additionalMessage);
assertEquals("same log error code", ADALError.AUTH_FAILED_BAD_STATE, response.errorCode);
response.reset();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public void testRefreshTokenWebResponseInvalidStatus() throws IllegalArgumentExc

// Verify that result returns null from this error
assertNull("Result is null", testResult.mResult);
assertEquals("Exception has same error message", TEST_RETURNED_EXCEPTION, response.message);
assertTrue("Exception has same error message", response.message.startsWith(TEST_RETURNED_EXCEPTION));
}

@SmallTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.CountDownLatch;

import com.microsoft.aad.adal.ADALError;
import com.microsoft.aad.adal.AuthenticationContext;
import com.microsoft.aad.adal.Logger;
import com.microsoft.aad.adal.Logger.ILogger;
import com.microsoft.aad.adal.Logger.LogLevel;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void listenForLogMessage(final String msg, final CountDownLatch signal) {
public void Log(String tag, String message, String additionalMessage, LogLevel level,
ADALError errorCode) {

if (message.equals(msg)) {
if (message.equals(msg + " ver:" + AuthenticationContext.getVersionName())) {
response.tag = tag;
response.message = message;
response.additionalMessage = additionalMessage;
Expand Down

0 comments on commit 91e7e09

Please sign in to comment.