简体   繁体   English

通过Ajax进行静态身份验证

[英]Restful Authentication Through Ajax

I am implementing a restful api and I wondering is it possible to securely authenticate using ajax without exposing the authenentication credientials? 我正在实现一个宁静的api,我想知道是否可以使用ajax安全地进行身份验证而不暴露身份验证凭证? The credentials that will be used are the same ways that will be passed using curls: 将使用的凭证与使用curls传递的方式相同:

curl_setopt($ch, CURLOPT_USERPWD, 'username:password');

The will be received using: 将使用以下方式接收:

$_SERVER['PHP_AUTH_USER']
$_SERVER['PHP_AUTH_USER'];
$_SERVER['PHP_AUTH_PW'];

Is this possible and if so, how? 这可能吗?如果可以,怎么办?

No. This is the exact purpose and use case for implementing Oauth . 否。这是实现Oauth的确切目的和用例。

Using Basic HTTP Authorization will expose the credentials especially from ajax, because even if you run everything through HTTPS , it will still be available in the client code (before any request is made). 使用基本HTTP授权将特别从ajax公开凭据,因为即使您通过HTTPS运行所有内容,它也将在客户端代码中可用(在发出任何请求之前)。

If you are using this api internally only (not expecting third party developers to utilize it), you might attempt to use a "API Key" instead of the plain text username and password. 如果您仅在内部使用此api(不希望第三方开发人员使用它),则可能会尝试使用“ API密钥”代替纯文本用户名和密码。 This way the key can be regenerated if ever compromised, and never expose your users (likely) reused passwords. 这样,一旦遭到破坏,就可以重新生成密钥,并且永远不会向您的用户公开(可能)重用的密码。

I've found that API key + HTTPS covers a lot of situations like this. 我发现API密钥+ HTTPS涵盖了很多这样的情况。

You just generate a unique hash and then use that to authenticate the user... the Basecamp API uses the API key as the username. 您只需生成一个唯一的哈希,然后使用该哈希来对用户进行身份验证... Basecamp API使用API​​密钥作为用户名。

curl_setopt($ch, CURLOPT_USERPWD, '<API KEY>:x');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM