0

我正在尝试为基于电子的桌面应用程序创建 NodeJS API 系统。该应用程序应允许使用用户名和密码进行基本登录,用户登录后,所有后续 API 调用都通过 id 和密钥进行身份验证。我在此处参考了有关使用 express-strompath 描述的指南。我无法像这样执行用户名/密码验证。

curl -L -H "Content-Type: application/json" -X POST -d '{"password":"Som3Pa55Word", "username":"user@domain.ai"}' http://ec2-54-88-168-7.compute-1.amazonaws.com:8000/api/v1.0/login

或通过 javascript

function Login() {
      ...
      var user = {
        username: 'user@domain.ai',
        password: 'Som3Pa55Word ',
      }
      var req = {
        method: 'POST',
        url: apiBaseUrl + '/login',
        headers: {
          'Content-Type': 'application/json'
        },
        data: user
      }
      return $http(req).then(handleSuccess, handleError);
    }

但是,当我使用 API 密钥时,我可以登录。

curl -L -H "Content-Type: application/json" -X POST --user ABCDEFGHIJKLMNOPQRSTUVWXYZ:AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz -d '{}' http://ec2-54-88-168-7.compute-1.amazonaws.com:8000/api/v1.0/login

或通过 javascript

      var url = apiBaseUrl + '/login';
      var sp_api_key_id = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
      var sp_api_key_secret ='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';

      get the Resource object w/ custom "get" method
      $resource(url, {}, {
        get: {
          method: 'GET',
          headers: {
            Authorization: 'Basic ' +
              $base64.encode(sp_api_key_id + ':' + sp_api_key_secret)
          }
        }
      }).get().then(function(result) {
        console.log("Loging Success")
      });

显然,让用户将他们的 API 密钥输入到表单中进行身份验证是不方便的。我想知道为什么stormpath-express 接受API ID/Secret 组合但不接受用户名/密码。

这是我的 nodejs 服务器的代码

router.post('/login', stormpath.loginRequired,  function (req, res, next) {
    /*
    * If we get here, the user is logged in.  Otherwise, they
    * were redirected to the login page
    */
    var respnse = {
      message: 'If you can see this page, you must be logged into your account!'
    }
    res.json(respnse);
    res.status(200);
  });

和 Stormpath 设置代码

// Config
app.use(stormpath.init(app, {
  // TODO
  // apiKeyFile: './app/config/stormpath_apikey.properties',
  application: 'https://api.stormpath.com/v1/applications/ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  secretKey: settings.stormpath_secret_key,
  web: {
    login: {
      enabled: false
    },
    register: {
      uri: '/user/register'
    },
    preLoginHandler: function (formData, req, res, next) {
      console.log('Got login request', formData);
      next();
    }
  },

  postLoginHandler: function (account, req, res, next) {
    console.log('User:', account.email, 'just logged in!');
    next();
  }
}));

4

2 回答 2

0

对于基于 angularJS 的基于 Web 的客户端,我们可以使用 /login 端点,并允许浏览器管理它发送的 cookie,使用 /oauth/token 端点接收访问和刷新令牌。oauth2 方法在此处此处进行了详细描述。oauth2 方法允许将其 API 密钥一次性交换为访问令牌。此访问令牌是有时间限制的,必须定期刷新。

function Login(user) {
      var apiBaseUrl = "https://api.stormpath.com/v1/applications/ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      var sp_api_id = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
      var sp_api_secret = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
      var req = {
        method: 'POST',
        url: apiBaseUrl + '/oauth/token',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          Authorization: 'Basic ' + $base64.encode(sp_api_id + ':' + sp_api_secret)
        },
        data:  'grant_type=password&username=' + user.email + '&password=' + user.password
      }
      return $http(req).then(handleSuccess, handleError);
    }

于 2016-06-22T10:24:51.203 回答
0

我在Stormpath工作。API 密钥身份验证功能(向帐户颁发 API 密钥)旨在用于服务器到服务器客户端,而不是 Web 浏览器客户端。因此,API 密钥旨在与 HTTP 基本身份验证一起使用或用于交换 OAuth2 访问令牌。本文档中描述了这两种策略:

http://docs.stormpath.com/nodejs/express/latest/authentication.html#http-basic-authentication

http://docs.stormpath.com/nodejs/express/latest/authentication.html#oauth2-client-credentials

Web 浏览器客户端旨在使用/login端点,它们会收到 Oauth2 访问和刷新令牌作为响应(我们将这些存储在安全 cookie 中)。这是 express-stormpath 的特殊细微差别。您可以使用 oauth/token 端点进行正确的 OAuth2 密码交换,此处也对此进行了描述:

http://docs.stormpath.com/nodejs/express/latest/authentication.html#oauth2-password-grant

我希望这个答案有帮助!

于 2016-06-20T21:43:08.850 回答