Feathersjs — несколько конечных точек аутентификации

#feathersjs #feathers-authentication

#feathersjs #feathers-аутентификация

Вопрос:

У меня есть входящие подключения от двух разных клиентов (angular client и node.js клиент feathers) и я хочу, чтобы они использовали две разные конечные точки аутентификации (на основе данных в двух отдельных таблицах). Одна должна проходить аутентификацию в службе / users, а другие — в службе / users2.

Как этого можно достичь?

Вот как это работает с одной конечной точкой аутентификации:

 // default.json
"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"
    }
  }

// authentication.js
const authentication = require('@feathersjs/authentication');
const jwt = require('@feathersjs/authentication-jwt');
const local = require('@feathersjs/authentication-local');

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

  app.configure(authentication(config));
  app.configure(jwt());
  app.configure(local());

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

};
  

Спасибо!

Ответ №1:

Не уверен, было ли это возможно в прошлом, но с текущим FeathersJS (4.5.0) вы можете создать несколько экземпляров AuthenticationService с разными конфигурациями:

 //default.json
"authentication": {
  "entity": "user",
  "service": "users",
  "secret": ** ** ** ** ** * ,
  "authStrategies": [
    "jwt",
    "local"
  ],
  ...
},
"authentication": {
  "entity": "user2",
  "service": "users2",
  "secret": ** ** ** ** ** * ,
  "authStrategies": [
    "jwt",
    "local"
  ],
  ...
},
...


// authentication.ts
...
export default function(app: Application) {
  const authentication = new AuthenticationService(app, 'authentication');
  authentication.register('jwt', new JWTStrategy());
  authentication.register('local', new LocalStrategy());
  app.use('/authentication/users', authentication2);

  const authentication2 = new AuthenticationService(app, 'authentication2');
  authentication2.register('jwt', new JWTStrategy());
  authentication2.register('local', new LocalStrategy());
  app.use('/authentication/users2', authentication2);

  app.configure(expressOauth());
}
...

// user.hooks.ts / user2.hooks.ts
import * as feathersAuthentication from '@feathersjs/authentication';
import * as local from '@feathersjs/authentication-local';
// Don't remove this comment. It's needed to format import lines nicely.

const {
  authenticate
} = feathersAuthentication.hooks;
const {
  hashPassword,
  protect
} = local.hooks;

export default {
  before: {
    all: [],
    find: [authenticate('jwt')],
    get: [authenticate('jwt')],
    create: [hashPassword('password')],
    update: [hashPassword('password'), authenticate('jwt')],
    patch: [hashPassword('password'), authenticate('jwt')],
    remove: [authenticate('jwt')]
  },

  after: {
    all: [
      // Make sure the password field is never sent to the client
      // Always must be the last hook
      protect('password')
    ],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};