我想知道在移动应用程序上存储密钥以进行会话身份验证的机制。我有一个Tornado网络服务器,它将使用第三方外部服务来验证用户身份,例如Facebook或Google。我熟悉使用浏览器时使用set_secure_cookie
存储cookie的知识。但是,如果移动应用程序现在正在连接并进行身份验证,该怎么办。我将使用什么机制来存储像安全cookie这样的秘密以便将来进行会话身份验证?下面显示了验证用户的代码:
class GoogleOAuth2LoginHandler(tornado.web.RequestHandler,
tornado.auth.GoogleOAuth2Mixin):
async def get(self):
if self.get_argument('code', False):
user = await self.get_authenticated_user(
redirect_uri='http://your.site.com/auth/google',
code=self.get_argument('code'))
# Save the user with e.g. set_secure_cookie
else:
await self.authorize_redirect(
redirect_uri='http://your.site.com/auth/google',
client_id=self.settings['google_oauth']['key'],
scope=['profile', 'email'],
response_type='code',
extra_params={'approval_prompt': 'auto'})
对于不依赖浏览器和cookie支持的移动应用程序,如何修改?
答案 0 :(得分:3)
在iOS中,NSHTTPCookie类中有一个API,您可以在其中保存整个http响应字符串。 该代码将类似于下面的内容以创建cookie。
if let requestUrl = url {
let httpCookies = HTTPCookie.cookies(withResponseHeaderFields: response.allHeaderFields as! [String : String], for: requestUrl)
}
然后您可以保存cookie,
HTTPCookieStorage.shared.setCookies(httpCookie, for: url, mainDocumentURL: url)
如果需要,您还可以访问此cookie并将其设置为WebView。
答案 1 :(得分:0)
我不知道这是否是您要寻找的东西,但是您可以使用SharedPreferences系统,它非常简单,并且可以保存本地密钥对值以存储任何有用的信息。
在Android上,您可以使用SharedPreferences或EncryptedSharedPreferences添加额外的安全层…
在iOS上,您有一种大致相同的机制,称为NSUserDefaults,该机制还可以存储密钥对值,并允许额外的安全层。