Skip to content

Instantly share code, notes, and snippets.

@yoshimax
Last active December 27, 2015 01:59
Show Gist options
  • Select an option

  • Save yoshimax/7248837 to your computer and use it in GitHub Desktop.

Select an option

Save yoshimax/7248837 to your computer and use it in GitHub Desktop.
Using Resource Owner Password Credentials setting with doorkeeper + devise
Doorkeeper.configure do
resource_owner_authenticator do |routes|
current_user || warden.authenticate!(:scope => :user)
end
resource_owner_from_credentials do |routes|
request.params[:user] = {:email => request.params[:username], :password => request.params[:password]}
request.env["devise.allow_params_authentication"] = true
request.env["warden"].authenticate!(:scope => :user)
end
end
# test
$ curl -d client_id="XXXX" -d client_secret="XXXX" -d username="XXXX" -d password="XXXX" -d grant_type="password" http://IP/oauth/token
# res
{"access_token":"068935cabb4f867c70086bdee8f5e05d5868b3ca6f63c86f5809fe92fe33700e","token_type":"bearer","expires_in":7200}
# api ---------------------------------------
# routes.rb
namespace :api do
namespace :v1 do
get 'user/index'
end
end
$rails g controller api/v1/user
# user_controller.rb
class Api::V1::UserController < ApplicationController
doorkeeper_for :all
before_action :validate_token
before_action :set_parameters
skip_before_action :verify_authenticity_token
def index
render json: current_user
end
end
# test
curl -H "Authorization: Bearer 068935cabb4f867c70086bdee8f5e05d5868b3ca6f63c86f5809fe92fe33700e" http://IP/api/v1/user/index
{"id":1,"email":"XXXX","created_at":"2013-10-30T03:55:36.000+09:00","updated_at":"2013-11-01T04:22:17.000+09:00","username":null}
#AFNetworking2
- (IBAction)getToken:(id)sender {
NSString *URLString = @"http://IP/oauth/token";
NSDictionary *parameters = @{@"client_id":self.client_id, @"client_secret":self.client_secret, @"username":self.username , @"password":self.password, @"grant_type":@"password"};
NSLog(@"parameters %@", parameters);
NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
NSLog(@"access_token: %@", responseObject[@"access_token"]);
self.token = responseObject[@"access_token"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
self.token = nil;
}];
[[NSOperationQueue mainQueue] addOperation:op];
}
- (IBAction)getUser:(id)sender {
NSString *URLString = @"http://IP/api/v1/user/index";
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@", self.token] forHTTPHeaderField:@"Authorization"];
NSURLRequest *request = [requestSerializer requestWithMethod:@"GET" URLString:URLString parameters:nil];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
self.token = responseObject[@"access_token"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
self.token = nil;
}];
[[NSOperationQueue mainQueue] addOperation:op];
}
# devise error JSON ---------------------------------------
config/initializers/custom_auth_failure_app.rb
class CustomAuthFailure < Devise::FailureApp
def respond
if request.format == :json
self.status = 401
self.content_type = 'json'
self.response_body = {"errors" => i18n_message}.to_json
else
super
end
end
end
config/initializers/devise.rb
config.warden do |manager|
manager.failure_app = CustomAuthFailure
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment