我正在开发一个辛纳特拉应用程序,我想使用OmniAuth。到目前为止,我的web应用程序也有类似的地方:
http://codebiff.com/omniauth-with-sinatra
我希望这个web应用程序可以通过Android手机使用,它可以使用API,通过令牌进行身份验证。API的开发似乎在这里得到了很好的介绍:
Sinatra - API - Authentication
不清楚的是,现在我可能会安排登录过程。据推测,这将遵循以下方针:
我不太清楚如何管理第3点--有人有什么建议吗?
发布于 2012-04-11 15:16:57
由于似乎没有人有任何建议,我到目前为止想出的是。不过,我不认为这很好。
我向用户模型添加了一个API密钥,它是在用户首次身份验证时创建的:
class User
include DataMapper::Resource
property :id, Serial, :key => true
property :uid, String
property :name, String
property :nickname, String
property :created_at, DateTime
property :api_key, String, :key => true
end
....
get '/auth/:name/callback' do
auth = request.env["omniauth.auth"]
user = User.first_or_create({ :uid => auth["uid"]},
{ :uid => auth["uid"],
:nickname => auth["info"]["nickname"],
:name => auth["info"]["name"],
:api_key => SecureRandom.hex(20),
:created_at => Time.now })
session[:user_id] = user.id
session[:api_key] = user.api_key
flash[:info] = "Welcome, #{user.name}"
redirect "/success/#{user.id}/#{user.api_key}"
end如果授权有效,那么api_key将提供给安卓应用程序,该应用程序可能会将其存储在设备的某个地方:
get '/success/:id/:api_key', :check => :valid_key? do
user = User.get(params[:id],params[:api_key])
if user.api_key == params[:api_key]
{'api_key' => user.api_key}.to_json
else
error 401
end
end所有API调用都受到保护,如我最初文章中的链接所示:
register do
def check (name)
condition do
error 401 unless send(name) == true
end
end
end
helpers do
def valid_key?
user = User.first(:api_key => params[:api_key])
if !user.nil?
return true
end
return false
end
end对于公共用途,我只允许SSL连接到服务器。任何改进建议都将受到欢迎。
https://stackoverflow.com/questions/10091637
复制相似问题