Skip to content

Commit

Permalink
Add AR mixins to simple oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
vovadevcode committed Feb 4, 2018
1 parent 43c431a commit 80d9aaa
Show file tree
Hide file tree
Showing 28 changed files with 1,095 additions and 47 deletions.
25 changes: 25 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
engines:
duplication:
enabled: true
config:
languages:
- ruby
- javascript
- python
- php
fixme:
enabled: true
rubocop:
enabled: true
ratings:
paths:
- "**.inc"
- "**.js"
- "**.jsx"
- "**.module"
- "**.php"
- "**.py"
- "**.rb"
exclude_paths:
- spec/
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-ci
50 changes: 5 additions & 45 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,50 +1,10 @@
*.gem
*.rbc
/.config
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/InstalledFiles
/doc/
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
4 changes: 4 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ruby:
config_file: .rubocop.yml

fail_on_violations: true
17 changes: 17 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
AllCops:
Exclude:
- spec/**/*
- db/**/*

Style/FrozenStringLiteralComment:
Enabled: false
Style/StringLiterals:
EnforcedStyle: single_quotes
Metrics/MethodLength:
Max: 15
Metrics/LineLength:
Max: 120
Style/Lambda:
Enabled: false
Style/DotPosition:
Enabled: false
30 changes: 30 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
language: ruby
cache: bundler
bundler_args: --without yard guard benchmarks
notifications:
email: false

addons:
code_climate:
repo_token: a79dc140c09279df903bea582c17d13eecb4e8d80a4d92e1e14f177c74547cc6

matrix:
allow_failures:
- rvm: ruby-head
include:
- rvm: 2.2.6
- rvm: 2.3.3
- rvm: 2.5.0
- rvm: ruby-head

after_success:
- bundle exec codeclimate-test-reporter

before_install:
- gem install bundler -v '~> 1.10'
- bundle install
- rake db:create
- rake db:migrate

script:
- rspec spec
20 changes: 20 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
source 'https://rubygems.org'

gemspec

group :test do
platforms :ruby, :mswin, :mswin64, :mingw, :x64_mingw do
gem 'sqlite3'
end

gem 'activerecord'
gem 'rubocop', '~> 0.49.0', require: false

gem 'codeclimate-test-reporter', '~> 1.0.0'
gem 'coveralls', require: false
gem 'database_cleaner', '~> 1.5.0'
gem 'ffaker'
gem 'otr-activerecord', '~> 1.2.1'
gem 'rspec-rails', '~> 3.6'
gem 'simplecov', require: false
end
90 changes: 88 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
# activerecord_simple_oauth2
ActiveRecord mixin for simple_oauth2.
# ActiveRecord SimpleOAuth2

## Installation

Add this line to your application's *Gemfile:*

```ruby
gem 'activerecord_simple_oauth2'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord_simple_oauth2

## Usage

*OAuth2* workflow implies the existence of the next four roles: **Access Token**, **Access Grant**, **Application** and **Resource Owner**. The gem needs to know what classes work, so you need to create them, and also you need to **configure** [Simple::OAuth2](https://github.com/simple-oauth2/simple_oauth2).

Your project must include 4 models - *AccessToken*, *AccessGrant*, *Client* and *User* **for example**. These models must contain a specific set of API (methods). So everything that you need, it just include each `mixin` to specific class.

***AccessToken*** class:
```ruby
# app/models/access_token.rb

class AccessToken
include ActiveRecord::Simple::OAuth2::AccessToken
end
```

***AccessGrant*** class:
```ruby
# app/models/access_grant.rb

class AccessGrant
include ActiveRecord::Simple::OAuth2::AccessGrant
end
```

***Client*** class:
```ruby
# app/models/client.rb

class Client
include ActiveRecord::Simple::OAuth2::Client
end
```

***User*** class:
```ruby
# app/models/user.rb

class User
include ActiveRecord::Simple::OAuth2::ResourceOwner
end
```

Migration for the simplest use case of the gem looks as follows: [example](https://github.com/simple-oauth2/activerecord_simple_oauth2/tree/master/db/migrations/20180204143133_create_schema.rb)

And that's it.
Also you can take a look at the [mixins](https://github.com/simple-oauth2/activerecord_simple_oauth2/tree/master/lib/activerecord_simple_oauth2/mixins) to understand what they are doing and what they are returning.

## Bugs and Feedback

Bug reports and feedback are welcome on GitHub at https://github.com/simple-oauth2/activerecord_simple_oauth2/issues.

## Contributing

1. Fork the project.
1. Create your feature branch (`git checkout -b my-new-feature`).
1. Implement your feature or bug fix.
1. Add documentation for your feature or bug fix.
1. Add tests for your feature or bug fix.
1. Run `rake` and `rubocop` to make sure all tests pass.
1. Commit your changes (`git commit -am 'Add new feature'`).
1. Push to the branch (`git push origin my-new-feature`).
1. Create new pull request.

Thanks.

## License

The gem is available as open source under the terms of the [MIT License](https://github.com/simple-oauth2/activerecord_simple_oauth2/blob/master/LICENSE).

Copyright (c) 2018 Volodimir Partytskyi (volodimir.partytskyi@gmail.com).
24 changes: 24 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ENV['RACK_ENV'] = 'test'

require 'bundler/setup'
require 'rspec/core/rake_task'
require 'simple_oauth2'
load 'tasks/otr-activerecord.rake'

OTR::ActiveRecord.db_dir = 'db'
OTR::ActiveRecord.migrations_paths = ['db/migrations']

desc 'Default: run specs.'
task default: :spec

namespace :db do
task :environment do
require_relative 'config/db'
end
end

RSpec::Core::RakeTask.new(:spec) do |config|
config.verbose = false
end

Bundler::GemHelper.install_tasks
28 changes: 28 additions & 0 deletions activerecord_simple_oauth2.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# coding: utf-8

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'activerecord_simple_oauth2/version'

Gem::Specification.new do |s|
s.name = 'activerecord_simple_oauth2'
s.version = ActiveRecord::Simple::OAuth2.gem_version
s.date = '2017-01-17'
s.summary = 'Mixin for ActiveRecord ORM'
s.description = 'ActiveRecord mixin for SimpleOAuth2 authorization'
s.authors = ['Volodimir Partytskyi']
s.email = 'volodimir.partytskyi@gmail.com'
s.homepage = 'https://github.com/simple-oauth2/activerecord_simple_oauth2'
s.license = 'MIT'

s.require_paths = %w[lib]
s.files = Dir['LICENSE', 'lib/**/*']

s.required_ruby_version = '>= 2.2.2'

s.add_runtime_dependency 'simple_oauth2', '0.1.0'

s.add_development_dependency 'rspec-rails', '~> 3.6.0', '>= 3.6.0'
s.add_development_dependency 'database_cleaner', '~> 1.5.0', '>= 1.5.0'
end
7 changes: 7 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'activerecord_simple_oauth2'

require 'irb'
IRB.start
4 changes: 4 additions & 0 deletions config/db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OTR::ActiveRecord.configure_from_hash!(adapter: 'sqlite3', database: ':memory:')

ActiveRecord::Base.logger = nil
ActiveRecord::Migration.verbose = false
47 changes: 47 additions & 0 deletions db/migrations/20180204143133_create_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class CreateSchema < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :username, null: false, unique: true
t.string :encrypted_password, null: false

t.timestamps null: false
end

create_table :clients do |t|
t.string :name, null: false
t.string :redirect_uri, null: false
t.string :key, null: false, unique: true, index: true
t.string :secret, null: false, unique: true, index: true

t.timestamps null: false
end

create_table :access_tokens do |t|
t.integer :resource_owner_id, null: false, index: true
t.integer :client_id, null: false, index: true

t.string :token, null: false, unique: true, index: true
t.string :refresh_token, unique: true, index: true
t.string :scopes

t.datetime :revoked_at
t.datetime :expires_at, null: false

t.timestamps null: false
end

create_table :access_grants do |t|
t.integer :resource_owner_id, null: false, index: true
t.integer :client_id, null: false, index: true

t.string :token, null: false, unique: true, index: true
t.string :redirect_uri, null: false
t.string :scopes

t.datetime :revoked_at
t.datetime :expires_at, null: false

t.timestamps null: false
end
end
end

0 comments on commit 80d9aaa

Please sign in to comment.