Coder Social home page Coder Social logo

ninja-oauth-google's Introduction

Google-Oauth2 module for Ninja framework

This is a plugable module for the Ninja web framework which supports Google oauth2 authentication.

Build Status license Maven Central

Setup

  1. Add the ninja-google-oauth dependency to your pom.xml:

    net.binggl ninja-google-oauth-module x.x.x
  2. Google Developer Console

Setup a new client ID of type 'Web application'. The 'Client ID' and 'Client secret' are used in the application.conf.

## oauth authentication
ninja.oauth.google.key=KEY
ninja.oauth.google.secret=SECRET
ninja.oauth.callback.url=http://localhost:8080/oauth2callback
ninja.oauth.success.url=http://localhost:8080/
ninja.oauth.failure.url=http://localhost:8080/login
  1. Setup the module
package conf;

import com.google.inject.AbstractModule;
import net.binggl.ninja.oauth.NinjaOauthModule;

public class Module extends AbstractModule {

    @Override
    protected void configure() {
        install(new NinjaOauthModule());
    }

}
  1. Add routes

The callback route needs to match the URL specified in the Google Developer Console!

public void init(Router router) {  

    // authentication routes
    router.GET().route("/startauth").with(NinjaOauthController.class, "startauth");
    router.GET().route("/oauth2callback").with(NinjaOauthController.class, "oauth2callback");
    
    ///////////////////////////////////////////////////////////////////////
    // Index / Catchall shows index page
    ///////////////////////////////////////////////////////////////////////
    router.GET().route("/.*").with(HomeController.class, "index");
}

To start the authentication process call the URL /startauth. A redirect is created in the browser and you are forwarded to the Google login and OAuth consent screen.

  1. Authorization logic

The module is only responsible for authentication using Google Oauth2. The authorization process needs to be implemented.

protected void configure() {
        
    bind(OauthAuthorizationService.class).toInstance(new OauthAuthorizationService() {
        @Override
        public boolean lookupAndProcessProfile(Context context, Google2Profile profile) {
            boolean profileValid = false;
            if(profile != null && StringUtils.isNotEmpty(profile.getAccessToken())) {
                profileValid = true;
                context.getSession().put("id", profile.getId());
            }
            return profileValid;
        }
    });        
}

ninja-oauth-google's People

Contributors

bihe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.