Permalink
Browse files

[server] Added CORS support using rack-cors and created a sample Java…

…Script client.
  • Loading branch information...
1 parent f36f4ab commit 611bfd04f23c21289fbd9339f2f8ec34ab105484 @yatsu committed Nov 30, 2012
View
@@ -50,6 +50,7 @@ gem 'thor', '~> 0.16.0'
gem 'oauth2', '~> 0.8.0'
gem "omniauth-facebook", '~> 1.4.1'
gem "koala", '~> 1.5.0'
+gem 'rack-cors', :require => 'rack/cors'
# gem 'msgpack', '~> 0.4.7'
gem 'capistrano'
View
@@ -200,6 +200,8 @@ GEM
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
+ rack-cors (0.2.7)
+ rack
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
@@ -314,6 +316,7 @@ DEPENDENCIES
pry
pry-rails
rabl (~> 0.7.2)
+ rack-cors
rails (= 3.2.8)
rb-fsevent
rspec-rails
@@ -72,5 +72,12 @@ class Application < Rails::Application
# Only Authorized Applications
# Doorkeeper::AuthorizedApplicationsController.layout "application"
end
+
+ config.middleware.use Rack::Cors do
+ allow do
+ origins '*'
+ resource '*', :headers => :any, :methods => [ :get, :post, :options ]
+ end
+ end
end
end
View
@@ -0,0 +1,3 @@
+require './web_client'
+
+run WebClient.new
View
@@ -0,0 +1,79 @@
+<!DOCTYPE html>
+
+<html lang="en">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>OAuth 2 User Agent Authentication Flow Demo</title>
+ <script type="text/javascript" charset="utf-8" src="javascript/jquery-1.6.3.js"></script>
+ <script type="text/javascript" charset="utf-8">
+ $(function () {
+ var extractToken = function(hash) {
+ var match = hash.match(/access_token=(\w+)/);
+ return !!match && match[1];
+ };
+
+ var setting =
+ {
+ 'host': "bestmix.local",
+ 'clientId': "a8d2c99c5582370972c2500176e9949a1257a35a9186b25818229cebf49f5de2"
+ };
+
+ var authHost = "http://" + setting.host;
+ var resourceHost = "http://" + setting.host;
+
+ var endUserAuthorizationEndpoint = authHost + "/oauth/authorize";
+
+ var token = extractToken(document.location.hash);
+ if (token) {
+ $('div.authenticated').show();
+
+ $('#token').text(token);
+
+ $.ajax({
+ type: 'get',
+ dataType: 'json',
+ url: resourceHost + '/api/v1/my_posts.json',
+ beforeSend: function (xhr) {
+ xhr.setRequestHeader('Authorization', "Bearer " + token);
+ xhr.setRequestHeader('Accept', "application/json");
+ },
+ success: function (response) {
+ if (response) {
+ $('#response').text(JSON.stringify(response));
+ } else {
+ container.text("An error occurred.");
+ }
+ }
+ });
+ } else {
+ $('div.authenticate').show();
+
+ var authUrl = endUserAuthorizationEndpoint +
+ "?response_type=token" +
+ "&client_id=" + setting.clientId +
+ "&redirect_uri=" + window.location;
+
+ $("a.connect").attr("href", authUrl);
+ }
+ });
+ </script>
+ </head>
+
+ <body>
+ <div class="authenticate">
+ <a class="connect" href="">Connect</a>
+ </div>
+
+ <div class="authenticated" style="display: none;">
+ <p>
+ Token:<br/>
+ <span id="token">[no token]</span>
+ </p>
+
+ <p>
+ Response:<br/>
+ <span id="response">[no response]</span>
+ </p>
+ </div>
+ </body>
+</html>
Oops, something went wrong.

0 comments on commit 611bfd0

Please sign in to comment.