简体   繁体   English

iOS验证到Web服务器

[英]iOS authentication to web server

OK. 好。 So i need some guidance as I am a total iOS authentication noob. 所以我需要一些指导,因为我是一个完全iOS身份验证的菜鸟。

I have a simple app. 我有一个简单的应用程序。 Users can login to the app, and send messages to friends. 用户可以登录该应用程序,并将消息发送给朋友。 There is a web server and a MySql Database that holds the users and login information. 有一个Web服务器和一个MySql数据库,其中包含用户和登录信息。

Question: How do I authenticate a user when he logs in safely and securely ? 问题:当用户safely securely登录时,如何验证用户身份?

I have spent the last several hours hurting my brain on the following authentication stuff i found from google: 在过去的几个小时中,我花了很多时间在Google上发现的以下身份验证内容上伤脑筋:

  1. OAuth 1.0 - is said to be good. OAuth 1.0-据说不错。 But it is a protocol and not a library. 但这是一个协议,而不是一个库。 Do i have to implement this from scratch? 我是否必须从头开始实施? Is this even needed in my case for authentication? 就我而言,这是否甚至需要身份验证?
  2. OAuth 2.0 - it seems that some sites are using this. OAuth 2.0-似乎某些网站正在使用此功能。 I have the same questions for this as version 1.0. 我对此有与1.0版相同的问题。 I also saw this this message from the library's lead creator literally saying f*** version 2.0 because it was bad for security. 我还从库的主要创建者那里看到了此消息字面意思是f *** 2.0版,因为它不利于安全。 But yet so many still use it. 但是仍然有很多人在使用它。 Is it dangerous? 危险吗?
  3. The creator of 2.0 has now gone on to make a completely other library because of how bad 2.0 was and because of how unscalable 1.0 was. 由于2.0的糟糕程度和1.0的不可扩展性,2.0的创建者现在继续制作其他库 His library is called OZ. 他的图书馆叫OZ。 Should I be using this for my server? 我应该在服务器上使用它吗?
  4. I see AlamoFire/ AFNetworking have basic authentication shown in their documentation. 我看到AlamoFire / AFNetworking在其文档中显示了基本身份验证。 Should i just screw the oAuth stuff and just use theirs? 我应该只是拧紧oAuth的东西并使用它们吗?

Being new to the authentication thing, all this is very confusing to me. 对于身份验证而言,这是新手,这一切令我感到困惑。 Can anyone knowledgeable in this provide some guidance? 对此有知识的人可以提供一些指导吗?

I am currently in the process of creating a cross-platform application and have spent quite some time researching this! 我目前正在创建一个跨平台应用程序,并花了很多时间对此进行研究!

My approach to the project is using a ASP.NET Web API using OWIN middleware. 我对该项目的方法是使用使用OWIN中间件的ASP.NET Web API。

This uses bearer tokens to authenticate the user. 这使用承载令牌来认证用户。 Using Microsoft.Identity you can limit endpoints down to roles or even individual users (Autherization) 使用Microsoft.Identity您可以将端点限制为角色甚至单个用户(授权)

Currently I create a user on the REST API, They log-in at the /token endpoint and then receive a token. 当前,我在REST API上创建一个用户,他们在/token端点登录,然后接收令牌。 This token is then saved to the Apple key chain and can be used to authenticate the user for further requests to the API. 然后,此令牌将保存到Apple钥匙链,并可用于验证用户的身份,以进一步请求对该API进行请求。

As long as you use SSL this is a secure method and is used widely in many applications. 只要您使用SSL,这就是一种安全的方法,并且已在许多应用程序中广泛使用。

This approach uses OAuth2 also, so you'll be albe to easily integrate Facebook/Google/etc integration. 这种方法也使用OAuth2,因此您可以轻松集成Facebook / Google / etc集成。

Here is a link to the Microsoft Documentation for some further reading on how I did it: http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api 这是指向Microsoft文档的链接,以进一步了解我的操作方式: http : //www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api

Currently this is working perfectly for me for an angular front-end but would work exactly the same in iOS except you may want to save the token to the KeyChain Storage. 目前,这对我来说对于有角度的前端来说是完美的,但是在iOS中可以完全一样,除了您可能希望将令牌保存到KeyChain存储中。

We mostly use OAuth 2 creating custom system on iOS to handle the authentication. 我们主要使用OAuth 2在iOS上创建自定义系统来处理身份验证。

Nothing is ever bullet-proof but the 2 token system decreases the chance for stealing credential quite nicely. 没有什么是防弹的,但是2令牌系统相当不错地减少了窃取证书的机会。

The AlamoFire, AFNetworking or any other libraries you amy find have nothing to do with this though. 不过,您发现的AlamoFire,AFNetworking或任何其他库与此无关。 What type of credentials you use depends on your choice or rather the choice of the API. 您使用哪种类型的凭据取决于您的选择,或者取决于API的选择。 You may use these tools to ease your communication with the API though. 不过,您可以使用这些工具来简化与API的通信。

So what the idea behind this is you will try to send your user name and password only once when logging in and then you will receive the two tokens which are further used to communicate. 因此,这背后的想法是,您将尝试在登录时仅发送一次用户名和密码,然后您将收到两个令牌,这些令牌将进一步用于通信。 This will decrease a chance for someone to intercept the request with the user name and password which are the ultimate key to get the access to the user data. 这将减少某人用用户名和密码来拦截请求的机会,用户名和密码是获得用户数据访问权的最终关键。

Next is "refresh token" which is used to receive a new "access token". 接下来是“刷新令牌”,用于接收新的“访问令牌”。 This call should be made every few hours or so (controlled by the API). 该调用应每隔几个小时左右进行一次(由API控制)。 If someone was to steal this token he would be able to use it to get further access for an infinite duration or until the owner chooses to invalidate the refresh tokens (this is what happens when you click "log out from all devices"). 如果有人要窃取此令牌,他将可以使用它无限期地进行进一步访问,或者直到所有者选择使刷新令牌无效(这是单击“从所有设备注销”时发生的情况)。 So this is still quite bad if someone gets it. 因此,如果有人得到了,这仍然很糟糕。

Then there is the "access token" which is used for each and every further request to the server. 然后是“访问令牌”,用于对服务器的每个其他请求。 These tokens have a limited time till they are invalidated so if someone was to intercept it somehow he would have the access to the data for the duration of the token. 这些令牌在失效之前有一段有限的时间,因此,如果有人以某种方式对其进行拦截,他将可以在令牌持续时间内访问数据。

So assuming this is the procedure that is done on the backend this is what you need to do: 因此,假设这是在后端完成的过程,这就是您需要做的:

  • If you have the access token and is valid simply use the service 如果您具有访问令牌并且有效,则只需使用该服务
  • If you receive the error that the access token is invalid you need to refresh the access token using your refresh token 如果您收到访问令牌无效的错误,则需要使用刷新令牌刷新访问令牌
  • If refresh token reports an error you need to navigate back to the login screen 如果刷新令牌报告错误,则需要导航回登录屏幕
  • If the app has no refresh token then simply go to the login screen 如果应用程序没有刷新令牌,则只需转到登录屏幕

There are some other things that are nice to cover such as if the request reports an invalid token you should pend the request, refresh the token and then repeat the call to the pending request. 还有其他一些很不错的事情,例如,如果请求报告了无效的令牌,则您应该将请求挂起,刷新令牌,然后重复调用待处理的请求。 A system around this may be quite large. 围绕这个系统可能很大。

This is pretty much it about the tokens and authentication but there are other parts of the communication which increase the security such as using a https secure connection. 这几乎与令牌和身份验证有关,但是通信的其他部分也增加了安全性,例如使用https安全连接。 When talking about security you must take a look into every part of the communication. 在谈论安全性时,您必须仔细研究通信的每个部分。

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

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