设备和 iPhone 应用程序之间的 HTTP 身份验证

发布于 2024-11-17 05:50:44 字数 620 浏览 2 评论 0原文

我是 ruby​​ on Rails 的新手,但我想将 SQlite 数据库中的数据从我的 iPhone 应用程序发送到 Rails Web 应用程序。就像“同步”服务一样。

我正在使用网络应用程序进行身份验证。我启用了基本的 HTTP 身份验证,并且可以进入网站获取 xml 或 json 数据。当我将帖子标题设置为 JSON 并使用用户名和密码时,我还可以将数据上传到网站。

这就是我被困住的地方。

1) 如何让用户在第一次登录后保持登录状态?每次向网站发送数据时是否都使用 http 身份验证?我已经阅读过有关令牌身份验证的内容,但我不确定如何使用它。

2)我可以使用正确的用户名和密码将 JSON 数据发布到 http://localhost:3000/example 之类的内容。但是,如果用户名和密码不正确,它会返回 HTML 内容。我是否必须编写一些返回有关登录成功/失败的 json 数据的内容?

3) 在我的 iPhone 应用程序和我的网络应用程序之间进行通信。我在网络应用程序端编写 RESTful API 是否正确?我需要使用活跃资源吗?

我真的很困惑这一切是如何运作的。谢谢!

I'm new to ruby on rails but I want to send the data from my SQlite database from my iphone app to the rails web app. Like a "sync" service.

I'm using devise for authentication for the web app. I enabled basic HTTP authentication and I can curl into the website for xml or json data. I can also upload data to the website when I set the post headers to JSON and with username and password.

Here's where I'm stuck.

1) How do I keep the user signed in after the 1st login? Do I use http authentication every time I send data to the website? I've read about token authentication but I'm not sure how to use it.

2) I can post JSON data to something like http://localhost:3000/example with the correct username and password. However, it returns the HTML content if the username and passowrd is incorrect. Do I have to write something that returns json data about login success/fialure?

3) To communicate between my iphone app and my web app. Am I correct in writing a RESTful API on the web app side? Do I need to use active resources?

I'm really stuck on the overall big picture of how all this works. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷心人i 2024-11-24 05:50:44

有很多方法可以做到这一点。为了让 Devise 将错误返回到我可以解释的 iPhone 应用程序(例如 401),我所做的就是创建一个自定义失败应用程序:

# config/initializers/devise.rb
config.warden do |manager|
  manager.failure_app   = CustomFailure
end

# config/initializers/custom_failure.rb
class CustomFailure < Devise::FailureApp     
  def respond
    unless request.format.to_sym == :html
  http_auth
    else
  super
end
  end
end

否则,无论登录信息正确还是不正确,Devise 都会返回带有重定向响应代码的 HTML。

由于我的应用程序要求用户针对我的 Rails 后端进行身份验证,因此我实现了一个简单的登录系统,如下所示:

#app/controllers/pages_controller.rb
before_filter :authenticate_user!, :only => [:login]
ssl_required :login # you have to set up ssl and ssl_requirement


def login
  @user = current_user
  respond_to do |format|
    format.html {render :text => "#{@user.id}"} 
    format.xml {render :text => "#{@user.id}" }   
  end
end

#config/routes.rb
match '/login',       :to => 'pages#login'

然后在 iphone 应用程序中,您可以通过向 /login 发送 GET 请求来进行验证(我使用 ASIHTTPRequest 因为它太棒了):

- (void) validate_login:(NSString*)name :(NSString*)pwd
{   
    NSURL *login_url = [NSURL URLWithString:@"https://mysite.com/login"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:login_url];
    [request setDelegate:self];
    [request setUsername:name];
    [request setPassword:pwd];  
    [request addRequestHeader:@"Accept" value:@"application/xml"]; 
    [request startAsynchronous];    
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
if ([request responseStatusCode] != 200) {
  [self requestFailed:request];
}
    else {
  // authentication successful, store credentials
        NSUSerDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:[request username] forKey:@"username"];
        [defaults setValue:[request password] forKey:@"password"];
    }
}


- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"failed with error: %d %@", [request responseStatusCode], [error localizedDescription]);
    // tell user incorrect username/password    
}

然后每当您需要将数据发布到应用程序时,您可以从用户默认值中检索用户名和密码并将其附加到的请求。如果您需要更安全,可以将它们存储在钥匙串中。

我确信有更好的方法可以做到这一点,但希望这可以让您思考 API 身份验证策略。

There's many ways you can do this. What I did to get Devise to return errors to my iphone app that I could interpret (e.g. 401) was create a custom failure app:

# config/initializers/devise.rb
config.warden do |manager|
  manager.failure_app   = CustomFailure
end

# config/initializers/custom_failure.rb
class CustomFailure < Devise::FailureApp     
  def respond
    unless request.format.to_sym == :html
  http_auth
    else
  super
end
  end
end

Otherwise Devise just returns HTML with a redirect response code regardless of whether the login information was correct or incorrect.

Since my app required users to authenticate against my rails backend, I implemented a simple login system like this:

#app/controllers/pages_controller.rb
before_filter :authenticate_user!, :only => [:login]
ssl_required :login # you have to set up ssl and ssl_requirement


def login
  @user = current_user
  respond_to do |format|
    format.html {render :text => "#{@user.id}"} 
    format.xml {render :text => "#{@user.id}" }   
  end
end

#config/routes.rb
match '/login',       :to => 'pages#login'

Then in the iphone app you can validate by sending a GET request to /login like this (I use ASIHTTPRequest because it's awesome):

- (void) validate_login:(NSString*)name :(NSString*)pwd
{   
    NSURL *login_url = [NSURL URLWithString:@"https://mysite.com/login"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:login_url];
    [request setDelegate:self];
    [request setUsername:name];
    [request setPassword:pwd];  
    [request addRequestHeader:@"Accept" value:@"application/xml"]; 
    [request startAsynchronous];    
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
if ([request responseStatusCode] != 200) {
  [self requestFailed:request];
}
    else {
  // authentication successful, store credentials
        NSUSerDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:[request username] forKey:@"username"];
        [defaults setValue:[request password] forKey:@"password"];
    }
}


- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"failed with error: %d %@", [request responseStatusCode], [error localizedDescription]);
    // tell user incorrect username/password    
}

Then whenever you need to post data to the app you can retrieve the username and password from user defaults and attach them to the request. If you need to be more secure, you can store them in the Keychain.

I'm sure there are better ways to do this, but hopefully this can get you thinking about API authentication strategies.

稚然 2024-11-24 05:50:44

我建议您查看此处提供的文档

https://github。 com/plataformatec/devise/blob/v1.1.6/README.rdoc

您可能还需要观看设备上的截屏视频。

由于有许多不同的方法来处理身份验证,因此您应该更好地了解可用的方法,因为 devise 支持 basicAuth 和基于令牌的身份验证

I would recommend taking a look at the documentation provided here

https://github.com/plataformatec/devise/blob/v1.1.6/README.rdoc

You might also want two watch the screencasts on the devise.

Since there are many different ways to handle auth, you should get a better understanding of what is available since devise supports basicAuth and token based auth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文