iOS HTTPS request to Rails Heroku debugging 401 error - ios

I am attempting to adapt this wenderlich tutorial to Swift 3 syntax to create an iOS frontned with rails backend hosted on Heroku.
Let me know if there is any other code that would be good to share, but it seems like an auth issue. Here's where the authorization header is set for the request:
let basicAuthString = "\(HTTPHelper.API_AUTH_NAME):\(HTTPHelper.API_AUTH_PASSWORD)"
let utf8str = basicAuthString.data(using: String.Encoding.utf8)
let base64EncodedString = utf8str?.base64EncodedString(options: Data.Base64EncodingOptions())
request.addValue("Basic \(base64EncodedString)", forHTTPHeaderField: "Authorization")
I am having trouble telling how to debug these requests to begin with. If I step through the code in the debugger, the request object seems pretty flat, and if I print the request I just see the URL.
The request to the heroku server (also built from that tutorial) returns a 401, and it doesn't seem to be entering any of the rails methods. It just returns a 401 unauthorized:
2017-03-24T20:13:47.022176+00:00 heroku[router]: at=info method=POST path="/api/signin" host=young-retreat-61850.herokuapp.com request_id=9d58e8f4-ee9f-4c73-babd-2a2ee5c82a4f fwd="73.83.200.10" dyno=web.1 connect=1ms service=13ms status=401 bytes=499 protocol=https
2017-03-24T20:13:47.014605+00:00 app[web.1]: Started POST "/api/signin" for 73.83.200.10 at 2017-03-24 20:13:47 +0000
2017-03-24T20:13:47.020239+00:00 app[web.1]: Processing by ApiController#signin as */*
2017-03-24T20:13:47.020279+00:00 app[web.1]: Parameters: {"email"=>"dsadsadsa", "password"=>"[FILTERED]", "api"=>{"email"=>"dsadsadsa", "password"=>"[FILTERED]"}}
2017-03-24T20:13:47.020671+00:00 app[web.1]: Filter chain halted as #<Proc:0x007f6ae4d6f140#/app/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.5/lib/action_controller/metal/http_authentication.rb:71> rendered or redirected
2017-03-24T20:13:47.020791+00:00 app[web.1]: Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
2017-03-24T20:13:47.142925+00:00 app[web.1]: Started POST "/api/signin" for 73.83.200.10 at 2017-03-24 20:13:47 +0000
2017-03-24T20:13:47.148098+00:00 app[web.1]: Processing by ApiController#signin as */*
2017-03-24T20:13:47.148134+00:00 app[web.1]: Parameters: {"email"=>"dsadsadsa", "password"=>"[FILTERED]", "api"=>{"email"=>"dsadsadsa", "password"=>"[FILTERED]"}}
2017-03-24T20:13:47.156450+00:00 app[web.1]: Filter chain halted as #<Proc:0x007f6ae4d6f140#/app/vendor/bundle/ruby/2.1.0/gems/actionpack-4.1.5/lib/action_controller/metal/http_authentication.rb:71> rendered or redirected
2017-03-24T20:13:47.156647+00:00 app[web.1]: Completed 401 Unauthorized in 8ms (ActiveRecord: 0.0ms)
2017-03-24T20:13:47.157852+00:00 heroku[router]: at=info method=POST path="/api/signin" host=young-retreat-61850.herokuapp.com request_id=bc270aa1-aeed-40b8-b9a0-3c09a68dec8d fwd="73.83.200.10" dyno=web.1 connect=2ms service=24ms status=401 bytes=458 protocol=https
Should it seem strange that the nested "api" key has the same params? I am unclear on which direction to take for debugging these requests. I can't use a service like Charles because they are https right?

Feel like a fool...
the base64EncodedString was an optional.
fix:
request.addValue("Basic \(base64EncodedString!)", forHTTPHeaderField: "Authorization")
I was able to get more details about the request with this URLRequest method:
print("request header:\(request.allHTTPHeaderFields)")

Related

Rails Google Oauth - Works for some users and not others

I am building rails and google oauth for a project. Right now, it is a very straight forward set up
# gemfile
gem 'rails', '~> 5.2.3'
gem 'administrate'
gem 'devise', github: 'heartcombo/devise', branch: 'ca-omniauth-2'
gem 'omniauth-google-oauth2'
gem 'dotenv-rails', groups: [:development, :test]
gem 'google-api-client', require: 'google/apis/calendar_v3'
gem 'omniauth-rails_csrf_protection'
# devise.rb
config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], {
access_type: "offline",
prompt: "consent",
select_account: true,
scope: 'userinfo.email, calendar, openid',
provider_ignores_state: true
}
and then I have my code working for auth'ing with Google and logging users into devise.
I am using google calendar for a scope, which is now as of 2021 considered a sensitive scope, and because of that I have to add test users to the google project to get it to work. Currently in my google set up, I have 2 users white-listed as allowed users to use the app.
Right now, 1 user is able to log into the app just fine. They go to the page, auth with google, accept permissions, and google logs them in.
For 1 other user, google does not work. They go to the page, attempt to auth with google, and get a page back that says
SOMETHING WENT WRONG
sorry, something went wrong there. Try again.
and then are redirected to the google error page that says
400. That's an error
The server cannot process the request because it is malformed. It should not be retried.
That's all we know.
I'm not able to figure this out, because it is working perfectly for 1 user and not for the other. The only thing I can think of is looking in the heroku logs. I was able to find requests for both users.
Here are the logs for a successful login
2021-01-26T19:15:49.316985+00:00 app[web.1]: I, [2021-01-26T19:15:49.316862 #4] INFO -- : [c8194f6f-6cc9-4d1c-8ea2-9439d4a3f758] Started POST "/admin_users/auth/google_oauth2" for 108.80.197.86 at 2021-01-26 19:15:49 +0000
2021-01-26T19:15:49.317524+00:00 app[web.1]: D, [2021-01-26T19:15:49.317454 #4] DEBUG -- omniauth: (google_oauth2) Request phase initiated.
2021-01-26T19:15:49.331305+00:00 heroku[router]: at=info method=POST path="/admin_users/auth/google_oauth2" host=<host> request_id=c8194f6f-6cc9-4d1c-8ea2-9439d4a3f758 fwd="108.80.197.86" dyno=web.1 connect=1ms service=16ms status=302 bytes=1662 protocol=https
2021-01-26T19:15:59.988809+00:00 app[web.1]: I, [2021-01-26T19:15:59.988635 #4] INFO -- : [cb1c461a-de2d-47cf-97d7-c5c594dcedc2] Started GET "/admin_users/auth/google_oauth2/callback?state=<state>&code=<code>&scope=email%20https://www.googleapis.com/auth/calendar%20https://www.googleapis.com/auth/userinfo.email%20openid&authuser=0&prompt=consent" for 108.80.197.86 at 2021-01-26 19:15:59 +0000
2021-01-26T19:15:59.989574+00:00 app[web.1]: D, [2021-01-26T19:15:59.989451 #4] DEBUG -- omniauth: (google_oauth2) Callback phase initiated.
2021-01-26T19:16:00.159852+00:00 app[web.1]: I, [2021-01-26T19:16:00.159697 #4] INFO -- : [cb1c461a-de2d-47cf-97d7-c5c594dcedc2] Processing by AdminUsers::OmniauthCallbacksController#google_oauth2 as HTML
2021-01-26T19:16:00.159977+00:00 app[web.1]: I, [2021-01-26T19:16:00.159863 #4] INFO -- : [cb1c461a-de2d-47cf-97d7-c5c594dcedc2] Parameters: {"state"=>"<state>", "code"=>"<code>", "scope"=>"email https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email openid", "authuser"=>"0", "prompt"=>"consent"}
Here are the logs for an unsuccessful login
2021-01-26T14:16:32.326486+00:00 app[web.1]: I, [2021-01-26T14:16:32.326382 #4] INFO -- : [545e059b-7f9b-4687-8b0e-b9f7479625e2] Started POST "/admin_users/auth/google_oauth2" for 107.203.102.116 at 2021-01-26 14:16:32 +0000
2021-01-26T14:16:32.326882+00:00 app[web.1]: D, [2021-01-26T14:16:32.326810 #4] DEBUG -- omniauth: (google_oauth2) Request phase initiated.
2021-01-26T14:16:32.331603+00:00 heroku[router]: at=info method=POST path="/admin_users/auth/google_oauth2" host=<host> request_id=545e059b-7f9b-4687-8b0e-b9f7479625e2 fwd="107.203.102.116" dyno=web.1 connect=0ms service=9ms status=302 bytes=1674 protocol=https
So in looking at the logs, the unsuccessful request just stops after the user hits the google button and google seems like it's not calling back the request.
Why would it be working for 1 user, and not working for another?
Any help is much appreciated
This was resolved. the problem was the other user was using a google provided email that was not a gmail i.e. a email#domain.com email address. even though it is controlled by google, it seems like you need a gmail to auth. we switched to a gmail email and it worked fine.

Autoscaling with periodic slow requests

We're using the autoscaler on Heroku. Almost all the requests are very quick (0-500ms), but periodically we have an image upload that takes a long time (3000-9000ms). Autoscaling doesn't help at all in this case. Is there a best practice handle this? For instance, is there a way to omit a particular URL from the autoscaler? Or do you handle this by setting up a parallel app just to handle these transactions?
Here is an example "slow" transaction (with identifiers anonymized) -- all times are intact:
May 22 11:46:19 production heroku/router: at=info method=POST path="/api/users/cases/24070/case_posts.json" host=www.website.com request_id=XXX...XXX fwd="xxx.xxx.xxx.xxx" dyno=web.1 connect=0ms service=10483ms status=200 bytes=1762 protocol=https
Started POST "/api/users/cases/24070/case_posts.json" for xxx.xxx.xxx.xxx at 2018-05-22 18:46:10 +0000
Processing by Users::CasePostsController#create as JSON
Parameters: {
"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f3068cb98d8 #tempfile=#<Tempfile:/tmp/RackMultipart20180522-11-1x9ayll.JPG>,
#original_filename="IMG_1325.JPG",
#content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"image\"; filename=\"IMG_1325.JPG\"\r\nContent-Type: image/jpeg\r\n">,
"step"=>"pack out",
...
}
Rendered users/case_posts/_case_post.json.jbuilder (1.0ms)
Rendering users/case_posts/show.json.jbuilder
Rendered users/case_posts/_case_post.json.jbuilder (0.4ms)
Rendered users/case_posts/show.json.jbuilder (1.0ms)
Completed 200 OK in 9120ms (Views: 1.8ms | ActiveRecord: 29.8ms)

Users can't sign in after changing from cookie session to server-side session storage (Devise, Rails)

I have also changed the domain of the cookie so that it would work on subdomains ("Switched from domain.com cookie to .domain.com cookie
The issue is users can't log in. Unless they clear their cookie or log in on incognito mode.
After a user logs in, they are just taken back to the sign in page without any errors.
Logs are showing no errors other than a 302, here's an example:
Started POST "/users/sign-in" for 174.235.55.66 at 2015-05-16 18:38:20 +0000
May 16 11:38:21 abc app/web.1: Processing by Users::SessionsController#create as HTML
May 16 11:38:21 abc app/web.1: Parameters: {"utf8"=>"✓", "user"=>{"email"=>"email#domain.com", "password"=>"[FILTERED]", "remember_me"=>"0"}}
May 16 11:38:21 abc heroku/router: at=info method=POST path="/users/sign-in" host=www.bidvine.com request_id=f3f8fd11-acc5-443a-866d-46c554bcabb1 fwd="70.53.68.47,173.245.55.66" dyno=web.1 connect=1ms service=454ms status=302 bytes=794
May 16 11:38:21 abc app/web.1: Redirected to http://www.bidvine.com/admin
May 16 11:38:21 abc app/web.1: Completed 302 Found in 392ms (ActiveRecord: 35.2ms)
May 16 11:38:21 abc app/web.2: Processing by Admin::RequestsController#index as HTML
May 16 11:38:21 abc app/web.3: Started GET "/users/sign-in" for 173.245.55.66 at 2015-05-16 18:38:21 +0000

"Poorly formatted HTTP response" in ruby on rails app in heroku

Our Production(SquareOff) and Staging(so-staging) App suddenly facing a terrible issue. The issue is when we send request to the app it failed to response. The response log is bellow:
2015-05-14T06:48:02.236633+00:00 heroku[router]: http_error="Invalid HTTP status line" at=error code=H17 desc="Poorly formatted HTTP response" method=POST path="/users/invitation" host=so-staging.herokuapp.com request_id=01f23953-580c-484a-9d6b-89ff19d70b2b fwd="203.202.242.130" dyno=web.1 connect=0ms service=1049ms status=503 bytes=755
However, sometimes it works perfectly. The response log is bellow for this scenario:
2015-05-14T06:43:10.129616+00:00 heroku[router]: at=info method=POST path="/users/invitation" host=so-staging.herokuapp.com request_id=cb961af7-c249-4705-8ed0-226fad76e6ab fwd="203.202.242.130" dyno=web.1 connect=1ms service=1362ms status=302 bytes=1317
Also i found this link that discuss about this issue. https://github.com/xyu/heroku-wp/issues/3 but the solution is really bad and don't acceptable.
I appreciate help regarding how to fix this issue.

Ajax request to other websites don't work at heroku [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a Rails app which does an ajax request to the other web site when an user clicks on the button. On localhost it works just fine but at heroku it doesn't. It return "We're sorry, but something went wrong." (500) and here is the logs
Processing by HomeController#some_request as */*
2013-04-09T12:42:33.050720+00:00 app[web.1]: Parameters: {"q"=>"another_web_site.com"}
2013-04-09T12:42:33.048011+00:00 app[web.1]: Started POST "/ajax/some_request" for 180.183.158.162 at 2013-04-09 12:42:33 +0000
2013-04-09T12:42:29.793791+00:00 heroku[router]: at=info method=GET path=/assets/application.js host=my_host.org fwd="180.183.158.162" dyno=web.1 connect=1ms service=5ms status=304 bytes=0
2013-04-09T12:42:54.057280+00:00 app[web.1]:
2013-04-09T12:42:54.057280+00:00 app[web.1]: Errno::ETIMEDOUT (Connection timed out - connect(2)):
2013-04-09T12:42:54.057280+00:00 app[web.1]:
2013-04-09T12:42:54.057280+00:00 app[web.1]:
2013-04-09T12:42:54.057280+00:00 app[web.1]: lib/domain_info.rb:14:in `who_is'
2013-04-09T12:42:54.057280+00:00 app[web.1]: app/controllers/home_controller.rb:8:in `who_is'
2013-04-09T12:42:54.055802+00:00 app[web.1]: Completed 500 Internal Server Error in 21004ms
2013-04-09T12:42:54.059787+00:00 heroku[router]: at=info method=POST path=/ajax/some_request host=my_host.org fwd="180.183.158.162" dyno=web.1 connect=2ms service=21017ms status=500 bytes=643
Now it's timeout. Right yesterday it was https://devcenter.heroku.com/articles/error-codes#h18-request-interrupted where sock field was equal to client.
Why is that happening?
Whatever your app is doing in lib/domain_info.rb is timing out - are you sure that code works from locations other than your machine/localhost? What is that code doing? Sharing that would be helpful.

Resources