Wednesday, July 8, 2009

How to protect part of a public method from being executed by others

I recently had to face this problem while I was modifying the android framework to add a security feature and I didn't want 3rd party apps to be able to mess with it.

To solve this problem I used enforceCallingOrSelfPermission.

Anything you do in your code until you call that method will be runnable by anyone who can access your method but passed this point it will through a SecurityException If neither you nor the calling process has been granted a particular permission.

Here's what it looks like:
void myMethod() {
// my code every one has access to...
myContext.enforceCallingOrSelfPermission(myPermission, myMessage);
// my super code that noone else can execute
}

No comments:

Post a Comment