我正在尝试为一个基于电子的桌面应用程序创建一个NodeJS应用程序接口系统。应用程序应该允许使用用户名和密码进行基本登录,在用户登录后,所有后续API调用都将通过ids和密钥进行身份验证。我引用的是this guide on using express-strompath description here。我无法像这样执行用户名/密码身份验证。
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();
}
}));
发布于 2016-06-21 05:43:09
我在Stormpath工作。API密钥身份验证功能旨在用于服务器到服务器客户端,而不是web浏览器客户端。因此,API密钥旨在与HTTP Basic Authentication一起使用,或者用于交换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
网页浏览器客户端应该使用/login端点,它们会收到一个Oauth2访问和刷新令牌作为响应(我们将这些内容存储在安全cookies中)。这是express-stormpath的一个特殊细微差别。您可以使用oauth/token端点进行正确的OAuth2密码交换,如下所述:
http://docs.stormpath.com/nodejs/express/latest/authentication.html#oauth2-password-grant
我希望这个答案能有所帮助!
发布于 2016-06-22 18:24:51
对于基于angularJS的基于web的客户端,我们可以使用/login端点,并允许浏览器管理它发送的cookies,使用/oauth/token端点接收访问和刷新令牌。The oauth2 method is described in detail here和here。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);
}
https://stackoverflow.com/questions/37888420
复制相似问题