Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

HTTP Token #25

Closed
mdfdroid opened this issue Nov 1, 2012 · 13 comments
Closed

HTTP Token #25

mdfdroid opened this issue Nov 1, 2012 · 13 comments

Comments

@mdfdroid
Copy link

mdfdroid commented Nov 1, 2012

Hey guys, amazing work on this project. Makes it such a joy now to communicate with my rails back end. Just a quick question though. I noticed there is support in the config for oauth and basic authentication, but is there no option for using tokens? I am currently using authenticate_or_request_with_http_token on a project. But I can't seem to find a way to configure NSRails to put just a token field in the Authorization header for all of the requests. Any plans for including this?

Thanks

Matt

@dingbat
Copy link
Owner

dingbat commented Nov 4, 2012

Hey Matt, sorry for the delayed response.

You bring up a good point. I added a quick property for this – let me know if it works for you. Usage is simple:

[[NSRConfig defaultConfig] setAdditionalHTTPHeaders:@{@"token" : @"ABC123"}];

From there on, every request (that uses that config (which is default)) will use that token header.

@mdfdroid
Copy link
Author

mdfdroid commented Nov 9, 2012

Maybe I'm am not quite understanding the rails implementation of this, but I changed your lines to this

[self.config.additionalHTTPHeaders enumerateKeysAndObjectsUsingBlock:
     ^(id key, id obj, BOOL *stop)
     {

         NSString *authHeader = [NSString stringWithFormat:@"Token token=%@", obj];
         [request setValue:authHeader forHTTPHeaderField:@"Authorization"];
     }];

and set the config with

 NSMutableDictionary *token = [NSMutableDictionary dictionaryWithObject:@"d7c110c4396bf89251f581bfaf196280" forKey:@"token"];
 [[NSRConfig defaultConfig] setAdditionalHTTPHeaders:token];

This is the only way I could get it to work?

The header has to end up looking like this.

Authorization: Token token=d7c110c4396bf89251f581bfaf196280\r\n

This is what the rails function looks like

 def authorize
    case request.format
    when Mime::JSON, Mime::XML
      authenticate_or_request_with_http_token do |token, options|
        App.exists?(key: token)
      end
    else
      redirect_to new_user_session_path, alert: "Unauthorized Access"
    end
  end

Thanks again for the help

@dingbat
Copy link
Owner

dingbat commented Nov 9, 2012

Ah, I see. That's simple. Change the code back to the code I committed. The additional headers functions like a dictionary, so you'd want to use this key/value pair:

NSMutableDictionary *token = [NSMutableDictionary dictionaryWithObject:@"Token token=d7c110c4396bf89251f581bfaf196280" forKey:@"Authorization"];
[[NSRConfig defaultConfig] setAdditionalHTTPHeaders:token];

@mdfdroid
Copy link
Author

mdfdroid commented Nov 9, 2012

Ok if I do that, it gives me this as the Authorization header

Authorization: token:Authorization

And there isn't any other header created. You think there is an issue with the decoder for the spaces?

EDIT

Ok hang on..... nevermind... somehow I was using some different code than you had here on the repo, all is well now, and it should have been from the start hah. Thanks again.

@dingbat
Copy link
Owner

dingbat commented Nov 9, 2012

I'm not sure I understand your result. The snippet I gave you would replicate exactly the tweak you had to do to my code. Are you sure the code in Request.m is changed back?

And you're not using either HTTP Basic or OAuth as well right?

What do you mean by "an issue with the decoder for the spaces"?

@mdfdroid
Copy link
Author

mdfdroid commented Nov 9, 2012

My edit above, guess I should be more clear. I thought I reverted back to your code, but I guess I was playing around with it before and just didn't remember, so the code I thought was yours wasn't. What I get for staying up too late working.

I put your original code from the commit back in and all is working as intended.

Sorry for the confusion ha.

@dingbat
Copy link
Owner

dingbat commented Nov 9, 2012

No problem, glad to hear it! Closing this for now, feel free to reopen if anything else comes up.

@dingbat dingbat closed this as completed Nov 9, 2012
@matthewsinclair
Copy link

Sorry to bother you on a closed issue - but I'm trying to use 2.0.1 with RubyMotion and I can't find any sign of the setAdditionalHTTPHeaders method on NSRConfig. It looks like it's in the source here, but the version I'm getting thru Cocoapods (2.0.1) doesn't have it. Am I doing something stupid?

For example, sould I expect something like this to work?

token = NSMutableDictionary.dictionaryWithObject("auth-token...", forKey:"X-Auth-Token")
NSRConfig.defaultConfig.setAdditionalHTTPHeaders:token

If not, how do I load up the requests with additional headers?

dingbat added a commit that referenced this issue Apr 2, 2013
@dingbat
Copy link
Owner

dingbat commented Apr 2, 2013

Hi, no problem!

I pushed a new version tag that includes that feature (2.0.2) -- Cocoapods should pick it up now.

Sorry for the delay.

@matthewsinclair
Copy link

Thanks for chasing that up. Much appreciated.

How long do you think it will take to wash thru to the Cocoapods directory? I'm getting this at the moment:

$ pod install
Analyzing dependencies
[!] Required version (NSRails (~> 2.0.2)) not found for `NSRails`.
Available versions: 2.0.1

@matthewsinclair
Copy link

I've noticed that the Cocoapods spec repo has not been updated fro 2.0.2 (which explains the problem above):

https://github.com/CocoaPods/Specs/tree/master/NSRails

Is that something that takes some time to wash thru?

@matthewsinclair
Copy link

Hi there - I've tried using the pod directly form the Git repo in order to get 2.0.2 but I'm still having trouble setting any header variables. I can see the request hitting my rails app, but because i can't seem to set the auth token, the Rails app is rejecting the request.

Here's the REPL output:

(main)> token = NSMutableDictionary.dictionaryWithObject("MpFKDTwDu5svNyfFcgjo", forKey:"X-Auth-Token")
=> {"X-Auth-Token"=>"MpFKDTwDu5svNyfFcgjo"}
(main)> NSRConfig.defaultConfig.additionalHTTPHeaders.addEntriesFromDictionary(token)
2013-04-03 10:42:27.712 T-Shopper[22161:c07] undefined method `additionalHTTPHeaders' for #<NSRConfig:0xbd93ea0> (NoMethodError)
=> #<NoMethodError: undefined method `additionalHTTPHeaders' for #<NSRConfig:0xbd93ea0>>
(nil)?  

As you can see from the above, I can't set the headers because NSRConfig is not responding to the "additionalHTTPHeaders" property.

Is there some way to check which version of the NSRConfig class I'm working with (at runtime)?

@matthewsinclair
Copy link

Doing a bit more investigation, and I think there might be something wrong with the checked-in pod spec configuration in some way. Even when using the gem from GitHub with the tag = 2.0.2 setting, the Podfile.lock file in vendor/Podfile.lock is showing 2.0.1, even though the Podfile.lock in the root of the project shows 2.0.2.

My project Podfile (in the root) of the project looks like this:

platform :ios, '6.0'
pod 'NSRails', :git => 'git@github.com:dingbat/nsrails.git', :tag => "v2.0.2"

And this is what the Podfile.lock file from the root of the project looks like after a "pod install":

PODS:
  - NSRails (2.0.2)

DEPENDENCIES:
  - NSRails (from `git@github.com:dingbat/nsrails.git`, tag `v2.0.2`)

EXTERNAL SOURCES:
  NSRails:
    :git: git@github.com:dingbat/nsrails.git
    :tag: v2.0.2

SPEC CHECKSUMS:
  NSRails: 6e21ec06f955b00c80a5f0ed41a78f38a178eb78

COCOAPODS: 0.17.0

However, this is what vendor/Podfile.lock looks like after a "pod install"

PODS:
  - NSRails (2.0.1)

DEPENDENCIES:
  - NSRails

SPEC CHECKSUMS:
  NSRails: 6c628aa8f38fc8a1b480837c311107d9bc0f0890

COCOAPODS: 0.17.0

I'd be the first to admit that I don't know very much about the internals of Podfiles, but this doesn't seem like it's correct to me.

Am I doing something wrong? Or has the bump to 2.0.2 not quite worked properly?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants