在处理POST请求时找出谁在Node上通过basicAuth进行了身份验证

强大的鼠标

我正在使用basicAuth来验证特定地址上的POST。

在客户端,我使用以下形式的命令:

$.ajax({
        type: "POST",
        accepts: "text/plain",
        url: "http://localhost:3000/somewhere",
        data: JSON.stringify(something),
        contentType: "application/json; charset=UTF-8", 
        dataType: "json",
        success: function(data) {
            window.alert("Received back: '" + data + "'");
        },
        username: theUsername,
        password: "a password"
    });

从某种意义上说,这很好地工作,即存储在Username中的用户名通过了我在节点上拥有的身份验证机制。在验证用户身份之后,我可以打印console.log语句并查看实际进行身份验证的人(此刻我不在验证密码)。但是,随后实际处理开始于POST请求。但是,到那时,我如何找出原始请求中使用的用户名和密码?我试图查看请求的标头,但那里什么都没看到。

马特·埃施(Matt Esch)

当您收到基本身份验证请求时,您应该能够阅读中的“授权”标头。req.headers.authorization您必须拔出base64编码的凭据,然后对其进行解码。大概在Express中,您使用req.header("authorization")req.get("authorization")

对于一个独立的示例,请看一下https://gist.github.com/charlesdaniel/1686663,我已将其复制到下面以供将来参考

var http = require('http');

var server = http.createServer(function(req, res) {
        // console.log(req);   // debug dump the request

        // If they pass in a basic auth credential it'll be in a header called "Authorization" (note NodeJS lowercases the names of headers in its request object)

        var auth = req.headers['authorization'];  // auth is in base64(username:password)  so we need to decode the base64
        console.log("Authorization Header is: ", auth);

        if(!auth) {     // No Authorization header was passed in so it's the first time the browser hit us

                // Sending a 401 will require authentication, we need to send the 'WWW-Authenticate' to tell them the sort of authentication to use
                // Basic auth is quite literally the easiest and least secure, it simply gives back  base64( username + ":" + password ) from the browser
                res.statusCode = 401;
                res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');

                res.end('<html><body>Need some creds son</body></html>');
        }

        else if(auth) {    // The Authorization was passed in so now we validate it

                var tmp = auth.split(' ');   // Split on a space, the original auth looks like  "Basic Y2hhcmxlczoxMjM0NQ==" and we need the 2nd part

                var buf = new Buffer(tmp[1], 'base64'); // create a buffer and tell it the data coming in is base64
                var plain_auth = buf.toString();        // read it back out as a string

                console.log("Decoded Authorization ", plain_auth);

                // At this point plain_auth = "username:password"

                var creds = plain_auth.split(':');      // split on a ':'
                var username = creds[0];
                var password = creds[1];

                if((username == 'hack') && (password == 'thegibson')) {   // Is the username/password correct?

                        res.statusCode = 200;  // OK
                        res.end('<html><body>Congratulations you just hax0rd teh Gibson!</body></html>');
                }
                else {
                        res.statusCode = 401; // Force them to retry authentication
                        res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');

                        // res.statusCode = 403;   // or alternatively just reject them altogether with a 403 Forbidden

                        res.end('<html><body>You shall not pass</body></html>');
                }
        }
});


server.listen(5000, function() { console.log("Server Listening on http://localhost:5000/"); });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Flask-Login检查是否在没有装饰器的情况下对用户进行了身份验证

来自分类Dev

我应该如何检查用户是否在MVC5中进行了身份验证?

来自分类Dev

通过LDAP进行外部身份验证

来自分类Dev

通过HttpClient进行SPNEGO身份验证

来自分类Dev

通过HTTP进行ArangoDB身份验证

来自分类Dev

BasicAuth的喷雾身份验证方法

来自分类Dev

通过Unirest JAVA进行身份验证

来自分类Dev

通过Atmosphere + Dropwizard进行身份验证

来自分类Dev

通过Google进行OWIN身份验证

来自分类Dev

客户端身份验证方案“匿名”对HTTP请求进行了未经授权的访问(通过C#基本身份验证写入)

来自分类Dev

通过Volley POST请求获取Twitter身份验证令牌

来自分类Dev

401在WebLogic上通过HttpClient进行身份验证时返回

来自分类Dev

错误:7 PERMISSION_DENIED:您的应用已使用Google Cloud SDK中的最终用户凭据进行了身份验证

来自分类Dev

即使在注册流程中对用户进行了身份验证,如何解决Firebase“用户未授权”错误?

来自分类Dev

我进行了用户身份验证,并且在localhost中正常运行,但是在服务器上出现此错误::找不到类'\ app \ models \ User'

来自分类Dev

无法通过GUI进行身份验证

来自分类Dev

FB朋友列表已对同一应用程序进行了身份验证

来自分类Dev

BasicAuth的喷雾身份验证方法

来自分类Dev

VBA是否通过未知代理对“ HTTP GET”请求进行了处理?

来自分类Dev

客户端检查是否在Firebase中进行了身份验证

来自分类Dev

通过POST使用请求库进行python身份验证

来自分类Dev

通过Python中的BeautifulSoup抓取时进行身份验证

来自分类Dev

通过javascript AND凭证提供程序对AWS API Gateway进行了身份验证

来自分类Dev

通过JMeter进行身份验证的SOAP请求

来自分类Dev

检查是否在 ApplicationController 中进行了身份验证,导致“参数数量错误(给定 1,预期为 0)”

来自分类Dev

检查用户是否在不安全的路由上进行了身份验证

来自分类Dev

如何知道用户是否通过 firebase 的第一个请求进行了身份验证

来自分类Dev

使用 cookie 身份验证时,每个请求运行了多少个数据库查询?

来自分类Dev

如何通过身份验证在 swift 上发出 http 请求?

Related 相关文章

  1. 1

    Flask-Login检查是否在没有装饰器的情况下对用户进行了身份验证

  2. 2

    我应该如何检查用户是否在MVC5中进行了身份验证?

  3. 3

    通过LDAP进行外部身份验证

  4. 4

    通过HttpClient进行SPNEGO身份验证

  5. 5

    通过HTTP进行ArangoDB身份验证

  6. 6

    BasicAuth的喷雾身份验证方法

  7. 7

    通过Unirest JAVA进行身份验证

  8. 8

    通过Atmosphere + Dropwizard进行身份验证

  9. 9

    通过Google进行OWIN身份验证

  10. 10

    客户端身份验证方案“匿名”对HTTP请求进行了未经授权的访问(通过C#基本身份验证写入)

  11. 11

    通过Volley POST请求获取Twitter身份验证令牌

  12. 12

    401在WebLogic上通过HttpClient进行身份验证时返回

  13. 13

    错误:7 PERMISSION_DENIED:您的应用已使用Google Cloud SDK中的最终用户凭据进行了身份验证

  14. 14

    即使在注册流程中对用户进行了身份验证,如何解决Firebase“用户未授权”错误?

  15. 15

    我进行了用户身份验证,并且在localhost中正常运行,但是在服务器上出现此错误::找不到类'\ app \ models \ User'

  16. 16

    无法通过GUI进行身份验证

  17. 17

    FB朋友列表已对同一应用程序进行了身份验证

  18. 18

    BasicAuth的喷雾身份验证方法

  19. 19

    VBA是否通过未知代理对“ HTTP GET”请求进行了处理?

  20. 20

    客户端检查是否在Firebase中进行了身份验证

  21. 21

    通过POST使用请求库进行python身份验证

  22. 22

    通过Python中的BeautifulSoup抓取时进行身份验证

  23. 23

    通过javascript AND凭证提供程序对AWS API Gateway进行了身份验证

  24. 24

    通过JMeter进行身份验证的SOAP请求

  25. 25

    检查是否在 ApplicationController 中进行了身份验证,导致“参数数量错误(给定 1,预期为 0)”

  26. 26

    检查用户是否在不安全的路由上进行了身份验证

  27. 27

    如何知道用户是否通过 firebase 的第一个请求进行了身份验证

  28. 28

    使用 cookie 身份验证时,每个请求运行了多少个数据库查询?

  29. 29

    如何通过身份验证在 swift 上发出 http 请求?

热门标签

归档