IT

모바일 애플리케이션을위한 API 작성-인증 및 권한

lottoking 2020. 5. 16. 10:34
반응형

모바일 애플리케이션을위한 API 작성-인증 및 권한


개요

내 응용 프로그램에 대한 (REST) ​​API를 만들려고합니다. 초기 / 1 차 목적은 모바일 앱 (iPhone, Android, Symbian 등)이 소비하는 것입니다. 웹 기반 API의 인증 및 권한 부여에 대한 다른 메커니즘을 조사했습니다 (다른 구현을 연구함으로써). 나는 기본 개념의 대부분을 머리에 감쌌지만 여전히 몇 가지 영역에서 지침을 찾고 있습니다. 내가하고 싶은 마지막 일은 바퀴를 재창조하는 것이지만 내 기준에 맞는 표준 솔루션을 찾지 못했습니다 (그러나 내 기준이 잘못 인도되었으므로 자유롭게 비판하십시오). 또한 API를 사용하는 모든 플랫폼 / 응용 프로그램에서 API가 동일하기를 원합니다.

oAuth

내가 제안한 첫 번째 해결책이 될 것임을 알기 때문에 계속해서 oAuth에 대한 이의를 제기 할 것입니다. 모바일 응용 프로그램 (또는 웹 이외의 응용 프로그램)의 경우 인증을 위해 응용 프로그램을 웹 브라우저로 두는 것은 잘못된 것 같습니다. 또한 브라우저가 콜백을 응용 프로그램 (특히 크로스 플랫폼)으로 반환 할 수있는 방법이 없습니다. 나는 그것을하는 몇 가지 응용 프로그램을 알고 있지만 잘못 느꼈고 응용 프로그램 UX에서 휴식을줍니다.

요구 사항

  1. 사용자는 응용 프로그램에 사용자 이름 / 암호를 입력합니다.
  2. 모든 API 호출은 호출 애플리케이션으로 식별됩니다.
  3. 오버 헤드는 최소한으로 유지되며 인증 측면은 개발자에게 직관적입니다.
  4. 이 메커니즘은 최종 사용자 (로그인 자격 증명이 노출되지 않음)와 개발자 (응용 프로그램 자격 증명이 노출되지 않음) 모두에 대해 안전합니다.
  5. 가능하면 https를 요구하지 마십시오 (어려운 요구 사항은 아님).

구현에 대한 나의 현재 생각

외부 개발자는 API 계정을 요청합니다. 그들은 apikey와 apisecret을 받게됩니다. 모든 요청에는 최소한 세 개의 매개 변수가 필요합니다.

  • apikey-등록시 개발자에게 제공
  • 타임 스탬프-지정된 apikey에 대한 각 메시지의 고유 식별자로 두 배로 증가
  • 해시-타임 스탬프의 해시 + apisecret

요청을 발행하는 응용 프로그램을 식별하려면 apikey가 필요합니다. 타임 스탬프는 oauth_nonce와 유사하게 작동하며 재생 공격을 방지 / 완화합니다. 해시는 지정된 apikey의 소유자로부터 요청이 실제로 발행되도록합니다.

인증 된 요청 (사용자를 대신하여 수행 된 요청)의 경우 여전히 access_token 경로 또는 사용자 이름과 암호 해시 콤보를 사용하는 것으로 결정되지 않았습니다. 어느 쪽이든 어느 시점에서 사용자 이름 / 암호 콤보가 필요합니다. 따라서 그렇게 할 때 여러 정보 (apikey, apisecret, timestamp) + 암호의 해시가 사용됩니다. 이 부분에 대한 의견을 듣고 싶습니다. 참고로, 해싱없이 암호를 시스템에 저장하지 않기 때문에 암호를 먼저 해시해야합니다.

결론

참고로, 이는 일반적으로 응용 프로그램 내에서만 인증 및 권한 부여를 처리하는 방법만으로 API를 빌드 / 구조화하는 방법에 대한 요청이 아닙니다.

임의의 생각 / 보너스 질문

요청의 일부로 apikey 만 필요한 API의 경우, apikey 소유자 이외의 다른 사람이 apikey를 보지 못하도록하고 (정확하게 보내진 이후) 과도한 요청을하여 사용 제한을 넘지 않도록하려면 어떻게해야합니까? 어쩌면 나는 이것을 생각하고 있지만 apikey 소유자에게 요청이 확인되었다는 것을 증명해야 할 것이 없어야합니까? 필자의 경우, 그것은 apisecret의 목적이었습니다. 해시되지 않고 표시 / 전송되지 않습니다.

해시라고하면 MD5 대 hmac-sha1은 어떻습니까? 충분히 긴 데이터 (예 : apisecret)로 모든 값을 해시하는 것이 실제로 중요합니까?

이전에는 사용자 / 해당 소금을 사용자 암호 해시에 추가하는 것을 고려하고있었습니다. 그렇게하려면 응용 프로그램에서 사용되는 소금을 모르고 어떻게 일치하는 해시를 만들 수 있습니까?


내 프로젝트에서 로그인의 일부를 수행하는 방법은 다음과 같습니다.

  1. 로그인하기 전에 사용자 login_token는 서버에 요청 합니다. 요청시 서버에서 생성 및 저장되며 수명이 제한 될 수 있습니다.

  2. 응용 프로그램에 로그인하면 사용자 비밀번호의 해시를 계산 한 다음 비밀번호를로 해시하여 login_token값을 얻은 다음 login_token및 해시를 모두 반환합니다 .

  3. 서버 login_token는 생성 한 서버를 확인하여 유효한 목록에서 제거합니다 login_token. 그런 다음 서버는 저장된 사용자 비밀번호 해시 login_token를와 결합하여 제출 된 결합 토큰과 일치하는지 확인합니다. 일치하면 사용자를 인증 한 것입니다.

이것의 장점은 서버에 사용자의 비밀번호를 저장하지 않으며, 비밀번호는 일반 비밀번호로 전달되지 않으며, 비밀번호 해시는 계정 작성시 일반 비밀번호로만 전달된다는 것입니다 (이와 관련하여 방법이있을 수 있음). login_token사용 중 DB에서 제거 되므로 재생 공격으로부터 안전 합니다.


그것은 하나의 질문에 대한 많은 질문입니다. 많은 사람들이 끝까지 모든 것을 읽을 수 없었습니다. :)

웹 서비스 인증에 대한 나의 경험은 사람들이 일반적으로 그것을 과도하게 엔지니어링한다는 것입니다. 문제는 웹 페이지에서 겪는 것과 동일합니다. 가능한 매우 간단한 옵션에는 로그인 단계에 대한 https, 토큰 반환, 향후 요청에 포함되어야 함이 포함됩니다. http 기본 인증을 사용하고 헤더에 내용을 전달할 수도 있습니다. 보안을 강화하려면 토큰을 자주 회전 / 만료하고 동일한 IP 블록에서 요청이 들어오는 지 확인하십시오 (모바일 사용자가 셀간에 이동하더라도 혼란 스러울 수 있음), API 키 또는 이와 유사한 기능을 결합하십시오. 또는 사용자를 인증하기 전에 oauth의 "요청 키"단계를 수행하고 (누군가 이미 이전 답변에서이를 제안했으며 좋은 아이디어입니다) 액세스 토큰을 생성하는 데 필요한 키로 사용하십시오.

아직 사용하지 않은 대안이지만 oAuth에 대한 장치 친화적 인 대안으로 많이 들었습니다 . xAuth 입니다. 그것을보고 당신이 그것을 사용한다면 나는 당신의 인상이 무엇인지 듣고 싶습니다.

해싱의 경우 sha1이 약간 나아지지만 그것에 매달리지 마십시오. 장치가 쉽게 (그리고 성능 적으로 빨리) 구현할 수있는 것은 무엇이든 괜찮을 것입니다.

행운을 빕니다 :)


트위터는 xAuth 라고하는 변형을 지원함으로써 oAuth의 외부 애플리케이션 문제를 해결했습니다 . 불행히도이 이름을 가진 수많은 다른 체계가 이미 있으므로 분류하기가 혼란 스러울 수 있습니다.

The protocol is oAuth, except it skips the request token phase and simply immediately issues an access token pair upon receipt of a username and password. (Starting at step E here.) This initial request and response must be secured - it's sending the username and password in plaintext and receiving back the access token and secret token. Once the access token pair has been configured, whether the initial token exchange was via the oAuth model or the xAuth model is irrelevant to both the client and server for the rest of the session. This has the advantage that you can leverage existing oAuth infrastructure and have very nearly the same implementation for mobile/web/desktop applications. The main disadvantage is that the application is granted access to the client's user name and password, but it appears like your requirements mandate this approach.

In any case, I'd like to agree with your intuition and that of several other answerers here: don't try to build something new from scratch. Security protocols can be easy to start but are always hard to do well, and the more convoluted they become the less likely your third-party developers are to be able to implement against them. Your hypothetical protocol is very similar to o(x)Auth - api_key/api_secret, nonce, sha1 hashing - but instead of being able to use one of the many existing libraries your developers are going to need to roll their own.


So what you're after is some kind of server side authentication mechanism that will handle the authentication and authorisation aspects of a mobile application?

Assuming this is the case, then I would approach it as follows (but only 'cos I'm a Java developer so a C# guy would do it differently):

The RESTful authentication and authorisation service

  1. This will work only over HTTPS to prevent eavesdropping.
  2. It will be based on a combination of RESTEasy, Spring Security and CAS (for single sign on across multiple applications).
  3. It will work with both browsers and web-enabled client applications
  4. There will be a web-based account management interface to allow users to edit their details, and admins (for particular applications) to change authorisation levels

The client side security library/application

  1. For each supported platform (e.g. Symbian, Android, iOS etc) create a suitable implementation of the security library in the native language of the platform (e.g. Java, ObjectiveC, C etc)
  2. The library should manage the HTTPS request formation using the available APIs for the given platform (e.g. Java uses URLConnection etc)
  3. Consumers of the general authentication and authorisation library ('cos that's all it is) will code to a specific interface and won't be happy if it ever changes so make sure it's very flexible. Follow existing design choices such as Spring Security.

So now that the view from 30,000ft is complete how do you go about doing it? Well, it's not that hard to create an authentication and authorisation system based on the listed technologies on the server side with a browser client. In combination with HTTPS, the frameworks will provide a secure process based on a shared token (usually presented as a cookie) generated by the authentication process and used whenever the user wishes to do something. This token is presented by the client to the server whenever any request takes place.

In the case of the local mobile application, it seems that you're after a solution that does the following:

  1. Client application has a defined Access Control List (ACL) controlling runtime access to method calls. For example, a given user can read a collection from a method, but their ACL only permits access to objects that have a Q in their name so some data in the collection is quiety pulled by the security interceptor. In Java this is straightforward, you just use the Spring Security annotations on the calling code and implement a suitable ACL response process. In other languages, you're on your own and will probably need to provide boilerplate security code that calls into your security library. If the language supports AOP (Aspect Oriented Programming) then use it to the fullest for this situation.
  2. The security library caches the complete list of authorisations into it's private memory for the current application so that it doesn't have to remain connected. Depending on the length of the login session, this could be a one-off operation that never gets repeated.

Whatever you do, don't try to invent your own security protocol, or use security by obscurity. You'll never be able to write a better algorithm for this than those that are currently available and free. Also, people trust well known algorithms. So if you say that your security library provides authorisation and authentication for local mobile applications using a combination of SSL, HTTPS, SpringSecurity and AES encrypted tokens then you'll immediately have creditibility in the marketplace.

Hope this helps, and good luck with your venture. If you would like more info, let me know - I've written quite a few web applications based on Spring Security, ACLs and the like.


Super late to the party but I wanted to throw in some additional points to consider for anyone interested in this issue. I work for a company doing mobile API security solutions (approov) so this whole area is definitely relevant to my interests.

To start with, the most important thing to consider when trying to secure a mobile API is how much it is worth to you. The right solution for a bank is different to the right solution for someone just doing things for fun.

In the proposed solution you mention that a minimum of three parameters will be required:

  • apikey - given to developer at registration
  • timestamp - doubles as a unique identifier for each message for a given apikey
  • hash - a hash of the timestamp + the apisecret

The implication of this is that for some API calls no username/password is required. This can be useful for applications where you don't want to force a login (browsing in online shops for example).

This is a slightly different problem to the one of user authentication and is more like authentication or attestation of the software. There is no user, but you still want to ensure that there is no malicious access to your API. So you use your API secret to sign the traffic and identify the code accessing the API as genuine. The potential problem with this solution is that you then have to give away the secret inside every version of the app. If someone can extract the secret they can use your API, impersonating your software but doing whatever they like.

To counter that threat there are a bunch of things you can do depending on how valuable the data is. Obfuscation is a simple way to make it harder to extract the secret. There are tools that will do that for you, more so for Android, but you still have to have code that generates your hash and a sufficiently skilled individual can always just call the function that does the hashing directly.

Another way to mitigate against excessive use of an API that doesn't require a login is to throttle the traffic and potentially identify and block suspect IP addresses. The amount of effort you want to go to will largely depend upon how valuble your data is.

그 외에도 당신은 쉽게 내 일의 영역으로 들어갈 수 있습니다. 어쨌든 중요하다고 생각하고 플래그를 지정하려는 API 보안의 또 다른 측면입니다.

참고 URL : https://stackoverflow.com/questions/3963877/creating-an-api-for-mobile-applications-authentication-and-authorization

반응형