내 Node.js 앱의 REST API를 보호합니까?
REST API에 대한 도움을받을 수 있습니다. Express, MongoDB를 사용하고 클라이언트 측에 Backbone.js가있는 Node.js 앱을 작성 중입니다. 나는 지난 이틀 동안이 모든 것을 해결하려고 노력했지만 많은 운이 없었습니다. 이미 체크 아웃했습니다.
- REST API 보안
- 타사 OAuth 공급자를 통한 인증을 허용하면서 OAuth로 내 REST API 보안 (DotNetOpenAuth 사용)
- http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/
- http://tesoriere.com/2011/10/10/node.js-getting-oauth-up-and-working-using-express.js-and-railway.js/
백엔드와 프런트 엔드를 가능한 한 별도로 유지하고 싶으므로 신중하게 설계된 REST API를 사용하는 것이 좋을 것이라고 생각했습니다. 내 생각은 내가 아이폰 앱 (또는 그와 비슷한 것)을 개발할 때 API를 사용하여 데이터에 액세스 할 수 있다는 것입니다.
그러나 나는 이것이 안전하기를 원합니다. 사용자가 내 웹 앱에 로그인했으며 내 API가 안전한지 확인하고 싶습니다. OAuth, OAuth 2.0, OpenID, Hmac, 해시 등에 대해 읽었습니다. 외부 로그인 (Facebook / Twitter 등)을 사용하지 않으려 고합니다. 등록 및 로그인이 내 앱 / 서버에 있어야합니다.
...하지만 여기서는 여전히 혼란 스럽습니다. 밤 늦었거나 뇌가 튀었을 수도 있지만 여기서해야 할 일에 대한 몇 가지 단계를 실제로 수행 할 수 있습니다. 보안 API를 만드는 단계는 무엇입니까?
모든 도움, 정보, 예, 단계 또는 무엇이든 좋을 것입니다. 도와주세요!
여기에 대해 다른 생각 방법이 있습니다.
API를 사용하지 않는다고 가정 해 보겠습니다. 사용자는 앱에 로그인하여 몇 가지 자격 증명을 제공하고 사용자에게 쿠키 또는 유사한 토큰을 제공하여 사용자가 로그인했는지 식별하는 데 사용합니다. 그런 다음 사용자는 제한된 정보가 포함 된 페이지를 요청합니다 (또는 생성 / 수정 / 삭제) 사용자가 해당 정보를 볼 수 있도록이 토큰을 확인합니다.
이제 여기서 변경하는 유일한 것은 정보가 전달되는 방식입니다. 정보를 렌더링 된 HTML로 전달하는 대신 정보를 JSON으로 반환하고 클라이언트 측에서 렌더링합니다. 서버에 대한 AJAX 요청은 이전과 동일한 로그인 토큰을 전달하므로 해당 토큰을 확인하고 동일한 방식으로 정보를 '사용자가 알 수있는 것만'으로 제한하는 것이 좋습니다.
이제 귀하의 API는 귀하의 로그인만큼 안전합니다. 누군가가 api에 액세스하는 데 필요한 토큰을 알고 있다면 사이트에 로그인하여 어쨌든 모든 정보에 액세스 할 수 있습니다. 가장 좋은 점은 이미 로그인을 구현 한 경우 실제로 더 이상 작업을 수행 할 필요가 없다는 것입니다.
OAuth와 같은 시스템의 요점은 일반적으로 타사 응용 프로그램 및 개발자에서이 '로그인'방법을 제공하는 것입니다. 이것은 잠재적으로 iPhone 앱 또는 이와 유사한 앱에 대한 좋은 솔루션이 될 수 있지만 미래에있을 것입니다. 둘 이상의 인증 방법을 허용하는 API에 문제가 없습니다!
보안 / 복잡성을 높이기 위해 :
기본 HTTP 인증
많은 API 라이브러리를 사용하여 (예를 들어 Django의 Piston) 빌드하거나 웹 서버에서 처리하도록 할 수 있습니다. Nginx와 Apache 모두 서버 지시문을 사용하여 간단한 b64로 인코딩 된 비밀번호로 사이트를 보호 할 수 있습니다. 세상에서 가장 안전한 것은 아니지만 최소한 사용자 이름과 암호입니다!
Nginx를 사용하는 경우 다음과 같이 호스트 구성에 섹션을 추가 할 수 있습니다.
auth_basic "Restricted";
auth_basic_user_file /path/to/htpasswd;
(당신의 location /
블록에 넣어 )
문서 : http://wiki.nginx.org/HttpAuthBasicModule
비밀번호를 생성하고 출력을 파일에 넣으려면 Python 스크립트를 가져와야 합니다. http://trac.edgewall.org/browser/trunk/contrib/htpasswd.py?format=txt
Nginx가 파일에 액세스 할 수있는 한 파일의 위치는 그다지 중요하지 않습니다.
HTTPS
서버에서 앱으로의 연결을 보호하십시오. 이것이 가장 기본적인 것이며 중간자 공격을 방지합니다.
매우 포괄적 인 문서 인 Nginx를 사용하여이 작업을 수행 할 수 있습니다. http://wiki.nginx.org/HttpSslModule
이에 대한 자체 서명 인증서는 괜찮습니다 (그리고 무료입니다!).
API 키
원하는 모든 형식이 될 수 있지만 필요한 경우 액세스 권한을 취소 할 수있는 이점이 있습니다. 연결의 양쪽 끝을 개발하는 경우 완벽한 솔루션이 아닐 수 있습니다. API를 사용하는 타사 (예 : Github)가있을 때 사용되는 경향이 있습니다.
OAuth
OAuth 2.0이 여기에 적합합니다. 사양의 기본 작동 방식을 모르지만 현재 대부분의 인증 (Twitter, Facebook, Google 등)에 대한 사실상의 표준이며이를 구현하는 데 도움이되는 수많은 라이브러리와 문서가 있습니다. 즉, 일반적으로 타사 서비스에 인증을 요청하여 사용자를 인증하는 데 사용됩니다.
Given that you doing the development both ends it would probably be enough to put your API behind Basic HTTP Auth and serve it over HTTPS, especially if you don't want to waste time messing around with OAuth.
The answers so far do a great job of explaining, but don't give any actual steps. I came across this blog post that goes into great detail about how to create and manage tokens securely with Node + Passport.
http://aleksandrov.ws/2013/09/12/restful-api-with-nodejs-plus-mongodb/
Tips valid for securing any web application
If you want to secure your application, then you should definitely start by using HTTPS instead of HTTP, this ensures a creating secure channel between you & the users that will prevent sniffing the data sent back & forth to the users & will help keep the data exchanged confidential.
You can use JWTs (JSON Web Tokens) to secure RESTful APIs, this has many benefits when compared to the server-side sessions, the benefits are mainly:
1- More scalable, as your API servers will not have to maintain sessions for each user (which can be a big burden when you have many sessions)
2- JWTs are self contained & have the claims which define the user role for example & what he can access & issued at date & expiry date (after which JWT won't be valid)
3- Easier to handle across load-balancers & if you have multiple API servers as you won't have to share session data nor configure server to route the session to same server, whenever a request with a JWT hit any server it can be authenticated & authorized
4- Less pressure on your DB as well as you won't have to constantly store & retrieve session id & data for each request
5- The JWTs can't be tampered with if you use a strong key to sign the JWT, so you can trust the claims in the JWT that is sent with the request without having to check the user session & whether he is authorized or not, you can just check the JWT & then you are all set to know who & what this user can do.
Node.js specific libraries to implement JWTs:
Many libraries provide easy ways to create & validate JWTs, for example: in node.js one of the most popular is jsonwebtoken, also for validating the JWTs you can use the same library or use express-jwt or koa-jwt (if you are using express/koa)
Since REST APIs generally aims to keep the server stateless, so JWTs are more compatible with that concept as each request is sent with Authorization token that is self contained (JWT) without the server having to keep track of user session compared to sessions which make the server stateful so that it remembers the user & his role, however, sessions are also widely used & have their pros, which you can search for if you want.
One important thing to note is that you have to securely deliver the JWT to the client using HTTPS & save it in a secure place (for example in local storage).
You can learn more about JWTs from this link
참고URL : https://stackoverflow.com/questions/9119648/securing-my-node-js-apps-rest-api
'developer tip' 카테고리의 다른 글
Java 애플리케이션에 파일을 어떻게로드해야합니까? (0) | 2020.11.12 |
---|---|
iOS 앱에서 iMessage 스타일 후진 키보드 (0) | 2020.11.12 |
'nice'에 해당하는 Windows (0) | 2020.11.12 |
CMSPermGenSweepingEnabled 대 CMSClassUnloadingEnabled (0) | 2020.11.12 |
화면이 꺼져있을 때 Android 가속도계가 작동하지 않음 (0) | 2020.11.12 |