Sonntag, 23. Oktober 2016

Google Login with Oracle Jet - Part 2 - Combine with Oracle Jet webapplication

Introduction


This is part 2 of a series of blogs, that explain on how to use Google Sign In with Oracle Jet. For more information, please start with part 1.
  1. Google Login with Oracle Jet - Part 1 - Create Google Project
  2. Google Login with Oracle Jet - Part 2 - Combine with Oracle Jet webapplication
  3. Google Login with Oracle Jet - Part 3 - Combine with Oracle Jet hybrid application
Let's begin with the fun part. We did create a Google project and an OAuth client ID in part 1. Now we will create a basic Oracle Jet webapplication and add Sign In and Sign Out functionality to it. Besides that, we also want to read the name of the logged in user and show the picture of the logged in user.


Create Oracle Jet webapplication

We did this several times before and we will do it again. We will create really fast an Oracle Jet page, which is based on the basic template. Just enter the following commands to first create it an than build it.

$ yo oraclejet webclient --template=basic:web

$ grunt build

First steps

I did call it first steps, because I didn't find a better word and cause I didn't want to put these steps in another chapter.

1. Open Netbeans
2. Open the project which you just created
3. Create the following file
File-Location:
src/js/googlecredentials.json

File-Content:
{
    "client_id": "<LONG_UNIQUE_ID>.apps.googleusercontent.com",
    "scope": "profile email"
}

As you can see this is a JSON file, which holds the client_id. I am pretty sure, that it is not a good idea to keep your client_id here, because now everybody can read it, but I didn't find a better place to put it.

As client_id you should use the id, which has been created in Part 1 of this series of blogs. 
Go to the following URL, select your project and select Credentials (left navigation bar). Now you should be able to see your client Id again.



4. Make adjustments to the main.js file to read the googlecredentials.json file and store it in the sessionStorage as soon as the page is being loaded. The adjustments should be made in the init() function. As you can see, the credentials are being stored in an item called googlecredentials.

$.ajax({                                
url: 'js/googlecredentials.json',
dataType: 'json',
async: false,
success: function (data) {
sessionStorage.setItem('googlecredentials', JSON.stringify(data));
},
error: function (req, status, err) {
console.log('something went wrong', status, err);
}
});


Create the Javascript functionality

We do need some Javascript functions like GoogleSignIn, GoogleSignOut or IsUserLoggedIn to make this work.
Please open the appController.js file and make the following adjustments  to it.

1. First of all we have to extend the define, because the GoogleApi also has to be loaded. So please add 'https://apis.google.com/js/platform.js' to your define.

My define looks like this now. Yours can look different, but the important part is, that you add this 'https://apis.google.com/js/platform.js'

define(['ojs/ojcore', 'knockout', 'ojs/ojknockout', 'https://apis.google.com/js/platform.js'],
        function (oj, ko) {


2. Create some knockout observable parameters, that will be defined later
// Define if the user is logged in
self.isUserLoggedIn = ko.observable(false);
// Username
self.userLogin = ko.observable("");
// User Profile Picture
self.imagePath = ko.observable("");


3. Send the Client_ID to the GoogleApi and in return you get information about the logged in user, if the user is logged in. 
  • As you can see, here we send the googlecredentials.json that has been stored in the sessionStorage during the init() function of main.js.
  • We also redefine the isUserLoggedIn();
  • Since this function is placed directly inside the ControllerViewModel it will be called whenever the index.html is being loaded.

function ControllerViewModel() {
...
window.gapi.load('client:auth2', function () {                    
window.gapi.auth2.init(JSON.parse(sessionStorage.getItem('googlecredentials'))).then(function (googleAuth) {
self.googleAuth = googleAuth;
self.isUserLoggedIn(googleAuth.isSignedIn.get());
});
});
...
}



4. We write a function, which will be triggered, when the user is clicking the sign in button. 
  • This is a promise, which is entering the first function, if everything worked fine and the second function, if there was an error.
  • In case of a success I decided to set isUserLoggedIn to true and get some user profile information like picture and name.
  • In the second function, which means, that the login was not correct, we set the isUserLoggedIn to false again. Later we will see in the html, that different parts of the page will be shown, depending on this variable.

function ControllerViewModel() {
...
self.googleSignIn = function (data, event) {
window.gapi.auth2.getAuthInstance().signIn().then(
function () {
self.isUserLoggedIn(true);
self.userLogin(self.googleAuth.currentUser.get().getBasicProfile().getName());
self.imagePath(self.googleAuth.currentUser.get().getBasicProfile().getImageUrl());
self.id_token = self.googleAuth.currentUser.get().getAuthResponse().id_token;
},
function (e) {
self.isUserLoggedIn(false);
console.log(e);
});
}
...
}



5. We write a function, which will be triggered, when the user is clicking the sign out button. 
  • This is a promise, which is entering the first function, if everything worked fine and the second function, if there was an error.
  • In case of a sign out success We do reset all the user profile informatoin.
  • In the second function, which means, that the sign out was not correct, we just log the exception.

function ControllerViewModel() {
...
self.googleSignOut = function (data, event) {
window.gapi.auth2.getAuthInstance().signOut().then(
function () {
self.userLogin('Unknown');
self.imagePath('');
self.isUserLoggedIn(false);
},
function (e) {
console.log(e);
});
}
...
}




6. We write a function, which will be triggered, when the user is clicking the Disconnect button. The difference between disconnect and sign out is the following. A sign out is just signing out the user from your webapplication. A disconnect is also removing all the rights, that the user gave you. Disconnect is hardly used in this world, but quite interesting for development.
  • This is a promise, which is entering the first function, if everything worked fine and the second function, if there was an error.
  • In case of a disconnect success We do reset all the user profile informatoin.
  • In the second function, which means, that the disconnect was not correct, we just log the exception.

function ControllerViewModel() {
...
self.googleDisconnect = function (data, event) {
window.gapi.auth2.getAuthInstance().disconnect().then(
function () {
self.userLogin('Unknown');
self.imagePath('');
self.isUserLoggedIn(false);
},
function (e) {
console.log(e);
});
}
...
}


Trigger the Javascript functions from HTML

Open the index.html file and make the following adjustments.

1. Add Sign In button and make it only visible, if the user is not logged in

<div data-bind="ifnot: isUserLoggedIn">
<h1>Google Login --- Not LoggedIn</h1>
<button id="google-signin-button"
data-bind="click: googleSignIn, ojComponent: { 
component:'ojButton', label: 'Sign In',
icons: {start:'oj-fwk-icon oj-fwk-icon-signin'},
chroming: 'full'
}">
</button>
</div>





2. Add a Sign out and Disconnect button and make it only visible, if the user is logged in. Also add an image of the logged in user.

<div data-bind="if: isUserLoggedIn">
<h1>Google Login --- LoggedIn</h1>
<button id="google-signout-button"
data-bind="click: googleSignOut, ojComponent: {
component:'ojButton', label: 'Sign Out',
icons: {start:'oj-fwk-icon oj-fwk-icon-signin'},
chroming: 'full'
 }">
</button>
<button id="google-signout-button"
data-bind="click: googleDisconnect, ojComponent: {
component:'ojButton', label: 'Disconnect',
icons: {start:'oj-fwk-icon oj-fwk-icon-signin'},
chroming: 'full'
 }">
</button>
<img data-bind="attr:{src: imagePath}" /> 
</div>




3. Add the username to the page

This is the easiest part. Thanks to the nice basic template of the Oracle Jet webapplication, all we had to do was make updates to the self.userLogin observable, after the sing in has been successfull. Inside the HTML, we didn't have to do any adjustments.




4. Also make the Sign Out link in the upper right corner of the Oracle Jet basic template work.

Add a data-bind to the existing link.

<li id="out"><a data-bind="click: googleDisconnect" href="#">Sign Out</a></li>




Conclusion

As already mentioned in the introduction, it is way more fun to do this, than taking care of all the administrative stuff. 

In the end it is a solution, that makes your user happy, because, they don't have to remember a new username/password combination and it makes you happy, because you don't have to take care of usermanagement.

You also get the id_token of the user, which opens you a whole new world to the Google world (YouTube, Maps, ...). This is basically just the beginning. And it is working like a charme with Oracle Jet, because both Google and Oracle support OAuth2.0.

In the following blog, we will take a look on how to make this work with a hybrid application. We will need a cordova plugin for that.

Google Login with Oracle Jet - Part 1 - Create Google Project

Introduction

Our goal in this series of three blogs is to make Google Login available with an Oracle Jet webapplication and with an Oracle Jet hybrid application.



Android Virtual Device - Oracle Jet Hybrid application




Chrome Browser - Oracle Jet WebApplication - Not Logged In - User Clicks Sign In



Chrome Browser - Oracle Jet WebApplication - Not Logged In - User has to give access rights to the webapplication




Chrome Browser - Oracle Jet WebApplication - User is logged in - Profile information (name, picture) available and user can sign out.



In this and the following two blogs we will talk about the following

  1. This blog (Google Login Part 1 blog): Create Google Project and OAuth2 ClientID
  2. Google Login Part 2 blog: Make Google Login with Oracle Jet Webapplication work
  3. Google Login Part 3 blog: Make Google Login with Oracle Jet hybrid appliation work

Create Project

If we want to use the Google Login mechanism, we have to create our own project on the Google cloud console.

1. Enter the following URL and click on Create Project

Choose whatever name you want for the project




Enable Google+ API

If we want to use the Google Login mechanism, we have to use the functions of the Google+ API. That's why we have to enable it next.

2. It usually takes a minute until the project is created. You will automatically get forwarded to the following page, after the creation of the project is finished.




Click on enable and Mange API






3. Now click on Library (left navigation bar) and after that click on Google+ API






4. Click on Enable




Create OAuth2 ClientID

The client ID is needed to allow the communication between the Oracle Jet webapplication and the Google+ API.

5. Do the following steps
  • Click on Credentials (left navigation bar)
  • Click on Create Credentials
  • Select OAuthClientID




6. Fill out the form with the following values
  • Application Type: web application
  • Name: [WHATEVER_NAME_YOU_WANT]
  • Authorised Javascript origins: Here you have to add all the webpages that should be allowed to use this clientId
    • http://localhost
    • http://localhost:8383 [Oracle Jet local server]
    • http://[YOUR_PRODUCTIVE_DOMAIN]
  • Click on Create






7. An OAuth 2.0 Client ID has been created and should be visible. 
It has the format [LONG_UNIQUE_ID].apps.googleusercontent.com

ATTENTION: This id will be needed in the future. I will reference to it as the GoogleLogin Web ClientID








Other

8. As the last step you have to click on OAuth consent screen. Here you have to define the application name that will be visible to the enduser, when he has to give the rights to your app. (Checkout first picture in this blog as example - Android virtual device screenshot)

Enter as Product name whatever you want


Conclusion

Now after creating a Google project and an OAuth client id for our project we can move on to the next blog, which will tell us how to use this client id to make Google Login possible with Oracle Jet webapplication. 

Montag, 3. Oktober 2016

Enable Debug and LiveReload functionality in OracleJet Android application

Introduction

OracleJet YeoMan Generator: version 1.1
OracleJet: version 2.1.0

Debug and LiveReload (aka HotDeployment) are two very powerful functions. You should always try to make this possible in your development environment, no matter what framework you are using. Debug saves you a lot of time in finding bugs and LiveReload allows you to see your changes to the sourcecode after seconds instead of waiting for another redeployment, which can take minutes.

What is going on behind the scenes


To make this happen, there is a lot going on. But don't worry, all of this is already setup. The only thing you have to do is setup an Google Android Virtual Device, start the Virtual Device and run the following scripts.

To find out the name of your Virtual Device
$ adb devices

To start the process, that is pointed out in the picture above
$ grunt serve:dev --platform=android --destination=<NAME_OF_YOUR_AVD_DEVICE>

Here comes an explanation of the steps, shown in the picture
  1. Your source code is being build and moved to the hybrid folder, which contains the informatione needed to make cordova work
  2. A server is being started on your local machine with port 8000. It is important, that this server is running on port 8000. You can also check that this step has been working fine, by entering localhost:8000 in your browser. You should see something similar to the following picture.
  3. This server is probably pointing to the hybrid folder. But I didn't make research on that. The important thing is, that server is always synchrone with your source code.
  4. Next your app is being deployed to the Virtual Device. Again, you don't have to trigger any separate command to make this happen. All this is being done by using the "grunt serve" command.
  5. Here comes the interesting part.Your Virtual Android Device is always listening to http://10.0.2.2/8000 for changes. 





Known Issues

Failed to establish connection to ADB server

When starting the Google Android Virtual Device you get this error in the log.

Solution
  1. Go to AVD Manager
  2. Edit your AVD
  3. Uncheck 'Use Host GPU'option.
  4. Click Finish (Save).
  5. Then Start AVD again


ERR_EMPTY_RESPONSE

In my case, my local Google Cloud Endpoint server's Debug Port has been listening on port 8000. Check on your computer, if there are any other actions going on on this port.

Conclusion

It took me quite some hair to find out why this is working the way it is. Why is 10.0.2.2 pointing directly to the localhost of my development environmen although I never setup my development environment or anything else to make 10.0.2.2 my localhost.

The reason why this is working is because Google did setup their Android Virtual Device in a way, that 10.0.2.2 is always forwarding the request to the localhost of your development environment.

Here comes the big problem with this. If you want to use your own Android device or some other Virtual Machine, than this is not working, because they don't forward 10.0.2.2 to your localhost development environment. Genymotion, which is another famous Android Virtual Device software is using 10.0.3.2 as IP to forward to your localhost development environment. And if you would want to make this work from your real device, than you would have to take the IP of your development environment and point to that from device, instead of pointing to 10.0.2.2.

I did make some research and found out, that currently the 10.0.2.2 can't be changed and is a hardcoded value. This means, that currently you can only use the debug and liveReload functionality with the original Google Android Virtual Devices

Sonntag, 2. Oktober 2016

OracleJet with Google Cloud Endpoints and Google Cloud SQL

Introduction

After I did implement some basic parts of my Android application with Oracle Jet, I had the issue, that my Android application was keeping all the needed information locally on the Smartphone. So my next goal was to implement a REST services which have functions to create and read existing information.

Part 1: SignIn with Google

Since I didn't want unauthorized access to my future REST services I have been first thinking about how to make this access happen. I didn't want my users to remember another password, so I decided to pick the Google Login, which should secure my future REST services.

Let me explain the steps real fast

  1. The webapplication is asking for the users request token, which can be later used to authenticate the user. Remember that you need a client_id for your webapplication/smartphone app and have to send that with your request.
  2. Google is showing a popup to the user, which is asking him to identify himself by using his username and his password.
  3. After successful identification of the user, authorization code is being sent to the webapplication
  4. This authorization code is being sent by the webapplication to Google again.
  5. In exchange the webapplication is receiving the token, which contains basic information (email, name, pictureurl, ...) about the user and how long the token is valid.
  6. The token is being used by the webapplication to make all future REST service requests to the Google Cloud Endpoint. The Google Cloud Endpoint is validating the token, which ensures, that only authenticated users have access to my REST services. Currently I don't have an authorization. Everybody is allowed to user all REST services.




Part 2 - Oracle Jet with Google Cloud Endpoints and Google Cloud SQL



It was important for me to use some standards like JPA or MySQL on Google Cloud Endpoint side, cause this will make it easier for me to migrate to another Cloud service, in case I should ever choose to leave the Google Cloud.


Conclusion

I had to do some reading to get into all the Google Cloud stuff. Now that I got my architecture running, it shouldn't be too hard to add extra functionality, or build other web applications, based on this architecture.