REST and passing authentication token - html

I'm writing my first REST program and I'm struggling with passing the session token. Also I have looked in several forums for similar issues questions, but with no luck.
So I have the following piece of Ruby chunk code (not the whole script):
require 'rest-client'
require 'json'
headers_lbvserver = {
'Content-Type' => application/vnd.com.citrix.netscaler.lbvserver+json',
'accept' => :json
}
add_lbvserver = RestClient::Request.execute(
:method => :post,
:url => url_lbvserver,
:headers => headers_lbvserver,
:payload => payload_lbvserver,
:cookie => {'NITRO_AUTH_TOKEN=' => token_login}
)
So I have already received the token from a previous REST post, but I can't passing it trough when i maken my second call to the object. It comes with the following error: 400 Bad Request (RestClient::BadRequest)

After looking through the docs of RestClient, it looks like you are declaring your cookie incorrectly. You don't need to have an equal sign after the cookie name:
:cookie => {'NITRO_AUTH_TOKEN=' => token_login}
should be
:cookie => {'NITRO_AUTH_TOKEN' => token_login}

Related

Have same route respond differently based on HTTP verb

The following is a small snippet of my routes in _urlManager.php
["pattern" => 'POST /create_chain', 'route' => 'site/add-chain'],
["pattern" => '/create_chain', 'route' => 'site/create-chain']
As you can see for POST I want a different action to be called. But this does not work.
For now I've used the following solution on temporary basis:
"POST /create_chain" => "site/add-chain",
["pattern" => '/create_chain', 'route' => 'site/create-chain']
But I'm not OK with this solution. If anyone knows how I can integrate HTTP VERB in pattern, please comment or answer.
You should use verb key if you want to configure verb using array syntax:
["pattern" => '/create_chain', 'verb' => 'POST', 'route' => 'site/add-chain'],

How to wrap Datamapper to_json response in a object-key hash

I'm working with a json parser that requires my response to be wrapped in an object-key hash. When I use DataMapper's .to_json method(datamapper/dm-serializer) the repsone I get is correct
get '/plane/all' do
#plane = Plane.all(:order => :id.desc).to_json(:relationships => {:pilots => {}, :passengers => {}, :cabin => {}})
#plane
end
The response in JSON
[{"id":2,"name":"Plane 2","picture_url":"https://s3.amazonaws.com","pilots":[{"pilot_id":2,"header":"Bruce Wayne","details":"Bruce loves his batwing that flies at an average speed of 200 mph","picture_url":"www.marvel.com","plane_id":2}],"passengers":[{"passenger_id":2,"name":"Passenger 2","details":"These are the details for passenger 2","picture_url":"www.toobar.com/","plane_id":2}],"cabin":[{"cabin_id":2,"details":"Great Details for has been put in this cabin","picture_url":"www.seatingchart.com","video_link":"www.skyview.com","passenger_passenger_id":2}]},{"id":1,"name":"Plane 1","picture_url":"https://s3.amazonaws.com","pilots":[{"pilot_id":1,"header":"Jack Smith","details":"Jack Smith description","picture_url":"https://www.foobar.com","plane_id":1}],"passengers":[{"passenger_id":1,"name":"Passenger 1","details":"Passenger 1 details","picture_url":"www.toobar.com/","passenger_id":1}],"cabin":[{"cabin_id":1,"details":"Flight details","picture_url":"www.seatingchart.com","video_link":"www.skyview.com","passenger_passenger_id":1}]}]
To wrap this response as a key I used the 'json' gem which has it's own to_json method
get '/plane/all' do
#plane = Plane.all(:order => :id.desc).to_json(:relationships => {:pilots => {}, :passengers => {}, :cabin => {}})
{ "planes" => #plane }.to_json
end
Here is the response
{"planes":"[{\"id\":2,\"name\":\"Plane 2\",\"picture_url\":\"https://s3.amazonaws.com\",\"pilots\":[{\"pilot_id\":2,\"header\":\"Bruce Wayne\",\"details\":\"Bruce loves his batwing that flies at an average speed of 200 mph\",\"picture_url\":\"www.marvel.com\",\"plane_id\":2}],\"passengers\":[{\"passenger_id\":2,\"name\":\"Passenger 2\",\"details\":\"These are the details for passenger 2\",\"picture_url\":\"www.toobar.com/\",\"plane_id\":2}],\"cabin\":[{\"cabin_id\":2,\"details\":\"Great Details for has been put in this cabin\",\"picture_url\":\"www.seatingchart.com\",\"video_link\":\"www.skyview.com\",\"passenger_passenger_id\":2}]},{\"id\":1,\"name\":\"Plane 1\",\"picture_url\":\"https://s3.amazonaws.com\",\"pilots\":[{\"pilot_id\":1,\"header\":\"Jack Smith\",\"details\":\"Jack Smith description\",\"picture_url\":\"https://www.foobar.com\",\"plane_id\":1}],\"passengers\":[{\"passenger_id\":1,\"name\":\"Passenger 1\",\"details\":\"Passenger 1 details\",\"picture_url\":\"www.toobar.com/\",\"plane_id\":1}],\"cabin\":[{\"cabin_id\":1,\"details\":\"Flight details\",\"picture_url\":\"www.seatingchart.com\",\"video_link\":\"www.skyview.com\",\"passenger_passenger_id\":1}]}]"}
Long story short, the JSON response I get has backslashes in it and JSON parser I'm using states it's only one object instead of the two I had previously.
Anyone who can help me out, it would be greatly appreciated.
Here is my workaround. If there is no view, then no need to use an instance variable.
def '/plane/all' do
planes = Plane.all(:order => :id.desc).to_json(:relationships => {:pilots => {}, :passengers => {}, :cabin => {}})
{ "planes" => JSON.parse(planes) }.to_json
end

Laravel Response::json not returning JSON properly?

Routes registered in app/routes.php
Route::resource('users', 'UsersController',
array('except' => array('new', 'update')));
Route::post('users/authenticate', array('as' => 'authenticate', 'uses' => 'UsersController#authenticate'));
Route::get('users/is_authenticated', array('as' => 'authenticated', 'uses' => 'UsersController#is_authenticated'));
The method is_authenticated is not returning JSON, but when I put the Response::json() in the index method it returns the JSON schema.
Here is my is_authenticated method:
public function is_authenticated()
{
return Response::json(['authenticated' => Auth::check()]);
}
What's going wrong here? I ran php artisan routes and it returns this for the route:
GET|HEAD api/users/is_authenticated | authenticated | UsersController#is_authenticated
I don't get a 404 Not Found when visiting the page, but there is no content. What's the problem?
Edit: routes are prefixed with api
The problem is that your first route is activated when calling users/is_authenticated. The order of the routes is important in Laravel, as the first matching route is executed. You can just change the order of your routes to make the route users/is_authenticated available, like so:
Route::post('users/authenticate', array('as' => 'authenticate', 'uses' => 'UsersController#authenticate'));
Route::get('users/is_authenticated', array('as' => 'authenticated', 'uses' => 'UsersController#is_authenticated'));
Route::resource('users', 'UsersController', array('except' => array('new', 'update')));

CakePHP basic auth on API (json) request

I want to make a request to resource/index.json, but since I index is not allowed without authentication it redirects me to login page. That's the behavior I want when no username:password has been sent
The thing is how do I set AuthComponent to work with both Form and Basic and only check for basic when the request goes through api prefix.
Also, does it automatically authenticate when found username and password in the header or do I have to do it manually?
in respective controller add few lines
class NameController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow("index");
}
}
This will allow index without authentication.
I decided to use Friend's of Cake TokenAuthenticate, and yes, it works along with FormAuthenticate so I am able to use both.
As a matter of fact, it automatically chooses the component it's going to use based on if there is an existing _token param or a X-MyApiTokenHeader header.
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form',
'Authenticate.Token' => array(
'parameter' => '_token',
'header' => 'X-MyApiTokenHeader',
'userModel' => 'User',
'scope' => array('User.active' => 1),
'fields' => array(
'username' => 'username',
'password' => 'password',
'token' => 'public_key',
),
'continue' => true
)
)
)
);

Ordering JSON rendered by Rails controller

I have a Rails 3.1 controller that renders a user's contacts, including associated email objects and message objects. If I am only rendering the contacts, I can do the following:
#contacts = #current_user.contacts.order('last_name asc', :include => [:emails, :messages])
render json: #contacts, :include => [:emails, :messages]
As you can see, I want to sort the contacts by last name rather than the default id. I am now needing to render the user object with other associated objects as well. So I have tried the following, but of course the contacts are not in the appropriate order:
render :status => 200, :json => {
:user => #current_user.as_json(
:include => {
:foos => {
:except => :user_id
},
:contacts => {
:except => :user_id,
:include => [:emails,:messages]
},
:bars => {
:except => :user_id
}
}
)
}
I didn't see any help in the as_json documentation, and I haven't been able to find the right syntax by trial and error.
In this case I would order the contacts in Ruby / SQL and just build your own JSON to render instead of using as_json and its various :include / :except methods.
Build a hash of your data and then send it along to render.
There are all sorts of libraries that can make building JSON easier. JBuilder is one such library. Look at the bottom of the JBuilder page for links to other similar libraries.