Wednesday, March 11, 2015

CAS - Central Authentication Service

Introduction


This post will give you a brief overview of what is CAS - Central authentication service as a protocol and CAS solution which is an solution for web services implemented by JASIG - Java in Administration Special Interest Group.

Before you dive in, there are some things you need to know like:

  • What is multi-sign-on? We have multi-sign-on when we have multiple web applications, each having their own login form. Most probably for each of them you need to use different usernames and passwords
  • What is single-sign-on? We can describe this as being able to login only once in order to access multiple web applications. From my point of view the most obvious example is Google, one needs to login only once to access all Google services like Gmail, Google Drive, Google+ or Google analytics.

CAS - the protocol


CAS is a single-sign-on protocol which allows users to access multiple web services by providing only once their authentication credentials, usually their username and password.

The involved entities in such a protocol would be the CAS server, the registered to CAS web service and the client web browser. Obviously the CAS server would hold an CAS application instance. A registered service is a service which will become accessible by the user after he successfully logs in, a user would use a web browser to access the service.

One of the best way to get you started with understanding CAS protocol is to follow the sequence diagram which is available on JASIG's website at http://jasig.github.io/cas/4.0.x/protocol/CAS-Protocol.html.

CAS - the application


CAS, the application, developed by JASIG is an open source software that implements CAS protocol.

The application consists of a server component which is written in Java. CAS developers also used Spring Webflow and Spring MVC framework which in my opinion is a very good thing as it allows other developers to improve or customize CAS according to their needs easier by just being able to understand these common used frameworks.

CAS has libraries for different authentication methods like authenticating user against LDAP or database. Also its very easy to configure your own authentication handler, for example an authentication handler which calls an external service.

As the registered services must also implement the CAS protocol there are client implementations in different programming languages like Java, C#, PHP or Perl.


When is CAS useful and when its not useful?


CAS is useful when you want to allow users to access multiple web applications by requiring them to authenticate only once instead of multiple times. Obviously you cant have multiple users with same username, this means that you might want to have only one place in which you store usernames and passwords.

One of the most problematic issue which I've found in CAS is that it does not allow you to group registered services which are accessible by a user after authenticating in CAS. For example if I have four registered services, after an user with a certain role logs in he should be able to access only first two of the four services and a second user with other privileges should be able to access only the last two of the services. From the short research which I've done there is no support for this. A way to solve this is to have two CAS instances running or another would be to customize CAS to work with something similar to ACL - Access Control List.


Useful resources


Even if I am not an expert in developing applications which implement CAS protocol or use CAS application as a solution for single-sign-on I wanted to share what I know and I hope that this will help other developers to get them started with CAS. I will also try to create a blogpost in which to describe a step by step and from scratch CAS installation and configuration. 

You're feedback and comments are more than welcome!

Good luck!
Robert Rusu

Saturday, February 28, 2015

Book review: Thinking in Java - Fourth edition

To become a better programmer you should always look into reading new programming books, watch online tutorials or attend to related workshop. For Java programmers this book is one of the best and it provides detailed explanations and examples about the most important Java features.

I read this book once and later came back several times when I wanted to refresh my knowledge about a certain Java topic. As it has been such a useful learning resource for me I want to share it with you. To do so I composed a short and straight to the point book review.

Introduction

Thinking in Java was written by Bruce Eckel who also wrote other popular books like: Thinking in C# and Thinking in C++. The book was revised and improved multiple times, as a result of this multiple editions were published. The last one, fourth edition, was published in 2006.

In my opinion you at least must have little to medium programming background and prior hands-on experience Java/C#/C++. Besides this even if you are an experienced Java developer, if you did not read this book yet I highly recommend to do so as you will find a lot of information which will certainly improve your skills.

Good things

  • The book is designed in such a manner that it helps you to learn fast starting with simple Java topics like Operators, Objects and Access Control to more advanced Java features like Annotations, Concurrency and Graphical User Interfaces.
  • This book contains a lot of examples and exercises. 
  • Interesting and relevant references are given when approaching a new topic.
  • Explanations are easy to understand.
  • Author mentions deprecated methods and why those should not be used anymore.
  • Covers common pitfalls.

Bad things


Final note

Reading a technical book is not enough, to get most value of your time you should run all code samples, modify them to crash or even think how to improve them. Besides this you should also solve all exercises from the book in order to clearly understand the approached topics. So what are you still waiting? Open the book, start your favorite IDE and start learning!

Feel free to add your opinion in a comment about this blog post or about this great book.

Robert Rusu

Thursday, June 12, 2014

Install APCu on Windows

Assumptions

-I assume that you know what is APC - Alternative PHP cache 
-You want to install APCu because APC is not compatible anymore with PHP 5.5.x
-You want to install APCu for wamp, xampp. Mostly windows web development platforms for PHP

Instructions

Pre: All directory locations might be different for you depending on your wamp installation folder and your PHP/apache versions.

1. Go to http://pecl.php.net/package/APCu, there is a table with available releases
2.Choose whatever release suits you better(I chose 4.0.5 DLL)  
3. Choose package from DLL list, depending on what Windows you are using(32 bits/64 bits) and PHP version. In my case I chose 5.5 Thread Safe (TS) x86
4. Unzip the archive, copy php_apcu.dll in C:\wamp\bin\php\php5.5.12\ext.
5. Go to C:\wamp\bin\apache\apache2.4.9\bin open php.ini  and add the following lines(I just added them at the end of the file):
[apcu]
extension="C:\wamp\bin\php\php5.5.12\ext\php_apcu.dll"
apc.enabled=1
apc.shm_size=32M
apc.ttl=7200
apc.enable_cli=1
apc.serializer=php

This are recommended configurations located in INSTALL file from the php_apcu archive, excepting the location of the DLL file.

6. Restart wamp
7. Go to http://localhost/phpinfo.php and check if apcu configuration table appears and apcu is enabled
8. If you also want to use apcu for PHP CLI then you only need to add in C:\wamp\bin\php\php5.5.12\bin\php.ini the config lines you added at step 5 in apache's php.ini.

The end!

Now you should be ready to start developing faster applications! I hope this helped everyone out there who did not find a tutorial on how to install APCu for windows. I also encourage you to leave me some feedback!

Robert Rusu

Tuesday, April 29, 2014

Things that you should do in order to become a better programmer

All of us try to become better at what we do, as a programmer I will try to give you a small list of things that will help you to become a better programmer.

#1 Always try to help others

Its always a good thing to help others when they are in trouble, when they need to learn new things or they just need an advice!

First of all helping others will help you understand better a concept or that something that they need explained. Your knowledge regarding that technology or whatever you help them with will get better just by trying to explain it. Being able to explain it in simple ways and giving others simple examples denotes the fact that you can master it.

For example helping others understand a design pattern, explaining them when and how to use it might very well refresh your memory regarding what you know about it. This would be another reason to help others. Helping others will make you feel better however its also important to not neglect your work!

#2 "Learning from the big fish"

This is a term that I often use when I am trying to say that you should always try to learn new things from more experienced programmers. For example I always try to see how more experience programmers act in different situations, how they solve different problems and what practices they use.

Its very important to also understand why more experienced programmers use a tool instead of another or why they are applying a design a pattern instead of another. Just copying what the "big fish" do is not enough!

"Learning from the big fish" can be accomplished in many other ways than meeting them face to face, I suggest reading books, watching videos or tutorials, watching(or even better going) to conferences hold by other programmers, reading articles or by just asking more experienced friends how to solve a particular problem or what would be a common solution to a general problem.

#3 Training

In my opinion its very important to train your programming skills. Even if you work 8 h a day I suggest that you should spend some time improving your skills. Programming is a skill and every skill is lost if you are not continuously training. 

This might be consisted of just working on a personal project in which you use another programming language than the one you use at work. Yes, learning a new programming language will improve your way you are programming in your favorite language just by understanding new concepts, existing problems solved in another manner than you were used to or just getting a fresh perspective.

It might be just you trying to understand concepts that you did not quite understand during work. It might be something new that you will encounter at work and you are trying to get a perspective about it. Or it can just be you write some small program to sharpen your coding speed or improve your analytic thinking.

Good luck!
Robert Rusu

Monday, April 14, 2014

Compile error Easy Mock

Easy Mock is a mocking library for Java which can help you to test your code isolated from some other components.

If you ever get the next compile error:
"expected (java.lang.void) in easymock cannot be applied to (void)"
It may be a result of you trying to call EasyMock.expect(mock.methodWhichReturnsVoid()), instead of doing that you should use expectLastCall method, you can use it like:
mock.methodWhichReturnsVoid(); 
EasyMock.expectLastCall();

I posted this tip because it took me several minutes to figure out what I was doing wrong, by the way, I was really tired!

Robert Rusu

Saturday, April 5, 2014

Symfony2.3 form, grandchildren forms are not validated

Recently I had some issues with Symfony2.3 grandchildren forms not being validated even if I used as default option 'cascade_validation' => true. After doing some research I found other people having same issue and I found out that the solution is that besides putting 'cascade_validation' to true as default option to the form I also had to 'cascade_validation' => true in the collection of children and grandchildren forms I added.

Check the code sample below
Father form
class FatherType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'children',
            'collection',
            array(
                'type' => new ChildFormType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'required' => false,
                'cascade_validation' => true //important to be added
            )
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Company/SomeBundle/Form/Entity/FatherFormEntity',
                'cascade_validation' => true, //important to be added
            )
        );
    }

    public function getName()
    {
        return 'father';
    }
}
Child form
class ChildType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'grandchildren',
            'collection',
            array(
                'type' => new GrandchildFormType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'required' => false,
                'cascade_validation' => true //important to be added
            )
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Company/SomeBundle/Form/Entity/ChildFormEntity',
                'cascade_validation' => true, //important to be added
            )
        );
    }

    public function getName()
    {
        return 'child';
    }
}
Grandchild form
class GrandchildType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'attribute',
            'text'
        );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Company/SomeBundle/Form/Entity/GrandchildFormEntity',
                'cascade_validation' => true, 
            )
        );
    }

    public function getName()
    {
        return 'grandchild';
    }
}

References: https://github.com/symfony/symfony/issues/5204


I hope it helped you!
Robert Rusu

Tuesday, February 18, 2014

Writing clean code and following the standards

What does "writing clean code" and "following the standards" mean?

You might wonder what does "writing clean code" mean, well it means that as a programmer you write code that can be easy to read, understand, modify and test. Also what I mean by "following the standards" is that we should learn and apply the rules and conventions of a programming language, team and community.

For example Oracle offers the coding conventions for Java or Microsoft offers the coding conventions for C#. Another example would be that if you would like to contribute for developing a framework like Symfony2 you must also follow its conding standards.

Why should we write clean code and follow the standards?

In our days most of the real world applications need continuous development and even small programs require to be changed or improved after a while. Sometimes we often share pieces of code or entire applications to help others. Also from time to time we need to update or fix code we wrote a long time ago. As a result of all this we often spend our time reading code, understand what does it do and we will be required to modify it. That is why writing clean code will help us to deliver quality code which others can understand and can update it to their needs, we will be able to fix bugs and add new features or update the code faster.

Examples of why it's important to write clean code


Nowadays applications get bigger and are used by a lot of users everyday, many of them offer a lot of features and rarely you are the only programmer working on an application, often you will work in a team or even sometimes several teams work on an application, each working at one of its module. You will often need to read and fix a piece of code wrote by one of the other team members which is in vacation, imagine how hard it would be if the code would have been written in chaotically? Maybe you would think that it would have been faster to rewrite the whole thing again? But no investor is willing to pay you to rewrite a piece of functionality again, maybe you would not write it better than the previous team member. Now imagine how easy it would be for you to change the code if it was written clean and easy to understand, even more it was written by following the coding style and standards on which the team agreed at the beginning of the project? It would be a lot easier and you would not need to suffer just because you need to do a bug fix.

Another example of why we should write clean code and follow the standards which I've experienced was that I was assigned a job to update a relative small PHP script designed for interacting with twitter, even if I am experienced with PHP I was unable to understand what did the code do because the code was written chaotically, half of its naming was in the native language of the previous programmer, it was not in English which is the common language used for naming variables, methods, classes, folders etc. The files had between five hundred and one thousand lines of code, variables were not suggestive(at least ones which were named in English). It would have been easy for me to update the script if it was written clean, had meaningful names, if it had been divided logically into modules.

Resources

There are a lot of resources out there which will help you start writing clean code, one of my favorites is a book called Clean Code: A Handbook of Agile Software Craftsmanship written by Robert C. Martin in who is a promoter of writing clean code. 

For following the standards of a programming language in which you write code you should consult the official coding conventions, also if you use a framework you should check out what is the standard coding style on top of which it was built, in general it should be the same of it's related programming language. Besides that a framework often has a standard way to organize classes and other resources, this is one of the many advantages of using a framework, that other programmers can understand the code easier.