Pages

Thursday, January 29, 2015

Beginning Grails, Groovy and Griffon, error #1

Just found an error in the book. When you create a filter, the author suggests you to copy this code block to your app:

if (session?.user?.id != params?.id) {
flash.message = "You can only modify yourself!"
redirect(controller:"user", action: "index")
return false

}

But actually the params.id is a String and the session.user.id is a Long type, so this condition turns out to be executed every time, even when both values are 2, for example.

The working code would be like this:

if (session?.user?.id?.toInteger() != params?.id?.toInteger()) {
flash.message = "You can only modify yourself!"
redirect(controller:"user"action"index")
return false

}

Tuesday, December 30, 2014



Look in to that determined glance. A being, determined to survive and do anything to survive. 

Lion is not trying to be a lion, the majestic king of nature, the ultimate predator it is. It simply is. It is his nature, and there is no other way that he can be.

To be a warrior.

There are no choices - to be the strongest predator, or not. He is, or he is not a lion. Or he is not alive. The only struggle that forces you to become something better - the struggle to survive.

Thursday, December 25, 2014

A simpler way to encode passwords with grails

Hi! I've discovered that a password can be encrypted much easier, and without any additional plugins! Probably the encryption will not be so strong as with the jasypt plugin, but is much easier to implement.


The authentication method:


  def encryptedPassword = params.password.bytes.encodeBase64().toString()
def user = User.findByLoginAndPassword(params.login, encryptedPassword)

The register method:

def encryptedPassword = params.password.bytes.encodeBase64().toString()
def user = new User(login:params.login, password:encryptedPassword)
user.save()

All this encrypts the password at once when you enter it. Persists new user with the encrypted string to the DB, and when logging in it encrypts the entered password and then compares to the persisted string in your DB


Update: this is encoding, not encrypting:
Encoding transforms data into another format using a scheme that is publicly available so that it can easily be reversed.
Encryption transforms data into another format in such a way that only specific individual(s) can reverse the transformation.

Probably there are some uses for this stuff, but certainly not for security.

Thursday, December 11, 2014

grails jasypt field encryption - encrypt database entries

Hi! One cool plugin i came across recently - jasypt-encryption plugin. Using it you can easily implement automatic encryption and decryption of fields to your database. This will protect data in your database. But not in your application, because in your app the data is unencrypted, it is encrypted only in the DB.

First of all, check this repository for a wiki and some code examples: https://github.com/dtanner/grails-jasypt

Installation is very easy:

1)In BuildConfig.groovy (for hibernate4 you need jasypt 1.3.1, for hibernate 3 you need jasypt 1.2.1):

plugins {
compile ":jasypt-encryption:1.3.1"
runtime ":hibernate4:4.3.5.5" 
}



2)in Config.groovy:


jasypt {
    algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC"
    providerName = "BC"
    password = "test"
    keyObtentionIterations = 1000

}

3)And in the domain object you will use encryption, add import and mapping:


 import com.bloomhealthco.jasypt.GormEncryptedStringType
String password
static mapping = {
password type: GormEncryptedStringType
}

Not when the user is persisted, the password field is automatically encrypted, and decrypted when fetched from DB.

So, this is a way to add some security to your database. But if you need encryption for, example, protecting passwords, then you better not use jasypt, but use spring security.

Jasypt encrypts and persists data, and when you fetch data from the DB it is automatically decrypted, so, for example, in an authentication algorithm you compare unencrypted passwords, and the password fetched from DB exists in an unencrypted state in your application.

While with spring security plugin the encryption is one-way: the password is encrypted and persisted, and when user logins, the password is encrypted, and the encrypted string is compared to the persisted one from earlier.

Sunday, November 23, 2014

Grails and XAMPP MySQL database configuration or any external DB

Not much happening lately. End of the year, have to study a little bit not to get kicked out.

SUBJ: as usually, couldnt find an easy tutorial, so im making one.


  1.  Install XAMPP, launch it. 
  2.  Check the mysql config > my.ini. Remember the user, password and port.
  3. In your grails BuildConfig.groovy add this line:

    dependencies {

    ...

    runtime 'mysql:mysql-connector-java:5.1.34'

    ...

    }

    ...
  4. In your DataSource.groovy comment the defaule values and add your own:
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "username from the config file"
    password = "password from the config file"
  5. There are the environment specific settings. The dbCreate = 'create' means that a new DB is created each time you run the app. Change it to 'update' and the DB will be persisted and updated each time.

         url = "jdbc:mysql://localhost:PORT_FROM_CONFIG_FILE_HERE/dev_db"

    Change the url to the DB schema. Not that you have to create it first in your DB. (I used MySQL workbench to connect to a running XAMPP DB, and created a schema)
  6. Basically that is it, now your app in development mode (or the mode you edited in the environment settings) will use the database you specified. Just remember that you must start the Apache too in XAMPP. 
  7. Oh, and you possibly need to grant permission for the user you defined, it is done through XAMPP mysql admin button.

Monday, November 3, 2014

Finally released my first game!



Much learned during development. If i would start now i would do so much things differently. Firstly - i would plan everything more thoroughly. The most important thing to plan would be the classes and relations, the architecture.

Also during development i played much similar games to mine, and now i would concentrate on a simplier control scheme. Some people have trouble getting the current controls. Which are: tap left part of screen for a small jump, tap right part of the screen for a bigger jump, and tap in jump for a double jump. Still some people prefer smashing their palm on the screen.

A disappointment was that my country isnt supported by google merchant, so i cant sell my app or offer in app purchases. So i turned to ads and a cool survey system that offers double jumps for completing short surveys - Pollfish.

Also one thing learned that game development consists of drawing graphics, that i dont like to do. I just want to code, code, code.

Pollfish integration with Libgdx and reward system.

So, to make pollfish showing in your app is very easy. Even with libgdx, you just make some changes to your androidlauncher.java , like that below.

Recently there was an official guide for libgdx posted, be sure to check it first -> https://github.com/libgdx/libgdx/wiki/Pollfish-in-libgdx

First of all, follow the official guide -> https://www.pollfish.com/android

1)Register  and get your developer id key.

2)Add the pollfish sdk as instructed in the guide

3)Add pollfish init method to the onResume method of androidlauncher:

  protected void onResume() {
super.onResume();
PollFish.init(this, "your id here", Position.TOP_RIGHT, 5);
}

4)Make your AndroidLauncher class implement PollfishSurveyCompletedListener

and add such a method:

@Override
public void onPollfishSurveyCompleted(boolean arg0, int arg1) {
main.surveyCompleted();
}

My libgdx main class has a method surveyCompleted that adds 15 double jumps and saves preferences.

Change the main class initialiation in onCreate, so that you can use main reference in other methods


main = new Main();

initialize(main, config);

Now your app will show pollfish icon when a survey is available, and a surveyCompleted() method will be executed when user completes a survey.

I also tried to make the pollfish icon hide during gameplay using PollFish.hide() method, but seems to me that there are some problems with libgdx, as it steals focus from the Android activity, my hide method call didnt hide the icon. If you implemented successfully the hide method in your libgdx game, post in comments!