【问题标题】:http authentication in devise and rails 3设计和rails 3中的http身份验证
【发布时间】:2011-07-23 22:04:13
【问题描述】:

我有一个在 rails 3 上使用 devise 的应用程序。我想启用 http 身份验证,以便我可以从 iPhone 应用程序对我的 Web 应用程序进行身份验证。如何从我的 iPhone 应用进行身份验证以进行设计?

这是安全的还是我应该以不同的方式进行身份验证?

【问题讨论】:

  • HI 你找到解决方法了吗,请分享一下

标签: iphone ruby ruby-on-rails-3 authentication devise


【解决方案1】:

从设计的角度来看,您有 3 个选项:

1) 使用基本的 http 身份验证:您的 iPhone 应用程序有一个密钥 - 它包含在您的 iPhone 应用程序代码中 - 用于对网络应用程序的每个请求进行身份验证。 Google 搜索:“设计基本的 http 身份验证”

2) 您可以通过在您的 iPhone 应用程序中使用公共证书和在您的网络应用程序中使用私有证书来使用 https。正确配置需要做很多工作,它非常安全,因为您的 iPhone 应用程序和 Rails 服务器正在通过加密通道交换消息。由于身份验证是在传输级别完成的,因此安全性对您的 rails 代码也是透明的。

3) iPhone 应用程序使用 https 连接到 web 应用程序,获取一个身份验证令牌,然后它使用该令牌通过常规 http 调用 web 应用程序。比 1 更安全,因为密钥可以过期,实施相当多的工作并且非常可扩展。 (http://matteomelani.wordpress.com/2011/10/17/authentication-for-mobile-devices/)

大多数应用都使用解决方案 1。

希望对您有所帮助。

编辑:实现http身份验证(基本或摘要)我建议你看看:

http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic.htmlhttps://github.com/plataformatec/devise/wiki/How-To:-Use-HTTP-Basic-Authentication

具体步骤取决于您的 Rails 服务器堆栈。

编辑 2:我不认为 Devise 提供了获取 auth_token 的方法。我可以看到你可以尝试几种解决方案:

  • 当用户登录服务器时,会检索 authentication_token 并将其放入 cookie 中。除非您使用共享密钥对其进行加密,否则不是很安全。

  • 您可以提供一个 https Web 服务,您的 iPhone 应用程序使用该服务来获取用户令牌。您的 iPhone 应用会在收到用户登录请求后立即发出请求。

抱歉,我无法提供一些实际代码的更多帮助。

【讨论】:

  • 我计划为 Web 应用程序提供一个 ssl 证书,以便通过 https 交换所有数据。我还计划对 iPhone 应用程序发出的每个请求使用 auth_token。但我感到困惑的是,我如何真正实现 iPhone 部分,以便第一次使用用户名和密码对 Web 应用程序进行实际身份验证,以便我可以获得后续请求的自动令牌。
【解决方案2】:

这在很大程度上取决于您如何在服务器端实现,但我们使用 Matteo 的第 3 个选项实现了这一点。我有一个使用设计的 rails 3.1 实现。登录路径是 /users/login.json 。首先使用如下代码构建用于登录的 JSON 正文:

NSMutableDictionary *loginDictionary = [NSMutableDictionary dictionary];
NSMutableDictionary *usernamePasswordDictionary = [NSMutableDictionary dictionary];
[usernamePasswordDictionary setObject:username forKey:@"email"];
[usernamePasswordDictionary setObject:password forKey:@"password"];
[loginDictionary setObject:usernamePasswordDictionary forKey:@"user"];

NSData *data = [NSJSONSerialization dataWithJSONObject:loginDictionary options:0 error:&error];

产生这个 JSON:

{"user":{"password":"blahblahblah","email":"admin@*****.com"}}

我发送一个 POST url 请求,代码如下:

NSString *postUrlString = [NSString stringWithFormat:@"%@users/login.json", kServerAPIBaseURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:postUrlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kTimeoutInterval];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[request setHTTPBody:data];

我返回的响应包含 JSON。我们配置服务器端返回一个 session_auth_token:

{
    admin = 1;
    "created_at" = "2012-01-25T00:15:58Z";
    "current_sign_in_at" = "2012-04-04T04:29:15Z";
    "current_sign_in_ip" = "75.163.148.101";
    email = "admin@******.com";
    "encrypted_password" = "*****";
    "failed_attempts" = 0;
    id = 1;
    "last_sign_in_at" = "2012-04-03T03:37:18Z";
    "last_sign_in_ip" = "75.163.148.101";
    "locked_at" = "<null>";
    name = "Joe Smith";
    "remember_created_at" = "2012-03-29T20:35:43Z";
    "reset_password_sent_at" = "<null>";
    "reset_password_token" = "<null>";
    "session_auth_token" = "3FRgX6CYlzQJGC8tRWwqEjFaMMFKarQAYKTy3u84M0U=";
    "sign_in_count" = 145;
    status = 1;
    "unlock_token" = "<null>";
    "updated_at" = "2012-04-04T04:29:15Z";
}

我们存储该 session_auth_token,然后将其与标头中的每个请求一起发回,如下所示:

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self postUrlString]]...
[postRequest setHTTPMethod:@"POST"];
[postRequest setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
[postRequest setValue:[self sessionAuth] forHTTPHeaderField:@"X-CSRF-Token"];
[postRequest setHTTPBody:data];

那个参数[self sessionAuth]包含session_auth_token。

如果您需要澄清,请告诉我。

【讨论】:

    猜你喜欢
    • 2011-10-27
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多