OAuth Authentication

The SDK provides a basic set of tools to allow creating an OAuth authentication flow.
It is based on the Google OAuth Java Client library: https://developers.google.com/api-client-library/java/google-oauth-java-client.

Usage

Get Client ID and Secret

Before you are able to use Geens OAuth authentication you need to request a clientID and clientSecret. You can do this by contacting us and requesting it. ([email protected])

Create a receiver

The receiver is a java object that will wait until the required code and privateKey is received from Geens and it must be a subclass of GeensVerificationCodeReceiver.
After a user authenticates himself on the Geens AuthorizationUrl, a code will be sent to the redirectUri. The most logical implementation of this receiver is listening on this uri and returning the code and privateKey when it is received. These values are sent to the redirect uri as url parameters:

----- Request Start ----->
/favicon.ico
Host: localhost
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://localhost/?code={{code}}&private={{privateKey}}
Accept-Encoding: gzip, deflate, br
Accept-Language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6
Cookie: snatched_view=list; soon_view=thumb; suggest_view=thumb; late_view=list; blu_ray_com_new_releases_view=thumb; imdb_box_office_view=thumb; imdb_top_dvd_rentals_view=thumb; wanted_view=thumb; advanced_toggle_checked=1; manage_view=thumb; user="2|1:0|10:1553802174|4:user|44:ZTYxYTYwODFmMzliNGQzNzllMzBhYTAwODg5OTE2ZjA=|1e7008766b658d6aa36285c962a72dfa81fa526aacba704a93bb2935f924a77a"; foo=bar

<----- Request End -----

The code below is a mockup of an implementation of the GeensVerificationCodeReceiver. A custom solution should be written depending on where the redirectUri is pointing to.

GeensVerificationCodeReceiver receiver = new GeensVerificationCodeReceiver() {
            public String getRedirectUri() throws IOException {
                return redirectUri;
            }

            public String waitForCode() throws IOException {
                return code;
            }

            public GeensOAuthCredentials waitForCredentials() throws IOException {
                return new GeensOAuthCredentials(code, privateKey);
            }

            public void stop() throws IOException {

            }
        };

Generate the Authentication url

To generate the authorization url you need to instantiate a GeensAuthentication object with your clientId and clientSecret. Also the previously created receiver, the base url of the backend and a custom keystore can be passed as arguments.

KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(null, null);
GeensVerificationCodeReceiver receiver = GeensVerificationCodeReceiverImpl.getInstance();
GeensAuthentication geensAuthentication = new GeensAuthentication(clientId, clientSecret, receiver, "https://beta.geens.com/", keystore);
AuthorizationCodeRequestUrl authorizationUrl = geensAuthentication.getAuthorizationUrl();

Browse to the Authentication url and let the user provide authorization to your application.

Browse to the AuthorizationCodeRequestUrl provided in the previous step. This will direct the user to a login screen and will request him to provide authorization for your application.

Below an example on how this can be achieved in java, though your application might require another approach.

  protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
        String url = authorizationUrl.build();
        Preconditions.checkNotNull(url);
        this.browser.browse(url);
    }

    public static void browse(String url) {
        Preconditions.checkNotNull(url);
        System.out.println("Please open the following address in your browser:");
        System.out.println("  " + url);

        try {
            if (Desktop.isDesktopSupported()) {
                Desktop desktop = Desktop.getDesktop();
                if (desktop.isSupported(Action.BROWSE)) {
                    System.out.println("Attempting to open that address in the default browser now...");
                    desktop.browse(URI.create(url));
                }
            }
        } catch (IOException var2) {
            LOGGER.log(Level.WARNING, "Unable to open browser", var2);
        } catch (InternalError var3) {
            LOGGER.log(Level.WARNING, "Unable to open browser", var3);
        }

    }

Receive the code

Use your receiver to capture the code and private key sent by the Geens OAuth Server.

GeensOAuthCredentials creds = geensAuthentication.waitForCredentials();
receiver.stop();
String code = creds.getCode();
String privateKey = creds.getPrivateKey();

Request an Access Token

After receiving a code, the access token can be requested. This access token should be used to authenticate each API call to the Geens servers and is necessary to access its resources.

TokenResponse response = geensAuthentication.newTokenRequest(code);
String token = response.getAccessToken();

Initialise the GeensClient

After receiving the access token you can initialise the GeensClient. This object will be used to interact with the Geens service.

GeensClient geensClient = new GeensClient(geensAuthentication);