Friday, February 17, 2017

Set up Shibboleth SP as a SAML 2.0 service provider with G Suite

Prerequisite:

  1. Basic understanding of SAML 2.0, SSO and Shibboleth SP.  
  2. SP setup up and working on your instance.
  3. Must having administrator account to register your SP on G suite

G Suite setup:

  • Login to https://admin.google.com using your administrator account.
  • Click Security > Set up single sign-on (SSO)
  • Click the Download button to download the Google IdP metadata and the X.509 Certificate
  • Now click on Apps > SAML apps.
  • Select the Add a service/App to your domain link or click the plus (+) icon in the bottom corner. The Enable SSO for SAML Application window opens.
  • Click SET UP MY OWN CUSTOM APP
  • We have already downloaded the certificate and Idp Metadata, click NEXT
  • On the Basic application information window, Enter the Application name and Description values.
  • In the Service Provider Details section, enter the following URLs into the Entity ID, ACS URL, and Start URL Fields:
    1. ACS URLhttps://your-domain-name.com/Shibboleth.sso/SAML2/POST
    2. Entity IDyour-domain-name.com/shibboleth
    3. Start URL: https://your-domain-name.com/app
Note: You can get the ACS URL and entityID by hitting https://your-domain-name/Shibboleth.sso/Metdata. It will download the Shibboleth SP metadata file containing all the URLs like entityID in the first few lines and ACS URL which is nothing but AssertionConsumerService URL having SAML 2.0 HTTP-POST binding.
    <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://your-domain-name.com/Shibboleth.sso/SAML2/POST" index="10"/>
  • Leave Signed Response unchecked. When the Signed Response checkbox is unchecked, only the assertion is signed. When the Signed Response checkbox is checked, the entire response is signed.
  • The default Name ID is the primary email and select EMAIL as Name ID Format.
  • Click Add NEW MAPPING and then add EMAIL and choose Basic information and Email from 2nd and 3rd drop down list.
  • Click Finish.
  • Now go to again on Apps -> SAML apps and select your APPLICATION.
  •  At the top of the gray box, click More and choose:
    1. On for everyone to turn on the service for all users (click again to confirm).
    2. Off to turn off the service for all users (click again to confirm).
    3. On for some organizations to change the setting only for some users.

Configured G Suite details in your Shibboleth SP

  • Drop the downloaded Google Idp metadata to opt\shibboleth-sp\etc\shibboleth directory.
  • Open Shibboleth2.xml file and add below snippet
    • <MetadataProvider type="XML" file="C:\opt\shibboleth-sp\etc\shibboleth\<GOOGLE_IDP_FILENAMExml>"/>    
  • Restart Shibboleth.

 Verify that SSO between G Suite and Zendesk is working

  • Close all browser windows.
  • Open https://your-domain-name.com/app and attempt to sign in.
  • You should be automatically redirected to the G Suite sign-in page or if you are having discovery page then it will come under drop down menu
  • Enter your sign-in credentials.
  • After your sign-in credentials are authenticated you're automatically redirected back to your Application.

Happy coding..!!!

Tuesday, February 14, 2017

Singleton Class Vs Singleton bean scope

I have seen people getting confused between singleton scope vs singleton design pattern. Basically, there is a bit difference between these two.

Singleton scope: The spring support five different scopes and it is used to decide which type of bean instance should be returning from Spring container back to the caller. One of the scope is Singleton and the by default scope too. It returns a single bean instance per Spring IoC container.

<bean id=”object1” class=“com.package.classname”/>

When I said, single bean instance per spring Ioc Container i.e. you will always get the same object regardless of the number of call of the same bean but if you declare another bean for the same class then you will get another object for another bean.

Let’s understand this with an example:

<bean id=”object1” class=“com.package.classname”/>
<bean id=”object2” class=“com.package.classname” scope=”prototype”/>
<bean id=”object3” class=“com.package.classname”/>

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(APP_FILE);
Classname name1 = (Classname) context.getBean(“object1”);
Classname name2 = (Classname) context.getBean(“object2”);
Classname name3 = (Classname) context.getBean(“object3”);


name1==name1 // true, object1 is singleton, calling again n again will give the same object.
name2==name2 //false, object2 is prototype, calling again n again will give the different object
name1==name3 // false, object1 & object3 is singleton but two different bean.

So, the question arise how will you achieve the Singleton design pattern in Spring?

Spring framework provides facility to inject bean using factory method i.e. that returns an instance of its own class and can be used in singleton design pattern.

public class Singleton {
         
                private static volatile Singleton instance = null;
                private Singleton(){        
                }
                                public static Singleton  getInstance(){
                                if(instance == null) {
                                                synchronized (Singleton.class) {
                                                                if(instance == null){
                                                                                instance = new Singleton();
                                                                }
                                                }
                                }
                                return instance;
                }
           }

<bean id="object4" class="com.package.Singleton" factory-method="getInstance"/>
       <bean id="object5" class="com.package.Singleton" factory-method="getInstance"/>
Singleton singleton1= (Singleton) context.getBean(“object4”);
Singleton singleton2= (Singleton) context.getBean(“object5”);

singleton1==singleton1 // true, object1 has singleton scope, calling again n again will give the same object.
singleton1==singleton2 //true, Class is a singleton and we are getting an object from getInstance using factory-method.

Conclusion: Singleton scope is a bit different from a single design pattern, Returns a single bean per Spring Ioc Container whereas singleton design pattern will always return the same object.
Singleton scope can be useful where you are creating multiple datasource in your application where each datasource point to a different object.

You can download the code source from my GitHub repository.

Happy coding!!!




Friday, January 13, 2017

How to uinstall MySQL completely from Windows OS?

I was running some script which did some changes to my database and corrupted my root permission. Tried so many things but didn't work out.

Finally, I decided to uninstall the MySQL from my instance and install a new one but again it was not an easy job as MySQL stores file at the various locations that you have to removed manually before starting from the scratch.

Simple steps to uninstall MySQL:

  1. Stop MySQL services and remove services by executing below command in command prompt (Start it as Administrator)
    1. Net stop MySQL
    2. Sc delete MySQL
  2. Uninstall MySQL program from the control panel.
  3. #2 will uninstall the program but will not remove all the files from your machine which we have to do it manually.(Removing all files will remove existing database. Take the backup, if you need it in future.)
    1. C:\Program Files\MySQL
    2. C:\Program Files (x86)\MySQL
    3. C:\ProgramData\MySQL
    4. C:\Users\<USERNAME>\AppData\Roaming\MySQL
  4. Restart your instance and install it again.
Happy coding..!!!


How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...