Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit aea07d4

Browse files
authored
Merge pull request #85 from ziccardi/AEROGEAR-7743
Changed naming of checks from SecurityCheck to DeviceCheck
2 parents 44a7583 + 7ab593e commit aea07d4

38 files changed

+398
-403
lines changed

Security/Security.Platform.Android/AndroidSecurityCheckFactory.cs renamed to Security/Security.Platform.Android/AndroidDeviceCheckFactory.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ namespace AeroGear.Mobile.Security
77
/// <summary>
88
/// Factory for Android security checks.
99
/// </summary>
10-
internal class AndroidSecurityCheckFactory : ISecurityCheckFactory
10+
internal class AndroidDeviceCheckFactory : IDeviceCheckFactory
1111
{
12-
public static readonly AndroidSecurityCheckFactory INSTANCE = new AndroidSecurityCheckFactory();
12+
public static readonly AndroidDeviceCheckFactory INSTANCE = new AndroidDeviceCheckFactory();
1313

1414
private readonly Context context;
1515

1616
/// <summary>
17-
/// Initializes a new instance of the <see cref="T:AeroGear.Mobile.Security.AndroidSecurityCheckFactory"/> class.
17+
/// Initializes a new instance of the <see cref="T:AeroGear.Mobile.Security.AndroidDeviceCheckFactory"/> class.
1818
/// </summary>
19-
public AndroidSecurityCheckFactory(Context ctx = null)
19+
public AndroidDeviceCheckFactory(Context ctx = null)
2020
{
2121
this.context = ctx != null ? ctx.ApplicationContext : Android.App.Application.Context;
2222
}
@@ -25,17 +25,17 @@ public AndroidSecurityCheckFactory(Context ctx = null)
2525
/// Returns an initialized instance of the check identified by the passed in pseudo enumeration.
2626
/// </summary>
2727
/// <returns>The initialized instance of the check.</returns>
28-
/// <param name="type">Type of the check to be instantiated. This must be an instance of <see cref="T:AeroGear.Mobile.Security.SecurityChecks"/></param>
29-
public ISecurityCheck create(ISecurityCheckType type)
28+
/// <param name="type">Type of the check to be instantiated. This must be an instance of <see cref="T:AeroGear.Mobile.Security.DeviceChecks"/></param>
29+
public IDeviceCheck create(IDeviceCheckType type)
3030
{
31-
SecurityChecks checkType = type as SecurityChecks;
31+
DeviceChecks checkType = type as DeviceChecks;
3232

3333
if (checkType == null)
3434
{
35-
throw new Exception("Passed in security check type is not supported");
35+
throw new Exception("Passed in device check type is not supported");
3636
}
3737

38-
return Activator.CreateInstance(checkType.CheckType, this.context) as ISecurityCheck;
38+
return Activator.CreateInstance(checkType.CheckType, this.context) as IDeviceCheck;
3939
}
4040

4141
/// <summary>
@@ -44,12 +44,12 @@ public ISecurityCheck create(ISecurityCheckType type)
4444
/// </summary>
4545
/// <returns>The initialized instance of the check.</returns>
4646
/// <param name="typeName">The name of the check to be instantiated.</param>
47-
public ISecurityCheck create(string typeName)
47+
public IDeviceCheck create(string typeName)
4848
{
49-
ISecurityCheckType securityCheckType = SecurityChecks.GetSecurityCheck(typeName);
49+
IDeviceCheckType securityCheckType = DeviceChecks.GetDeviceCheck(typeName);
5050
if (securityCheckType == null)
5151
{
52-
throw new Exception(String.Format("No security check with name {0} is known", typeName));
52+
throw new Exception(String.Format("No device check with name {0} is known", typeName));
5353
}
5454

5555
return create(securityCheckType);

Security/Security.Platform.Android/Checks/BackupDisallowedCheck.cs renamed to Security/Security.Platform.Android/Checks/BackupEnabledCheck.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ namespace AeroGear.Mobile.Security.Checks
77
/// <summary>
88
/// Check to determine whether the allowBackup flag is enabled for the application.
99
/// </summary>
10-
public class BackupDisallowedCheck : AbstractSecurityCheck
10+
public class BackupEnabledCheck : AbstractDeviceCheck
1111
{
1212
protected override string Name => "Backup Flag Check";
1313

1414
private readonly Context context;
1515

16-
public BackupDisallowedCheck(Context ctx)
16+
public BackupEnabledCheck(Context ctx)
1717
{
1818
this.context = ctx;
1919
}
2020

21-
public override SecurityCheckResult Check()
21+
public override DeviceCheckResult Check()
2222
{
2323
PackageInfo packageInfo = context.PackageManager.GetPackageInfo(context.PackageName, 0);
24-
bool disabled = (packageInfo.ApplicationInfo.Flags & ApplicationInfoFlags.AllowBackup) == 0;
25-
return new SecurityCheckResult(this, disabled);
24+
bool enabled = (packageInfo.ApplicationInfo.Flags & ApplicationInfoFlags.AllowBackup) != 0;
25+
return new DeviceCheckResult(this, enabled);
2626
}
2727
}
2828
}

Security/Security.Platform.Android/Checks/NoDebuggerCheck.cs renamed to Security/Security.Platform.Android/Checks/DebuggerEnabledCheck.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ namespace AeroGear.Mobile.Security.Checks
77
/// <summary>
88
/// A check for whether a debugger is attached to the current application.
99
/// </summary>
10-
public class NoDebuggerCheck : AbstractSecurityCheck
10+
public class DebuggerEnabledCheck : AbstractDeviceCheck
1111
{
1212
protected override string Name => "Debugger Check";
1313

1414
private readonly Context context;
1515

16-
public NoDebuggerCheck(Context ctx)
16+
public DebuggerEnabledCheck(Context ctx)
1717
{
1818
this.context = ctx;
1919
}
2020

21-
public override SecurityCheckResult Check()
21+
public override DeviceCheckResult Check()
2222
{
23-
return new SecurityCheckResult(this, !(Debug.IsDebuggerConnected || System.Diagnostics.Debugger.IsAttached));
23+
return new DeviceCheckResult(this, (Debug.IsDebuggerConnected || System.Diagnostics.Debugger.IsAttached));
2424
}
2525
}
2626
}

Security/Security.Platform.Android/Checks/DeveloperModeDisabledCheck.cs renamed to Security/Security.Platform.Android/Checks/DeveloperModeEnabledCheck.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ namespace AeroGear.Mobile.Security.Checks
77
/// <summary>
88
/// Security check that detects if developer mode is enabled in the device.
99
/// </summary>
10-
public class DeveloperModeDisabledCheck : AbstractSecurityCheck
10+
public class DeveloperModeEnabledCheck : AbstractDeviceCheck
1111
{
1212
protected override string Name => "Developer Mode Check";
1313

1414
private readonly Context context;
1515

16-
public DeveloperModeDisabledCheck(Context ctx)
16+
public DeveloperModeEnabledCheck(Context ctx)
1717
{
1818
this.context = ctx;
1919
}
2020

21-
public override SecurityCheckResult Check()
21+
public override DeviceCheckResult Check()
2222
{
2323
bool devModeEnabled = Settings.Global.GetInt(
2424
context.ContentResolver,
2525
Settings.Global.DevelopmentSettingsEnabled, 0) != 0;
26-
return new SecurityCheckResult(this, !devModeEnabled);
26+
return new DeviceCheckResult(this, devModeEnabled);
2727
}
2828
}
2929
}

Security/Security.Platform.Android/Checks/EncryptionCheck.cs renamed to Security/Security.Platform.Android/Checks/EncryptionEnabledCheck.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ namespace AeroGear.Mobile.Security.Checks
77
/// <summary>
88
/// Detects whether a devices filesystem is encrypted.
99
/// </summary>
10-
public class EncryptionCheck : AbstractSecurityCheck
10+
public class EncryptionEnabledCheck : AbstractDeviceCheck
1111
{
1212
protected override string Name => "Encryption Check";
1313

1414
private readonly Context context;
1515

16-
public EncryptionCheck(Context ctx)
16+
public EncryptionEnabledCheck(Context ctx)
1717
{
1818
this.context = ctx;
1919
}
2020

21-
public override SecurityCheckResult Check()
21+
public override DeviceCheckResult Check()
2222
{
2323
DevicePolicyManager policyManager = (DevicePolicyManager)context
2424
.GetSystemService(Context.DevicePolicyService);
2525
bool enabled = policyManager != null && policyManager
2626
.StorageEncryptionStatus == EncryptionStatus.Active;
27-
return new SecurityCheckResult(this, enabled);
27+
return new DeviceCheckResult(this, enabled);
2828
}
2929
}
3030
}

Security/Security.Platform.Android/Checks/NotInEmulatorCheck.cs renamed to Security/Security.Platform.Android/Checks/IsEmulatorCheck.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ namespace AeroGear.Mobile.Security.Checks
77
/// <summary>
88
/// A check for whether the device the application is running on an emulator.
99
/// </summary>
10-
public class NotInEmulatorCheck : AbstractSecurityCheck
10+
public class IsEmulatorCheck : AbstractDeviceCheck
1111
{
1212
protected override string Name => "Emulator Check";
1313

1414
private readonly Context context;
1515

16-
public NotInEmulatorCheck(Context ctx)
16+
public IsEmulatorCheck(Context ctx)
1717
{
1818
this.context = ctx;
1919
}
2020

21-
public override SecurityCheckResult Check()
21+
public override DeviceCheckResult Check()
2222
{
2323
if (Build.Fingerprint != null)
2424
{
2525
if (Build.Fingerprint.Contains("vbox") ||
2626
Build.Fingerprint.Contains("generic"))
27-
return new SecurityCheckResult(this, false);
27+
return new DeviceCheckResult(this, true);
2828
}
29-
return new SecurityCheckResult(this, true);
29+
return new DeviceCheckResult(this, false);
3030
}
3131
}
3232
}

Security/Security.Platform.Android/Checks/NonRootedCheck.cs renamed to Security/Security.Platform.Android/Checks/RootEnabledCheck.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace AeroGear.Mobile.Security.Checks
99
/// <summary>
1010
/// A check for whether the device the application is running on is rooted.
1111
/// </summary>
12-
public class NonRootedCheck : AbstractSecurityCheck
12+
public class RootEnabledCheck : AbstractDeviceCheck
1313
{
1414
protected override string Name => "Rooted Check";
1515

1616
private readonly Context context;
1717

18-
public NonRootedCheck(Context ctx)
18+
public RootEnabledCheck(Context ctx)
1919
{
2020
this.context = ctx;
2121
}
@@ -24,10 +24,10 @@ public NonRootedCheck(Context ctx)
2424
/// Solution found at https://stackoverflow.com/a/8097801.
2525
/// </summary>
2626
/// <returns>The check.</returns>
27-
public override SecurityCheckResult Check()
27+
public override DeviceCheckResult Check()
2828
{
2929
bool rooted = CheckForTestKeys() || CheckForSuBinary() || CheckSuExists();
30-
return new SecurityCheckResult(this, !rooted);
30+
return new DeviceCheckResult(this, rooted);
3131
}
3232

3333
/// <summary>

Security/Security.Platform.Android/Checks/ScreenLockCheck.cs renamed to Security/Security.Platform.Android/Checks/ScreenLockEnabledCheck.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ namespace AeroGear.Mobile.Security.Checks
77
/// <summary>
88
/// A check for whether the device the application is running on has a screen lock.
99
/// </summary>
10-
public class ScreenLockCheck : AbstractSecurityCheck
10+
public class ScreenLockEnabledCheck : AbstractDeviceCheck
1111
{
1212
protected override string Name => "Screen Lock Check";
1313

1414
private readonly Context context;
1515

16-
public ScreenLockCheck(Context ctx)
16+
public ScreenLockEnabledCheck(Context ctx)
1717
{
1818
this.context = ctx;
1919
}
2020

21-
public override SecurityCheckResult Check()
21+
public override DeviceCheckResult Check()
2222
{
2323
KeyguardManager manager = (KeyguardManager)context.GetSystemService(Context.KeyguardService);
24-
return new SecurityCheckResult(this, manager.IsDeviceSecure);
24+
return new DeviceCheckResult(this, manager.IsDeviceSecure);
2525
}
2626
}
2727
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using AeroGear.Mobile.Core.Utils;
4+
using AeroGear.Mobile.Security.Checks;
5+
6+
namespace AeroGear.Mobile.Security
7+
{
8+
/// <summary>
9+
/// This class enums all the provided security checks.
10+
///
11+
/// To get an instance of the check, use the following code:
12+
/// <code>
13+
/// <![CDATA[
14+
/// var deviceChek = ServiceFinder.Resolve<IDeviceCheckFactory>().create(DeviceChecks.NOT_ROOTED);
15+
/// ]]>
16+
/// </code>
17+
/// </summary>
18+
public class DeviceChecks : IDeviceCheckType
19+
{
20+
private static Dictionary<string, DeviceChecks> typesByName = new Dictionary<string, DeviceChecks>();
21+
22+
public static readonly DeviceChecks ROOT_ENABLED = new DeviceChecks(typeof(RootEnabledCheck));
23+
public static readonly DeviceChecks DEVELOPER_MODE_ENABLED = new DeviceChecks(typeof(DeveloperModeEnabledCheck));
24+
public static readonly DeviceChecks IS_EMULATOR = new DeviceChecks(typeof(IsEmulatorCheck));
25+
public static readonly DeviceChecks SCREEN_LOCK_ENABLED = new DeviceChecks(typeof(ScreenLockEnabledCheck));
26+
public static readonly DeviceChecks BACKUP_ENABLED = new DeviceChecks(typeof(BackupEnabledCheck));
27+
public static readonly DeviceChecks ENCRYPTION_ENABLED = new DeviceChecks(typeof(EncryptionEnabledCheck));
28+
public static readonly DeviceChecks DEBUGGER_ENABLED = new DeviceChecks(typeof(DebuggerEnabledCheck));
29+
30+
// add others checks here
31+
// i.e.
32+
// public static readonly DeviceChecks NO_DEBUGGER = new DeviceChecks(typeof(NoDebuggerCheck));
33+
// this way the user will be able to do an enum like selection:
34+
// DeviceChecks.NOT_JAILBROKEN
35+
36+
internal readonly Type CheckType;
37+
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="T:AeroGear.Mobile.Security.DeviceChecks"/> class.
40+
/// Private so that it can't be instantiated externally: useful to emulate an enum.
41+
/// </summary>
42+
/// <param name="checkType">The class type of the check represented by this instance.</param>
43+
private DeviceChecks(Type checkType, string friendlyName = null)
44+
{
45+
if (!ServiceFinder.IsRegistered<IDeviceCheckFactory>())
46+
{
47+
ServiceFinder.RegisterInstance<IDeviceCheckFactory>(AndroidDeviceCheckFactory.INSTANCE);
48+
}
49+
50+
this.CheckType = checkType;
51+
typesByName[friendlyName ?? checkType.Name] = this;
52+
}
53+
54+
/// <summary>
55+
/// Returns an the DeviceChecks instance identified by the passed in name.
56+
/// </summary>
57+
/// <returns>The DeviceChecks instance identified by the passed in name or <code>null</code> if not found.</returns>
58+
/// <param name="name">Name.</param>
59+
public static DeviceChecks GetDeviceCheck(string name)
60+
{
61+
if (typesByName.ContainsKey(name))
62+
{
63+
return typesByName[name];
64+
}
65+
return null;
66+
}
67+
68+
/// <summary>
69+
/// Returns all the checks.
70+
/// </summary>
71+
/// <returns>All the checks.</returns>
72+
public static ICollection<DeviceChecks> GetAllChecks()
73+
{
74+
return typesByName.Values;
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)