Skip to content
Browse files

extract core plugin

  • Loading branch information...
0 parents commit 80400af92c0abe34c4a8a293a8a6d183732d4e60 Ryan Sonnek committed
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in devise_oauth2_token_bearer_authenticatable.gemspec
+gemspec
2 Rakefile
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
24 devise_oauth2_token_bearer_authenticatable.gemspec
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "devise_oauth2_token_bearer_authenticatable/version"
+
+Gem::Specification.new do |s|
+ s.name = "devise_oauth2_token_bearer_authenticatable"
+ s.version = DeviseOauth2TokenBearerAuthenticatable::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Ryan Sonnek"]
+ s.email = ["ryan@socialcast.com"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "devise_oauth2_token_bearer_authenticatable"
+
+ s.add_runtime_dependency(%q<devise>, ["~> 1.3.3"])
+ s.add_runtime_dependency(%q<rack-oauth2>, ["~> 0.6.3"])
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
17 lib/devise_oauth2_token_bearer_authenticatable.rb
@@ -0,0 +1,17 @@
+require 'devise'
+require 'rack/oauth2'
+require 'devise_oauth2_token_bearer_authenticatable/strategy'
+require 'devise_oauth2_token_bearer_authenticatable/model'
+require 'devise_oauth2_token_bearer_authenticatable/schema'
+
+module Devise
+ module Oauth2TokenBearerAuthenticatable
+ # Your code goes here...
+
+ end
+end
+
+Devise.add_module(:oauth2_token_bearer_authenticatable,
+ :strategy => true,
+ :model => 'devise_oauth2_token_bearer_authenticatable/model')
+
12 lib/devise_oauth2_token_bearer_authenticatable/model.rb
@@ -0,0 +1,12 @@
+require 'devise/models'
+
+module Devise
+ module Models
+ module Oauth2TokenBearerAuthenticatable
+ extend ActiveSupport::Concern
+ included do
+ has_many :access_tokens
+ end
+ end
+ end
+end
44 lib/devise_oauth2_token_bearer_authenticatable/schema.rb
@@ -0,0 +1,44 @@
+require 'devise/schema'
+
+module Devise
+ module Oauth2TokenBearerAuthenticatable
+ module Schema
+ def self.up(migration)
+ migration.create_table :clients do |t|
+ t.string :name
+ t.string :redirect_uri
+ t.string :website
+ t.string :identifier
+ t.string :secret
+ t.timestamps
+ end
+ migration.add_index :clients, :identifier
+
+ migration.create_table :access_tokens do |t|
+ t.belongs_to :user, :client
+ t.string :token
+ t.datetime :expires_at
+ t.timestamps
+ end
+ migration.add_index :access_tokens, :token
+ migration.add_index :access_tokens, :expires_at
+
+ migration.create_table :refresh_tokens do |t|
+ t.belongs_to :user, :client
+ t.string :token
+ t.datetime :expires_at
+ t.timestamps
+ end
+ migration.add_index :refresh_tokens, :token
+ migration.add_index :refresh_tokens, :expires_at
+ end
+
+ def self.down(migration)
+ migration.drop_table :refresh_tokens
+ migration.drop_table :access_tokens
+ migration.drop_table :clients
+ end
+ end
+ end
+end
+
19 lib/devise_oauth2_token_bearer_authenticatable/strategy.rb
@@ -0,0 +1,19 @@
+require 'devise/strategies/base'
+
+module Devise
+ module Strategies
+ class Oauth2TokenBearerAuthenticatable < Base
+ def valid?
+ env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN].present?
+ end
+ def authenticate!
+ token = AccessToken.valid.find_by_token env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN]
+ raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized unless token
+ raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new(:invalid_token, 'User token is required') unless token.user
+ success! token.user
+ end
+ end
+ end
+end
+
+Warden::Strategies.add(:oauth2_token_bearer_authenticatable, Devise::Strategies::Oauth2TokenBearerAuthenticatable)
3 lib/devise_oauth2_token_bearer_authenticatable/version.rb
@@ -0,0 +1,3 @@
+module DeviseOauth2TokenBearerAuthenticatable
+ VERSION = "0.0.1"
+end

0 comments on commit 80400af

Please sign in to comment.
Something went wrong with that request. Please try again.