cow_auth 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bfaaf0d5f8c6e90a6f6cef610a1e22ae253e9583
4
+ data.tar.gz: c5817e97f60e5dae55dd9ea28df6c821ac47bc4f
5
+ SHA512:
6
+ metadata.gz: b71afdd4e265c535ddfa8dfda4963c4df1a5db672085487448bee259995910e1b8a5fc1b995e41c225a97da81a7eb596cccf2e2d0ff0019e9a1db02e8694036f
7
+ data.tar.gz: 952dfd528ae4264b8f7dad71f33c709403bd72cf92fc7e1e3681ce7e320a6318435b4979effe3d81d9115114b9fc4e1146598749e6f2616a82cab6eb3a9dba59
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at mickey@vt.edu. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cow_auth.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Mickey Cowden
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # CowAuth
2
+
3
+ WARNING: This gem is in early development, which means you probably shouldn't use it yet for critical applications.
4
+
5
+ The main goal of this gem is to provide API authentication for Rails (or Rails-like) web applications.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cow_auth'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cow_auth
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ Example Rails model generator command:
28
+
29
+ $ bundle exec rails generate model user email:string sid:string encrypted_password:string first_name:string last_name:string sign_in_count:integer
30
+
31
+ # Modified migration; includes indexes.
32
+ class CreateUsers < ActiveRecord::Migration[5.0]
33
+ def change
34
+ create_table :users do |t|
35
+ t.string :email, null: false
36
+ t.string :sid, null: false
37
+ t.string :encrypted_password
38
+ t.string :first_name
39
+ t.string :last_name
40
+ t.integer :sign_in_count
41
+ t.timestamps
42
+ end
43
+ add_index :users, :email, unique: true
44
+ add_index :users, :sid, unique: true
45
+ end
46
+ end
47
+
48
+ ### Controllers
49
+
50
+ Add the following lines in the controller(s) that you want to enforce authenticatication for.
51
+
52
+ include CowAuth::Authentication
53
+ before_action :authenticate_user
54
+
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ bundle exec gem build cow_auth.gemspec
63
+ bundle exec gem install cow_auth-0.1.0.gem
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mickey13/cow_auth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
73
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cow_auth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cow_auth.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cow_auth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cow_auth'
8
+ spec.version = CowAuth::VERSION
9
+ spec.authors = ['Mickey Cowden']
10
+ spec.email = ['mickey@vt.edu']
11
+
12
+ spec.summary = 'Summary'
13
+ spec.description = 'Description'
14
+ spec.homepage = 'https://github.com/mickey13/cow_auth'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'minitest', '~> 5.0'
25
+ # spec.add_runtime_dependency 'activesupport', '5.0.0.beta3'
26
+ spec.add_runtime_dependency 'scrypt', '~> 2.1'
27
+ # spec.add_runtime_dependency 'redis', '~> 3.2'
28
+ end
@@ -0,0 +1,21 @@
1
+ module CowAuth
2
+ module Authentication
3
+ extend ActiveSupport::Concern
4
+ include ActionController::HttpAuthentication::Token::ControllerMethods
5
+
6
+ private
7
+
8
+ def authenticate_user
9
+ authenticate_or_request_with_http_token do |api_key, options|
10
+ sid, auth_token = api_key.match(/sid=([[:alnum:]]*)&auth_token=([[:alnum:]]*)/).try(:captures)
11
+ @current_user = User.authenticate_from_token(sid, auth_token)
12
+ end
13
+ end
14
+
15
+ def request_http_token_authentication(realm = 'Application', message = nil)
16
+ message ||= 'HTTP Token: Access denied.'
17
+ self.headers['WWW-Authenticate'] = %(Token realm="#{realm.tr('"'.freeze, ''.freeze)}")
18
+ self.__send__ :render, json: { error: message }, status: :unauthorized
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module CowAuth
2
+ module Session
3
+ extend ActiveSupport::Concern
4
+
5
+ def create
6
+ user = User.find_by(email: params[:email])
7
+ if user.try(:authenticate, params[:password])
8
+ user.api_sign_in
9
+ render json: user
10
+ else
11
+ render json: { error: 'Invalid user credentials.' }, status: :unauthorized
12
+ end
13
+ end
14
+
15
+ def destroy
16
+ if @current_user.try(:api_sign_out)
17
+ head :ok
18
+ else
19
+ render json: { error: 'Could not sign user out.' }, status: :unauthorized
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,91 @@
1
+ require 'scrypt'
2
+
3
+ module CowAuth
4
+ class User < ActiveRecord::Base
5
+
6
+ after_initialize :generate_sid_if_necessary
7
+
8
+ validates :email, presence: true
9
+ validates :email, uniqueness: true
10
+ validates :encrypted_password, presence: true
11
+ validates :sid, presence: true
12
+ validates :sid, uniqueness: true
13
+ validates :sid, format: { with: /\AC[a-z0-9]{32}\z/ }
14
+
15
+ def password=(new_password)
16
+ return false if new_password.blank?
17
+ salt = SCrypt::Engine.generate_salt
18
+ self.encrypted_password = SCrypt::Engine.hash_secret(new_password, salt)
19
+ return true
20
+ end
21
+
22
+ def authenticate(password)
23
+ return false if self.encrypted_password.blank?
24
+ if SCrypt::Password.new(self.encrypted_password) == password
25
+ self.update(sign_in_count: self.sign_in_count + 1)
26
+ return true
27
+ end
28
+ return false
29
+ end
30
+
31
+ def api_sign_in
32
+ $redis.set(self.redis_key, {
33
+ auth_token: User.generate_random_hex_string,
34
+ expires_at: User.generate_token_expires_at
35
+ }.to_json)
36
+ end
37
+
38
+ def api_sign_out
39
+ $redis.del(self.redis_key)
40
+ end
41
+
42
+ def self.authenticate_from_token(sid, auth_token)
43
+ api_key = User.fetch_api_key_from_redis(sid)
44
+ if api_key.present? && api_key.key?(:auth_token) && api_key.key?(:expires_at) && api_key[:auth_token] == auth_token && api_key[:expires_at] > Time.zone.now
45
+ return User.find_by(sid: sid)
46
+ end
47
+ return nil
48
+ end
49
+
50
+ def as_json(options = nil)
51
+ return {
52
+ uuid: self.uuid,
53
+ email: self.email,
54
+ sid: self.sid,
55
+ auth_token: User.fetch_api_key_from_redis(self.sid).try(:[], :auth_token),
56
+ first_name: self.first_name,
57
+ last_name: self.last_name,
58
+ sign_in_count: self.sign_in_count,
59
+ is_approved: self.is_approved,
60
+ created_at: self.created_at,
61
+ updated_at: self.updated_at
62
+ }
63
+ end
64
+
65
+ protected
66
+
67
+ def redis_key
68
+ return "user_#{self.sid}"
69
+ end
70
+
71
+ private
72
+
73
+ def generate_sid_if_necessary
74
+ self.sid ||= "C#{SecureRandom.hex(16)}"
75
+ return true
76
+ end
77
+
78
+ def self.generate_random_hex_string
79
+ return SecureRandom.hex(16)
80
+ end
81
+
82
+ def self.generate_token_expires_at
83
+ return 1.month.from_now
84
+ end
85
+
86
+ def self.fetch_api_key_from_redis(sid)
87
+ api_key = $redis.get("user_#{sid}")
88
+ return api_key.present? ? JSON.parse(api_key).try(:symbolize_keys) : nil
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module CowAuth
2
+ VERSION = '0.1.1'
3
+ end
data/lib/cow_auth.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'cow_auth/version'
2
+ require 'cow_auth/user'
3
+ require 'cow_auth/session'
4
+ require 'cow_auth/authentication'
5
+
6
+ module CowAuth
7
+ def self.hi
8
+ user = CowAuth::User.new
9
+ puts user
10
+ return user
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cow_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mickey Cowden
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: scrypt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ description: Description
70
+ email:
71
+ - mickey@vt.edu
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - cow_auth.gemspec
86
+ - lib/cow_auth.rb
87
+ - lib/cow_auth/authentication.rb
88
+ - lib/cow_auth/session.rb
89
+ - lib/cow_auth/user.rb
90
+ - lib/cow_auth/version.rb
91
+ homepage: https://github.com/mickey13/cow_auth
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Summary
115
+ test_files: []