strongloop / loopback-component-oauth2

oAuth 2.0 server for LoopBack
http://www.strongloop.com
Other
62 stars 63 forks source link

It is kinda hard to integrate oauth2 with loopback #83

Closed ghost closed 5 years ago

ghost commented 5 years ago

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
  2. /oauth/authorize, show the username and password fields when unregistered client id passed

sudheer160540 commented 5 years ago

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

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

Kabangi commented 5 years ago

@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

ghost commented 5 years ago

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

Kabangi commented 5 years ago

@samarmeena Thanks

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

stale[bot] commented 5 years ago

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] commented 5 years ago

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.