Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Step by step Google SSO (java)?

I am lost in all my open browser tabs for Google single sign on :)

I already have an application which I would like to put on Google market place. And mandatory integration is Google SSO. I have built application on Struts2 with Spring.

So now I need some instructions how to make this integration. An example would be perfect. Or how to start, which technology to use, best approaches, anything similar...

Also, do I have to use Google App Engine for SSO integration or no? Honestly, I am confused :)

EDIT

I started here: developers.google.com/google-apps/marketplace/sso Because I use Java, if you look at Getting started at the bottom, I wanted to use step2, but the link is dead. From there on I got stuck...

Links here are also dead.

like image 395
Trick Avatar asked Aug 30 '12 10:08

Trick


People also ask

What is Google Apps SSO?

Single sign-on (SSO) lets users sign in to all their enterprise cloud applications using their managed Google account credentials. Google offers pre-integrated SSO with over 200 popular cloud applications.

Is OAuth a SSO?

OAuth (Open Authorization) is an open standard for token-based authentication and authorization which is used to provide single sign-on (SSO). OAuth allows an end user's account information to be used by third-party services, such as Facebook, without exposing the user's password.


2 Answers

I made with Apache's HttpClient. This is my solution and works perfectly for me.

First point to authorization url:

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email&state=%2Fprofile&response_type=code&client_id=<YOUR_CLIENT_ID>&redirect_uri=<YOUR_CALLBACK_URL>

Then get parameters back from your redirect_uri and build request body for getting access_token:

    String code =  request.getParameter("code");
    String foros = "code="+code +
                "&client_id=<YOUR_CLIENT_ID>" +
                "&client_secret=<YOUR_CLIENT_SECRET>" +
                "&redirect_uri="+getText("google.auth.redirect.uri") +
                "&grant_type=authorization_code";

Then with HttpClient make a POST and with simple JSON parser parse out the access token.

    HttpClient client = new HttpClient();
    String url = "https://accounts.google.com/o/oauth2/token";
    PostMethod post = new PostMethod(url);
    post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    try {
        post.setRequestEntity(new StringRequestEntity(foros, null, null));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    String accessToken = null;
    try {
        client.executeMethod(post);
        String resp = post.getResponseBodyAsString();
        JSONParser jsonParser = new JSONParser();
        Object obj = jsonParser.parse(resp);
        JSONObject parsed = (JSONObject)obj;            
        accessToken = (String) parsed.get("access_token");
    } catch (HttpException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }

Now you have the access token and you can now access all of the Google API. For example, getting all the users info:

    GetMethod getUserInfo = new GetMethod("https://www.googleapis.com/oauth2/v1/userinfo?access_token="+accessToken);

    String googleId = null;
    String email = null;
    String name = null;
    String firstName = null;
    String lastName = null;
    try {
        client.executeMethod(getUserInfo);
        String resp = getUserInfo.getResponseBodyAsString();
        JSONParser jsonParser = new JSONParser();
        Object obj = jsonParser.parse(resp);
        JSONObject parsed = (JSONObject)obj;
        googleId = (String) parsed.get("id");
        email = (String) parsed.get("email");
        name = (String) parsed.get("name");
        firstName = (String) parsed.get("given_name");
        lastName = (String) parsed.get("family_name");
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }

This data you can now save and use it for login to your application.

like image 138
Trick Avatar answered Oct 20 '22 03:10

Trick


You can follow the Java tutorial for the Google Apps Marketplace which explains how to perform Single Sign-On: https://developers.google.com/google-apps/marketplace/tutorial_java

The tutorial also includes a download link for a zip with the application source and all the required libraries, including step2: http://apps-marketplace-resources.googlecode.com/files/helloworld-java-2011050303.zip

Here are valid links for the broken ones in the docs:

  • step2: http://code.google.com/p/step2/
  • openid4java: http://code.google.com/p/openid4java/
  • gdata-java-client: http://code.google.com/p/gdata-java-client/
like image 37
Claudio Cherubino Avatar answered Oct 20 '22 02:10

Claudio Cherubino