使用socket.io-client对Featherjs进行身份验证

时间:2019-03-26 18:13:18

标签: node.js socket.io feathersjs feathers-authentication

如何使用直接连接(https://docs.feathersjs.com/api/client/socketio.html#authentication)向Feathersjs(https://docs.feathersjs.com/api/client/socketio.html#direct-connection)进行身份验证?下面的代码说我的accessToken格式错误,但是我怀疑还有更多的功能可以使它起作用。我在哪里获取accessToken?

app.js(客户端):

import express from 'express';

const socket = require('socket.io-client')('http://localhost:3030', {
  transports: ['websocket']
});

socket.emit('authenticate', { 
  strategy: 'jwt',
  accessToken: 'what to enter here'
}, (message: any, data: any) => {
  console.log(message);
  console.log(data);
});

const app = express();

app.get('/', (req, res) => res.send('Up and running!'));

app.listen(4390, () => console.log('Example app listening on port 4390!'));

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');

  // Set up authentication with the secret
  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')
      ]
    }
  });
};

我尝试使用机密作为accessToken,但没有成功:) default.json(羽毛服务器配置)

"authentication": {
    "secret": "r323r32rada86700f18d82ea1d3e74fb58141dbefcd7460ef71736759265e347151a12d68dff50aa59c05e2f18db88661be8ae91a3f12932fddea8891b5ca9f63b5e4fc650edabc59d0d14e8fe4ea54256e7e386845321ab58a320a9ec99438bd058a3fbbda65dadf97bc9585ea82f72201f932beqwdqwdqwd5761a0d0be3e95474db5b9c8a3f4c7303beed0344a1768ba5dad6a1c916d183ea5dd923587768661ff0b08f25ed85dff4ff4e6b58327fe5914e5e7fb2356ee67754b102434f22686444a35fc38c75bcdd6386240a22e0cf62bdc7f227200868da387174b365af2afa7dec378c4ccf22956b134a3ec961fd1ba8d3dc85a7594ab711",
    "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"
    }
  },
...

感谢所有回复!

2 个答案:

答案 0 :(得分:1)

为了获得file '/tmp/somefile' do content <<-EOF.gsub(/^\s+/, '') some line here some other line here more lines EOF end ,您通常需要使用accessTokenemail/password的策略进行身份验证。这将返回一个oauth,您可以将其用于accessToken身份验证。

另一种方法是使用{{3}},这将使您拥有一个共享的秘密,两个服务器都可以使用该秘密相互通信。

答案 1 :(得分:0)

谢谢@mchaffe!我设法在您的帮助下解决了这个问题。这是使用的代码:

import dotenv from 'dotenv';

// Load environments
const config = dotenv.config()
if (config.error) throw config.error

const io = require('socket.io-client');
const feathers = require('@feathersjs/client');
const localStorage = require('localstorage-memory');

const client = feathers();

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

client.configure(feathers.socketio(socket), {
  timeout: 10000
});

client.configure(feathers.authentication({
  jwtStrategy: 'jwt',
  storage: localStorage,
  storageKey: 'some-token'
}));

const payload = {
  strategy: 'local',
  email: process.env.FEATHERS_AUTHENTICATION_EMAIL,
  password: process.env.FEATHERS_AUTHENTICATION_PASSWORD
};

client.authenticate(payload).then((response: any) => {
  // Do stuff to hooray here
  console.log('Access Token: ' + response.accessToken);

  // Works!
  socket.emit('get', 'logger', 1, (error: any, log: any) => {
    console.log('Found log: ' + JSON.stringify(log));
  });

}).catch((e: any) =>  {
  console.log('Error: ' + e); 
});

如果您有改进的建议,我会全力以赴! :)似乎我可以使用socket.emit方法从数据库访问数据。我需要验证返回的accessToken吗?再次感谢!