Uploaded image for project: 'Help-Desk'
  1. Help-Desk
  2. HELP-9495

[fiware-stackoverflow] How to authenticate a java web app with KeyRock?

    Details

    • Type: Monitor
    • Status: Closed
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 2021
    • Component/s: FIWARE-TECH-HELP
    • Labels:

      Description

      Created question in FIWARE Q/A platform on 01-09-2015 at 10:09
      Please, ANSWER this question AT https://stackoverflow.com/questions/32326937/how-to-authenticate-a-java-web-app-with-keyrock

      Question:
      How to authenticate a java web app with KeyRock?

      Description:
      We are trying to create a user authentication in our web app ( that we are developing in Java Spring MVC). For our authentication we want to use the token and user info acquired from the users fiware.lab account on global instance of keyrock.

      Since Keyrock is based on OAuth2 protocol, what is the best approach to use keyrock from our web app?

      Is there a java library that we could use for this purpose?

      Is there a way to integrate spring security or apache oltu?

      Every example would be more than welecome.

      We only have the implementation of node.js but we need a java version of this:

      var express = require('express');
      var OAuth2 = require('./oauth2').OAuth2;
      var config = require('./config');

      // Express configuration
      var app = express();
      app.use(express.logger());
      app.use(express.bodyParser());
      app.use(express.cookieParser());
      app.use(express.session({
      secret: "skjghskdjfhbqigohqdiouk"
      }));

      app.configure(function () {
      "use strict";
      app.use(express.errorHandler(

      { dumpExceptions: true, showStack: true }

      ));
      //app.use(express.logger());
      app.use(express.static(__dirname + '/public'));
      });

      // Config data from config.js file
      var client_id = config.client_id;
      var client_secret = config.client_secret;
      var idmURL = config.idmURL;
      var response_type = config.response_type;
      var callbackURL = config.callbackURL;

      // Creates oauth library object with the config data
      var oa = new OAuth2(client_id,
      client_secret,
      idmURL,
      '/oauth2/authorize',
      '/oauth2/token',
      callbackURL);

      // Handles requests to the main page
      app.get('/', function(req, res){

      // If auth_token is not stored in a session cookie it sends a button to redirect to IDM authentication portal
      if(!req.session.access_token)

      { res.send("Oauth2 IDM Demo.<br><br><button onclick='window.location.href=\"/auth\"'>Log in with FI-WARE Account</button>"); // If auth_token is stored in a session cookie it sends a button to get user info }

      else

      { res.send("Successfully authenticated. <br><br> Your oauth access_token: " +req.session.access_token + "<br><br><button onclick='window.location.href=\"/user_info\"'>Get my user info</button>"); }

      });

      // Handles requests from IDM with the access code
      app.get('/login', function(req, res){

      // Using the access code goes again to the IDM to obtain the access_token
      oa.getOAuthAccessToken(req.query.code, function (e, results)

      { // Stores the access_token in a session cookie req.session.access_token = results.access_token; res.redirect('/'); }

      );
      });

      // Redirection to IDM authentication portal
      app.get('/auth', function(req, res){
      var path = oa.getAuthorizeUrl(response_type);
      res.redirect(path);
      });

      // Ask IDM for user info
      app.get('/user_info', function(req, res){
      var url = config.idmURL + '/user/';

      // Using the access token asks the IDM for the user info
      oa.get(url, req.session.access_token, function (e, response)

      { var user = JSON.parse(response); res.send("Welcome " + user.displayName + "<br> Your email address is " + user.email + "<br><br><button onclick='window.location.href=\"/logout\"'>Log out</button>"); }

      );
      });

      // Handles logout requests to remove access_token from the session cookie
      app.get('/logout', function(req, res){

      req.session.access_token = undefined;
      res.redirect('/');
      });

      console.log('Server listen in port 80. Connect to localhost');
      app.listen(80);

      Edit 1
      Here is my set up:

      and the end result error I get when I call the token:

        Activity

        Hide
        backlogmanager Backlog Manager added a comment -

        2017-06-06 00:05|CREATED monitor | # answers= 1, accepted answer= True

        Show
        backlogmanager Backlog Manager added a comment - 2017-06-06 00:05|CREATED monitor | # answers= 1, accepted answer= True
        Hide
        backlogmanager Backlog Manager added a comment -

        2017-06-06 03:05|UPDATED status: transition Answer| # answers= 1, accepted answer= True

        Show
        backlogmanager Backlog Manager added a comment - 2017-06-06 03:05|UPDATED status: transition Answer| # answers= 1, accepted answer= True
        Hide
        backlogmanager Backlog Manager added a comment -

        2017-06-06 06:05|UPDATED status: transition Answered| # answers= 1, accepted answer= True

        Show
        backlogmanager Backlog Manager added a comment - 2017-06-06 06:05|UPDATED status: transition Answered| # answers= 1, accepted answer= True
        Hide
        backlogmanager Backlog Manager added a comment -

        2017-06-06 09:05|UPDATED status: transition Finish| # answers= 1, accepted answer= True

        Show
        backlogmanager Backlog Manager added a comment - 2017-06-06 09:05|UPDATED status: transition Finish| # answers= 1, accepted answer= True

          People

          • Assignee:
            backlogmanager Backlog Manager
            Reporter:
            backlogmanager Backlog Manager
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: