loopback-component-oauth2 icon indicating copy to clipboard operation
loopback-component-oauth2 copied to clipboard

It is kinda hard to integrate oauth2 with loopback

Open ghost opened this issue 7 years ago • 6 comments

Issue

I have working model of passport with google and github. Now I want to use oauth2 server to authorize external website with my login model.

Expected Working Model

  1. Collect User logins with passport login strategy.
  2. Oauth2 server will use the login details to get user information and than it will authorize external oauth2 request.
  3. the /oauth/authorize and /oauth/token should be working with that

server files

/server/boot/oauth2-server.js

'use strict';

var oauth2 = require('loopback-component-oauth2');
module.exports = function(app) {
  var options = {
    dataSource: app.dataSources.db, // Data source for oAuth2 metadata persistence
    loginPage: '/login', // The login page url
    loginPath: '/login', // The login form processing url
    loginFailPage: '/loginfailed',
    resourceServer: true,
    authorizationServer: true,
    authorizePath: '/oauth/authorize',
    tokenPath: '/oauth/token',
    supportedGrantTypes: [
      'implicit',
      'jwt',
      'clientCredentials',
      'authorizationCode',
      'refreshToken',
      'resourceOwnerPasswordCredentials',
    ],
  };
  oauth2.oAuth2Provider(
    app, // The app instance
    options // The options
  );
};

/server/boot/passport-auth.js

'use strict';
var loopback = require('loopback');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require('body-parser');
var loopbackPassport = require('loopback-component-passport');
var flash = require('express-flash');
module.exports = function(app) {
  var PassportConfigurator = loopbackPassport.PassportConfigurator;
  var passportConfigurator = new PassportConfigurator(app);
  var config = {};
  try {
    config = require('../providers.json');
  } catch (err) {
    console.trace(err);
    process.exit(1); // fatal
  }

  // to support JSON-encoded bodies
  app.middleware('parse', bodyParser.json());
  // to support URL-encoded bodies
  app.middleware('parse', bodyParser.urlencoded({
    extended: true,
  }));

  // The access token is only available after boot
  app.middleware('auth', loopback.token({
    model: app.models.accessToken,
  }));

  app.middleware('session:before', cookieParser('mysecret'));
  app.middleware('session', session({
    secret: 'kitty',
    saveUninitialized: true,
    resave: true,
  }));
  passportConfigurator.init();

  // We need flash messages to see passport errors
  app.use(flash());

  passportConfigurator.setupModels({
    userModel: app.models.user,
    userIdentityModel: app.models.userIdentity,
    userCredentialModel: app.models.userCredential,
  });
  for (var s in config) {
    var c = config[s];
    c.session = c.session !== false;
    passportConfigurator.configureProvider(s, c);
  }
};

Errors

  1. undefined LogIn
this._passport.instance._sm.logIn(this, user, function(err) {
                                ^
TypeError: Cannot read property 'logIn' of undefined
  1. /oauth/authorize, show the username and password fields when unregistered client id passed

ghost avatar Jan 03 '19 19:01 ghost

@dhmlau how to run the "loopback-component-oauth2" with below code repository

https://github.com/strongloop/loopback-component-oauth2.git

sudheer160540 avatar Jan 30 '19 08:01 sudheer160540

@samarmeena

Did you ever get this working?

Which loopback version are you using?

I want to achieve the same and I can't figure out how to go about it. Kindly help if you have ideas.

Thanks

Kabangi avatar Apr 10 '19 08:04 Kabangi

@Kabangi Use new loopback 4,But you have to implement your own oauth2 system, that's what I'm currently working on.

ghost avatar Apr 12 '19 05:04 ghost

@samarmeena Thanks

Yes loopback4 is really awesome. I will implement the same.

Kabangi avatar Apr 14 '19 10:04 Kabangi

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 17 '19 17:09 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Oct 01 '19 17:10 stale[bot]