Skip to content

Instantly share code, notes, and snippets.

@r2DoesInc
Created August 18, 2014 13:54
Show Gist options
  • Save r2DoesInc/8d4a924574ec51022672 to your computer and use it in GitHub Desktop.
Save r2DoesInc/8d4a924574ec51022672 to your computer and use it in GitHub Desktop.
Third Party iap style verification
#!/bin/bash
#generate_codes.sh 1 500 salty-poop dangerous_codes.txt theSponsor
#start, finish, salt, file_name, sponsor
for i in $(seq $1 $2)
do
md5value=$(md5sum <<< "$i $3")
echo "${md5value// -/}" >> ./"$4"
./push_code.sh "${md5value// -/}" $5
done
//Called in onCreate
private void setupInstall() {
InstallObject install = new SettingsProvider(this).getInstallObject();
if (install == null || install.getFirstInstall() == -1) {
ParseInstallObject.createInstall(this,
new ParseInstallObject.IParseInstallFinished() {
@Override
public void finished(ParseInstallObject obj) {
new SettingsProvider(MainActivity.this)
.setParseInstallObject(obj);
invalidateOptionsMenu();
}
});
} else {
ParseInstallObject.checkNukedStatus(this, install);
invalidateOptionsMenu();
}
}
//called when userenters a code
protected void launchRedeemCode(String code, final AlertDialog d) {
VerificationCode.validateCode(MainActivity.this, code,
new CodeValidationListener() {
@Override
public void onSuccess(boolean isValid, VerificationCode code) {
d.dismiss();
if (isValid) {
new SettingsProvider(MainActivity.this)
.setHasFullUpgrade(true);
AlertDialog.Builder b = new Builder(
new ContextThemeWrapper(MainActivity.this,
R.style.AlertDialogCustom));
b.setTitle("Valid Code");
b.setMessage("Thank you! You have now removed the demo restrictions. Please restart the app.");
b.setPositiveButton("Restart",
new OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
Intent i = getBaseContext()
.getPackageManager()
.getLaunchIntentForPackage(
getBaseContext()
.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
Dialog d = b.show();
int dividerId = d
.getContext()
.getResources()
.getIdentifier("android:id/titleDivider",
null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(
R.color.app_base));
} else {
AlertDialog.Builder b = new Builder(
new ContextThemeWrapper(MainActivity.this,
R.style.AlertDialogCustom));
b.setTitle("Invalid Code");
InstallObject obj = new SettingsProvider(
MainActivity.this).getInstallObject();
if (code.attachedUser == null) {
b.setMessage("Please try again.");
} else {
if (!code.attachedUser.equalsIgnoreCase(obj
.getPrimaryEmail())) {
b.setMessage("This code has already been redeemed by another account.");
} else {
b.setMessage("Please try again.");
}
}
b.setPositiveButton("Dismiss",
new OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
Dialog d = b.show();
int dividerId = d
.getContext()
.getResources()
.getIdentifier("android:id/titleDivider",
null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(
R.color.app_base));
}
}
@Override
public void onFailure() {
d.dismiss();
}
});
}
package com.t3hh4xx0r.nfcapplock.models;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Pattern;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.util.Log;
import android.util.Patterns;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;
import com.t3hh4xx0r.nfcapplock.SettingsProvider;
public class ParseInstallObject {
private IParseInstallFinished parseInstallListener;
private Context c;
InstallObject installObject;
private static final String PRIMARY_EMAIL = "primaryEmail";
private static final int MINUTE = 60000;
private static final int DAY = 86400000;
public ParseInstallObject(Context c) {
this.c = c;
installObject = new InstallObject();
}
public ParseInstallObject() {
// TODO Auto-generated constructor stub
}
public static void createInstall(Context c, IParseInstallFinished listener) {
ParseInstallObject o = new ParseInstallObject(c);
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(c).getAccounts();
if (accounts[0] != null) {
if (emailPattern.matcher(accounts[0].name).matches()) {
o.installObject.setPrimaryEmail(accounts[0].name);
}
}
o.setParseInstallListener(listener);
o.getInstallByEmails();
}
public IParseInstallFinished getParseInstallListener() {
return parseInstallListener;
}
public void setParseInstallListener(
IParseInstallFinished parseInstallListener) {
this.parseInstallListener = parseInstallListener;
}
private void getInstallByEmails() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Install");
query.whereEqualTo(PRIMARY_EMAIL, installObject.primaryEmail);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> results, ParseException e) {
if (e == null) {
if (results.isEmpty()) {
createNewInstall();
} else {
updateInstallFromRemote(results.get(0));
}
} else {
e.printStackTrace();
}
}
});
}
protected void createNewInstall() {
ParseObject install = toParseObject();
install.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException arg0) {
parseInstallListener.finished(ParseInstallObject.this);
}
});
}
private ParseObject toParseObject() {
ParseObject install = new ParseObject("Install");
install.put(PRIMARY_EMAIL, installObject.primaryEmail);
return install;
}
protected void updateInstallFromRemote(ParseObject install) {
installObject.firstInstall = install.getCreatedAt().getTime();
installObject.primaryEmail = install.getString(PRIMARY_EMAIL);
checkNukedStatus(c, installObject);
parseInstallListener.finished(this);
}
public static void checkNukedStatus(Context c,
ParseInstallObject.InstallObject installObject) {
long now = System.currentTimeMillis();
Log.d("TIME SINCE INSTALL",
Long.toString(now - installObject.firstInstall));
if ((now - installObject.firstInstall) > (DAY * 7)) {
// if ((now - installObject.firstInstall) > MINUTE) {
handleNukedInstall(true, c, installObject);
} else {
handleNukedInstall(false, c, null);
}
}
private static void handleNukedInstall(boolean nuked, Context c,
ParseInstallObject.InstallObject installObject) {
if (!nuked) {
new SettingsProvider(c).setAppNuked(false, "");
return;
}
long now = System.currentTimeMillis();
DecimalFormat f = new DecimalFormat("#");
String mod = " mins ago.";
long nukedFor = (now - installObject.firstInstall) / (60000 * 1);
if (nukedFor > 60) {
mod = " hours ago.";
nukedFor = nukedFor / 60;
if (nukedFor > 24) {
mod = " days ago.";
nukedFor = nukedFor / 24;
}
}
new SettingsProvider(c).setAppNuked(true,
"Your demo expired " + f.format(nukedFor) + mod);
}
public interface IParseInstallFinished {
void finished(ParseInstallObject object);
}
public InstallObject getInstallObject() {
return installObject;
}
public class InstallObject {
private String primaryEmail;
private long firstInstall = -1;
public String getPrimaryEmail() {
return primaryEmail;
}
@Override
public String toString() {
return "InstallObject [getPrimaryEmail()=" + getPrimaryEmail()
+ ", getFirstInstall()=" + getFirstInstall() + "]";
}
public void setPrimaryEmail(String primaryEmail) {
this.primaryEmail = primaryEmail;
}
public long getFirstInstall() {
return firstInstall;
}
public void setFirstInstall(long firstInstall) {
this.firstInstall = firstInstall;
}
}
}
curl -X POST \
-H "X-Parse-Application-Id: XXXXXXX" \
-H "X-Parse-Master-Key: XXXXXX" \
-H "Content-Type: application/json" \
-d '{"code":"'"$1"'", "sponsor":"'"$2"'"}' \
https://api.parse.com/1/classes/VerificationCode
public void setParseInstallObject(ParseInstallObject obj) {
Gson g = new Gson();
String toStringValue = g.toJson(obj.getInstallObject());
Editor e = PreferenceManager.getDefaultSharedPreferences(c).edit();
e.putString("parse_install", toStringValue);
e.commit();
}
public ParseInstallObject.InstallObject getInstallObject() {
Gson g = new Gson();
return g.fromJson(PreferenceManager.getDefaultSharedPreferences(c)
.getString("parse_install", ""),
ParseInstallObject.InstallObject.class);
}
package com.t3hh4xx0r.nfcapplock.models;
import java.util.List;
import java.util.regex.Pattern;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.util.Patterns;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.t3hh4xx0r.nfcapplock.SettingsProvider;
import com.t3hh4xx0r.nfcapplock.models.ParseInstallObject.InstallObject;
public class VerificationCode {
String code;
public String attachedUser;
public String type = "simple";
public static final String FULL = "full";
public static final String SIMPLE = "simple";
public interface CodeValidationListener {
void onSuccess(boolean isValid, VerificationCode code);
void onFailure();
}
public static void validateCode(final Context c, final String code,
final CodeValidationListener listener) {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"VerificationCode");
query.whereEqualTo("code", code.trim().toLowerCase() + " ");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> res, ParseException e) {
if (e == null) {
VerificationCode vCode = new VerificationCode();
if (res.isEmpty()) {
listener.onSuccess(false, vCode);
} else {
vCode.code = code;
String email = getPrimaryEmail(c);
if (!res.get(0).has("attachedUser")) {
res.get(0).put("attachedUser", email);
res.get(0).saveInBackground();
vCode.attachedUser = email;
if (res.get(0).has("type")) {
vCode.type = res.get(0).getString("type");
}
listener.onSuccess(true, vCode);
} else {
vCode.attachedUser = res.get(0).getString(
"attachedUser");
if (res.get(0).has("type")) {
vCode.type = res.get(0).getString("type");
}
InstallObject obj = new SettingsProvider(c)
.getInstallObject();
if (vCode.attachedUser.equalsIgnoreCase(obj
.getPrimaryEmail())) {
listener.onSuccess(true, vCode);
} else {
listener.onSuccess(false, vCode);
}
}
}
} else {
listener.onFailure();
}
}
});
}
public static String getPrimaryEmail(Context c) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(c).getAccounts();
if (accounts[0] != null) {
if (emailPattern.matcher(accounts[0].name).matches()) {
return accounts[0].name;
}
}
return "";
}
@Override
public String toString() {
return "VerificationCode [code=" + code + ", attachedUser="
+ attachedUser + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment