code

Node 및 Express 4를 사용한 기본 HTTP 인증

codestyles 2020. 9. 2. 18:38
반응형

Node 및 Express 4를 사용한 기본 HTTP 인증


Express v3로 기본 HTTP 인증을 구현하는 것은 사소한 것 같습니다.

app.use(express.basicAuth('username', 'password'));

버전 4 (저는 4.2를 사용하고 있습니다)는 basicAuth미들웨어를 제거 했기 때문에 약간 갇혀 있습니다. 다음 코드가 있지만 브라우저가 사용자에게 자격 증명을 요구하지 않습니다. 이것이 내가 원하는 것입니다 (그리고 이전 방법이했던 것이라고 생각합니다).

app.use(function(req, res, next) {
    var user = auth(req);

    if (user === undefined || user['name'] !== 'username' || user['pass'] !== 'password') {
        res.writeHead(401, 'Access invalid for user', {'Content-Type' : 'text/plain'});
        res.end('Invalid credentials');
    } else {
        next();
    }
});

바닐라 JavaScript (ES6)를 사용한 단순 기본 인증

app.use((req, res, next) => {

  // -----------------------------------------------------------------------
  // authentication middleware

  const auth = {login: 'yourlogin', password: 'yourpassword'} // change this

  // parse login and password from headers
  const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
  const [login, password] = new Buffer(b64auth, 'base64').toString().split(':')

  // Verify login and password are set and correct
  if (login && password && login === auth.login && password === auth.password) {
    // Access granted...
    return next()
  }

  // Access denied...
  res.set('WWW-Authenticate', 'Basic realm="401"') // change this
  res.status(401).send('Authentication required.') // custom message

  // -----------------------------------------------------------------------

})

참고 :이 "미들웨어"는 모든 핸들러에서 사용할 수 있습니다. next()논리를 제거 하고 반전하십시오. 아래의 1 개 문 예 또는 이 답변 편집 내역 을 참조하십시오.

왜?

  • req.headers.authorization" Basic <base64 string>" 값을 포함 하지만 비어있을 수도 있으며 실패하지 않기를 원하므로|| ''
  • 노드는 알 수 없습니다 atob()btoa()따라서,Buffer

ES6-> ES5

const그냥 var.. 일종의
(x, y) => {...}그냥 하나에 function(x, y) {...}
const [login, password] = ...split()두 개의 var할당입니다

영감의 원천 (패키지 사용)


위는 매우 짧고 플레이 그라운드 서버에 빠르게 배포 할 수 있도록 고안된 매우 간단한 예제입니다 . 그러나 주석에서 지적했듯이 암호에는 콜론 문자도 포함될 수 있습니다 . b64auth 에서 올바르게 추출하려면 이것을 사용할 수 있습니다. :

  // parse login and password from headers
  const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
  const strauth = new Buffer(b64auth, 'base64').toString()
  const splitIndex = strauth.indexOf(':')
  const login = strauth.substring(0, splitIndex)
  const password = strauth.substring(splitIndex + 1)

  // using shorter regex by @adabru
  // const [_, login, password] = strauth.match(/(.*?):(.*)/) || []

하나의 문에서 기본 인증

... 반면에 로그인을 한 번만 사용하거나 거의 사용 하지 않는 경우 필요한 최소한의 사항입니다. (인증 정보를 전혀 구문 분석 할 필요도 없습니다)

function (req, res) {
//btoa('yourlogin:yourpassword') -> "eW91cmxvZ2luOnlvdXJwYXNzd29yZA=="
//btoa('otherlogin:otherpassword') -> "b3RoZXJsb2dpbjpvdGhlcnBhc3N3b3Jk"

  // Verify credentials
  if (  req.headers.authorization !== 'Basic eW91cmxvZ2luOnlvdXJwYXNzd29yZA=='
     && req.headers.authorization !== 'Basic b3RoZXJsb2dpbjpvdGhlcnBhc3N3b3Jk')        
    return res.status(401).send('Authentication required.') // Access denied.   

  // Access granted...
  res.send('hello world')
  // or call next() if you use it as middleware (as snippet #1)
}

추신 : "보안"경로와 "공개"경로가 모두 필요합니까? express.router대신 사용 고려하십시오 .

var securedRoutes = require('express').Router()

securedRoutes.use(/* auth-middleware from above */)
securedRoutes.get('path1', /* ... */) 

app.use('/secure', securedRoutes)
app.get('public', /* ... */)

// example.com/public       // no-auth
// example.com/secure/path1 // requires auth

많은 미들웨어가 v4의 Express 코어에서 분리되어 별도의 모듈에 배치되었습니다. 기본 인증 모듈은 여기에 있습니다 : https://github.com/expressjs/basic-auth-connect

귀하의 예제는 다음과 같이 변경해야합니다.

var basicAuth = require('basic-auth-connect');
app.use(basicAuth('username', 'password'));

TL; DR :

express.basicAuth사라짐
basic-auth-connect지원 중단됨
basic-auth로직이 없음
http-auth과잉 임
express-basic-auth원하는 것

더 많은 정보:

Express를 사용하고 있으므로 express-basic-auth미들웨어를 사용할 수 있습니다 .

문서를 참조하십시오.

예:

const app = require('express')();
const basicAuth = require('express-basic-auth');

app.use(basicAuth({
    users: { admin: 'supersecret123' },
    challenge: true // <--- needed to actually show the login dialog!
}));

원본 코드를 사용하여 basicAuth답을 찾았습니다.

app.use(function(req, res, next) {
    var user = auth(req);

    if (user === undefined || user['name'] !== 'username' || user['pass'] !== 'password') {
        res.statusCode = 401;
        res.setHeader('WWW-Authenticate', 'Basic realm="MyRealmName"');
        res.end('Unauthorized');
    } else {
        next();
    }
});

I changed in express 4.0 the basic authentication with http-auth, the code is:

var auth = require('http-auth');

var basic = auth.basic({
        realm: "Web."
    }, function (username, password, callback) { // Custom authentication method.
        callback(username === "userName" && password === "password");
    }
);

app.get('/the_url', auth.connect(basic), routes.theRoute);

There seems to be multiple modules to do that, some are deprecated.

This one looks active:
https://github.com/jshttp/basic-auth

Here's a use example:

// auth.js

var auth = require('basic-auth');

var admins = {
  'art@vandelay-ind.org': { password: 'pa$$w0rd!' },
};


module.exports = function(req, res, next) {

  var user = auth(req);
  if (!user || !admins[user.name] || admins[user.name].password !== user.pass) {
    res.set('WWW-Authenticate', 'Basic realm="example"');
    return res.status(401).send();
  }
  return next();
};




// app.js

var auth = require('./auth');
var express = require('express');

var app = express();

// ... some not authenticated middlewares

app.use(auth);

// ... some authenticated middlewares

Make sure you put the auth middleware in the correct place, any middleware before that will not be authenticated.


We can implement the basic authorization without needing any module

//1.
var http = require('http');

//2.
var credentials = {
    userName: "vikas kohli",
    password: "vikas123"
};
var realm = 'Basic Authentication';

//3.
function authenticationStatus(resp) {
    resp.writeHead(401, { 'WWW-Authenticate': 'Basic realm="' + realm + '"' });
    resp.end('Authorization is needed');

};

//4.
var server = http.createServer(function (request, response) {
    var authentication, loginInfo;

    //5.
    if (!request.headers.authorization) {
        authenticationStatus (response);
        return;
    }

    //6.
    authentication = request.headers.authorization.replace(/^Basic/, '');

    //7.
    authentication = (new Buffer(authentication, 'base64')).toString('utf8');

    //8.
    loginInfo = authentication.split(':');

    //9.
    if (loginInfo[0] === credentials.userName && loginInfo[1] === credentials.password) {
        response.end('Great You are Authenticated...');
         // now you call url by commenting the above line and pass the next() function
    }else{

    authenticationStatus (response);

}

});
 server.listen(5050);

Source:- http://www.dotnetcurry.com/nodejs/1231/basic-authentication-using-nodejs


Express has removed this functionality and now recommends you use the basic-auth library.

Here's an example of how to use:

var http = require('http')
var auth = require('basic-auth')

// Create server
var server = http.createServer(function (req, res) {
  var credentials = auth(req)

  if (!credentials || credentials.name !== 'aladdin' || credentials.pass !== 'opensesame') {
    res.statusCode = 401
    res.setHeader('WWW-Authenticate', 'Basic realm="example"')
    res.end('Access denied')
  } else {
    res.end('Access granted')
  }
})

// Listen
server.listen(3000)

To send a request to this route you need to include an Authorization header formatted for basic auth.

Sending a curl request first you must take the base64 encoding of name:pass or in this case aladdin:opensesame which is equal to YWxhZGRpbjpvcGVuc2VzYW1l

Your curl request will then look like:

 curl -H "Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l" http://localhost:3000/

참고URL : https://stackoverflow.com/questions/23616371/basic-http-authentication-with-node-and-express-4

반응형