feathersjs-ecosystem / authentication-oauth2

[MOVED] OAuth 2 plugin for feathers-authentication
https://github.com/feathersjs/feathers
MIT License
26 stars 15 forks source link

Authentication always logging as first user #77

Closed thomasarbona closed 6 years ago

thomasarbona commented 6 years ago

We are trying to make the auth work with AzureAD, and i keep getting into weird situations where it seems like after one user has been correctly logged in, database and auth wont be updated to a new user afterwards.

Expected behavior

Simple Log in && Log off behavior.

Actual behavior

a. When the database is empty and no-one has logged in previously user gets logged in correctly and everything is registered into the database by feather. Case

b. When there WAS already someone logged in and registered in the database, new connections wont work, you stay logged in as the first user, and the database is not updated. Case

Here is my server code:

module.exports = function (app) {
  const config = app.get('authentication');

  // Set up authentication with the secret
  app.configure(authentication(config));
  app.configure(jwt());
  app.configure(local());

  app.configure(oauth2(Object.assign({
    name: 'azure-ad',
    Strategy: OIDCStrategy,
    passReqToCallback: true,
    successRedirect: 'https://localhost:3000',
    failureRedirect: 'https://localhost:3000/oauth_error',
    entity: 'user',
    service: 'users',
  }, config['azure-ad'])));

  app.service('authentication').hooks({
    before: {
      create: [
        authentication.hooks.authenticate(config.strategies)
      ],
      remove: [
        authentication.hooks.authenticate('jwt')
      ]
    }
  });
};

And my client code:

import feathers from '@feathersjs/feathers';
import socketio from '@feathersjs/socketio-client';
import io from 'socket.io-client';
import auth from '@feathersjs/authentication-client';

const socket = io('https://localhost:3030', {
  transports: ['websocket'],
  forceNew: true,
});

const feathersClient = feathers();

feathersClient.configure(auth({
  storage: window.localStorage,
  cookie: 'feathers-jwt',
}));

feathersClient.configure(socketio(socket));

export default feathersClient;

And finally, my featherJS configuration:

"authentication": {
    "secret": "SECRET",
    "strategies": [
      "jwt",
      "local"
    ],
    "path": "/authentication",
    "service": "users",
    "jwt": {
      "header": {
        "typ": "access"
      },
      "audience": "https://yourdomain.com",
      "subject": "anonymous",
      "issuer": "feathers",
      "algorithm": "HS256",
      "expiresIn": "1d"
    },
    "local": {
      "entity": "user",
      "usernameField": "email",
      "passwordField": "password"
    },
    "azure-ad": {
      "clientID": "CLIENT_ID",
      "clientSecret": "CLIENT_SECRET",
      "identityMetadata": "https://login.microsoftonline.com/901cb4ca-b862-4029-9306-e5cd0f6d9f86/v2.0/.well-known/openid-configuration",
      "scope": ["email", "profile"],
      "responseType": "code",
      "responseMode": "query",
      "redirectUrl": "https://localhost:3030/auth/azure-ad/callback"
    },
    "cookie": {
      "enabled": true,
      "name": "feathers-jwt",
      "httpOnly": false,
      "secure": false
    }
  },

System configuration

Module versions

  "@feathersjs/authentication": "^2.1.6",
    "@feathersjs/authentication-jwt": "^2.0.1",
    "@feathersjs/authentication-local": "^1.2.1",
    "@feathersjs/authentication-oauth2": "^1.0.3",
    "@feathersjs/configuration": "^1.0.2",
    "@feathersjs/errors": "^3.3.0",
    "@feathersjs/express": "^1.2.3",
    "@feathersjs/feathers": "^3.1.6",
    "@feathersjs/socketio": "^3.2.2",
    "compression": "^1.7.2",
    "cookie-parser": "^1.4.3",
    "cors": "^2.8.4",
    "express-session": "^1.15.6",
    "feathers-authentication": "^1.3.1",
    "feathers-mongoose": "^6.1.2",
    "helmet": "^3.12.1",
    "mongoose": "^5.1.4",
    "passport-azure-ad": "^3.0.12",
    "serve-favicon": "^2.5.0",
    "winston": "^2.4.2"

NodeJS version: v10.3.0

Operating System: Ubuntu 16.04

Browser Version: Firefox 60.0.1 (64-bit)

React Native Version: /

Module Loader: RequireJS

daffl commented 6 years ago

You may have to customize the verifier if the default user lookup (by options.idField) doesn't work. It will probably use undefined since it got never set and always return the first entry that matches.