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
defvova committed Feb 4, 2018
1 parent 43c431a commit c7e4e1d
Show file tree
Hide file tree
Showing 28 changed files with 1,149 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
9 changes: 9 additions & 0 deletions .hound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ruby:
config_file: .rubocop_todo.yml

fail_on_violations: true

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

inherit_from: .rubocop_todo.yml
12 changes: 12 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
language: ruby
cache: bundler
bundler_args: --without yard guard benchmarks
notifications:
email: false

addons:
code_climate:
repo_token: a79dc140c09279df903bea582c17d13eecb4e8d80a4d92e1e14f177c74547cc6

services:
- postgresql

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:
# - mysql -e 'CREATE DATABASE simple_oauth2_test;'
- gem install bundler -v '~> 1.10'
- bundle install
- rake db:create
- rake db:migrate
17 changes: 17 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
source 'https://rubygems.org'

gemspec

gem 'activerecord'
gem 'otr-activerecord'
gem 'pg', '0.21.0'
gem 'rubocop', '~> 0.49.0', require: false

group :test do
gem 'codeclimate-test-reporter', '~> 1.0.0'
gem 'coveralls', require: false
gem 'database_cleaner'
gem 'ffaker'
gem 'rspec-rails', '~> 3.4'
gem 'simplecov', require: false
end
137 changes: 135 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,135 @@
# 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:
```ruby
ActiveRecord::Schema.define(version: 3) do
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 :applications do |t|
t.string :name, null: false
t.string :redirect_uri, null: false
t.string :key, null: false, unique: true, index: true, default: ::Simple::OAuth2.config.token_generator.generate
t.string :secret, null: false, unique: true, index: true, default: ::Simple::OAuth2.config.token_generator.generate

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, default: ::Simple::OAuth2.config.token_generator.generate
t.string :refresh_token, unique: true, index: true, default: ::Simple::OAuth2.config.issue_refresh_token ? ::Simple::OAuth2.config.token_generator.generate : ''
t.string :scopes

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

t.timestamps null: false
end

create_table :access_grant 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, default: ::Simple::OAuth2.config.token_generator.generate
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
```

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).
17 changes: 17 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'bundler/setup'
require 'rspec/core/rake_task'
require 'simple_oauth2'
load 'tasks/otr-activerecord.rake'

OTR::ActiveRecord.configure_from_file! 'config/database.yml'
OTR::ActiveRecord.db_dir = 'db'
OTR::ActiveRecord.migrations_paths = ['db/migrations']

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

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

Bundler::GemHelper.install_tasks
25 changes: 25 additions & 0 deletions activerecord_simple_oauth2.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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'
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
19 changes: 19 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
development:
adapter: postgresql
host: localhost
port: 5432
database: activerecord_simple_oauth2_development
username:
password:
pool: 5
template: template0

test:
adapter: postgresql
host: localhost
port: 5432
database: travis_ci_test
username:
password:
pool: 5
template: template0
Loading

0 comments on commit c7e4e1d

Please sign in to comment.