Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strategy callback never called #39

Closed
malinosqui opened this issue Jul 5, 2017 · 4 comments
Closed

Strategy callback never called #39

malinosqui opened this issue Jul 5, 2017 · 4 comments

Comments

@malinosqui
Copy link

malinosqui commented Jul 5, 2017

I have this code

const express = require('express');
const app = express();
const passport = require('passport');
const Strategy = require('openid-client').Strategy;
const Issuer = require('openid-client').Issuer;
var cookieSession = require('cookie-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

const issuer = new Issuer({
  issuer: 'http://localhost:5000',
  authorization_endpoint: 'http://localhost:5000/connect/authorize',
  token_endpoint: 'http://localhost:5000/connect/token',
  userinfo_endpoint: 'http://localhost:5000/connect/userinfo',
  jwks_uri: 'http://localhost:5000/.well-known/openid-configuration/jwks'
});

const client = new issuer.Client({
  client_id: 'xxx',
  client_secret: 'xxx'
});

const params = {
  "redirect_uri": "http://localhost:3000/users",
  "response_type": "code id_token token",
  "scope": "xxx openid profile",
  "authentication_type": "xxx"
};

passport.use('oidc', new Strategy({ client, params }, function (tokenset, userinfo, done) {
  console.log('tokenset', tokenset);
  console.log('access_token', tokenset.access_token);
  console.log('id_token', tokenset.id_token);
  console.log('claims', tokenset.claims);
  console.log('userinfo', userinfo);

  return done(null, false);
}));

app.use(cookieParser());
app.use(bodyParser());
app.use(session({ secret: 'xxx', key: 'user', cookie: { maxAge: 60000, secure: false } }));
app.use(passport.initialize());
app.use(passport.session());

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.get('/login', function (req, res) {
  console.log(req.isAuthenticated());
  res.send('Login');
});

app.get('/users', (req, res) => {
  console.log(req.isAuthenticated());
  res.send('aeae');
});

app.get('/auth',
  passport.authenticate('oidc'));

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
})]

everthing works fine, but my Strategy Callback is never called, so the code console.log(req.isAuthenticated()); always returns false!

@panva
Copy link
Owner

panva commented Jul 5, 2017

First off, your redirect_uri route handler /users doesn't have a call to passport.authenticate (i.e. consume the callback). Second, your verify function alwayrs returns done(null, false), so this code will never work for you. Please see the usage of openid-client passport strategy

@panva panva closed this as completed Jul 5, 2017
@malinosqui
Copy link
Author

malinosqui commented Jul 5, 2017

Thanks for the answer @panva, but if I put passport.authenticate in my route handle /users, I have a loop of redirect. And still not firing callback

@panva
Copy link
Owner

panva commented Jul 5, 2017

heh, ok i see now. You're using a code id_token token response_type, which means response_mode=fragment. The backend service has no clue about the return parameters. Just use response_type=code. You still need the passport.authenticate with success and failure routes in the callback handler

@panva
Copy link
Owner

panva commented Jul 5, 2017

Alternatively change your response_mode to form_post if your OP supports it.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants