acts_as_hoc_user 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +2 -0
- data/acts_as_hoc_user.gemspec +36 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/acts_as_hoc_user/acts_as_hoc_user.rb +41 -0
- data/lib/acts_as_hoc_user/configuration.rb +7 -0
- data/lib/acts_as_hoc_user/json_web_token.rb +18 -0
- data/lib/acts_as_hoc_user/version.rb +3 -0
- data/lib/acts_as_hoc_user.rb +22 -0
- data/lib/generators/acts_as_hoc_user/hoc_user_generator.rb +58 -0
- data/lib/generators/acts_as_hoc_user/templates/create_hoc_user.rb.erb +16 -0
- data/lib/generators/acts_as_hoc_user/templates/hoc_user.rb.erb +3 -0
- data/lib/generators/acts_as_hoc_user/templates/initializer.rb +3 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d53ee526d2e91cf1a297ef7d42307206429c802e
|
4
|
+
data.tar.gz: 9b18bc0c028eeb444b5aa8d2a4e4d15ddf5dfa64
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0bd7ecd06bfdd8d25523fa88d1fddcba8136e2cc2e55b09ec7738bffc9b3440325bb65202f5f279c9076273e0093fb865bf73d2bc4aadf8089c7861f28362192
|
7
|
+
data.tar.gz: 3a5ec7aefe40cab070b884898548073c7e2d80a163a7fceaba366d9c14408f108f545aad40598c7e0a919f69a14bdc80acefb9efb4e66828fd2a788b0ab93ae9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Gert Lavsen
|
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,94 @@
|
|
1
|
+
# ActsAsHocUser
|
2
|
+
|
3
|
+
`acts_as_hoc_user` makes it easy to authenticate users.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'acts_as_hoc_user'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install acts_as_hoc_user
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Generate model
|
25
|
+
You can create the model and table migration with the following generator:
|
26
|
+
```bash
|
27
|
+
$ rails generate acts_as_hoc_user:hoc_user NAME FIELDS
|
28
|
+
```
|
29
|
+
|
30
|
+
Eg.
|
31
|
+
```bash
|
32
|
+
$ rails generate acts_as_hoc_user:hoc_user user name:string age:integer phone_number:string address:string zip:string
|
33
|
+
```
|
34
|
+
Which will generate the following migration:
|
35
|
+
```ruby
|
36
|
+
#db/migration/xxxxxxxxxxxx_create_users.rb
|
37
|
+
class CreateUsers < ActiveRecord::Migration[5.0]
|
38
|
+
def self.up
|
39
|
+
create_table :users do |t|
|
40
|
+
t.string :email, index: {unique: true}, null: false
|
41
|
+
t.string :password_digest
|
42
|
+
t.string :name
|
43
|
+
t.integer :age
|
44
|
+
t.string :phone_number
|
45
|
+
t.string :address
|
46
|
+
t.string :zip
|
47
|
+
t.timestamps
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.down
|
52
|
+
drop_table :users
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
and model
|
57
|
+
```ruby
|
58
|
+
#app/model/user.rb
|
59
|
+
class User < ActiveRecord::Base
|
60
|
+
acts_as_hoc_user
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
and initializer
|
65
|
+
```ruby
|
66
|
+
#config/initializers/acts_as_hoc_user.rb
|
67
|
+
ActsAsHocUser.configure do |config|
|
68
|
+
config.min_password_length = 6
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### Manual usage
|
73
|
+
If you prefer you can create the model yourself. Just make sure that the model has email:string and password_digest:string fields and add `acts_as_hoc_user` to the model
|
74
|
+
|
75
|
+
### Authenticate user
|
76
|
+
|
77
|
+
#### Authenticate and get JWT token
|
78
|
+
```ruby
|
79
|
+
auth_token = User.authenticate_with_credentials("email@test.com","s3cr3t")
|
80
|
+
```
|
81
|
+
|
82
|
+
#### Authenticate with JWT token
|
83
|
+
```ruby
|
84
|
+
user = User.authenticate_with_authentication_token(auth_token)
|
85
|
+
```
|
86
|
+
|
87
|
+
#### Authenticate with http headers
|
88
|
+
```ruby
|
89
|
+
user = User.authenticate_with_headers(request.headers)
|
90
|
+
```
|
91
|
+
|
92
|
+
|
93
|
+
## Licence
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "acts_as_hoc_user/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "acts_as_hoc_user"
|
8
|
+
spec.version = ActsAsHocUser::VERSION
|
9
|
+
spec.authors = ["Gert Lavsen"]
|
10
|
+
spec.email = ["gert@houseofcode.io"]
|
11
|
+
|
12
|
+
spec.summary = "Easy authentication with JWT"
|
13
|
+
spec.description = "This gem makes it easy to authenticate a user by email and password"
|
14
|
+
spec.homepage = "https://github.com/house-of-code/acts_as_hoc_user"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
spec.add_dependency "bcrypt", "~> 3.1.7"
|
33
|
+
spec.add_dependency "jwt"
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16.a"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "acts_as_hoc_user"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'json_web_token'
|
2
|
+
|
3
|
+
module ActsAsHocUser
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
end
|
7
|
+
|
8
|
+
def authentication_token(expiration = 14.days.from_now)
|
9
|
+
JsonWebToken.encode({ user_id: id }, expiration)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def authenticate_with_credentials(email, password, expiration = 14.days.from_now)
|
14
|
+
user = find_by(email:email)
|
15
|
+
return user.authentication_token(expiration) if user && user.authenticate(password)
|
16
|
+
nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def authenticate_with_authentication_token(token)
|
20
|
+
decoded_auth_token = JsonWebToken.decode(token)
|
21
|
+
return nil if decoded_auth_token.nil?
|
22
|
+
user = User.find(decoded_auth_token[:user_id])
|
23
|
+
return user
|
24
|
+
end
|
25
|
+
|
26
|
+
def authenticate_with_http_headers(headers = {})
|
27
|
+
if headers['Authorization'].present?
|
28
|
+
return authenticate_with_authentication_token(headers['Authorization'].split(' ').last)
|
29
|
+
end
|
30
|
+
return nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def acts_as_hoc_user(_options = {})
|
34
|
+
has_secure_password
|
35
|
+
validates_presence_of :email
|
36
|
+
validates :email, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
37
|
+
validates :password, length: { minimum: ActsAsHocUser.configuration.min_password_length }, if: -> { password.present? }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
ActiveRecord::Base.send :include, ActsAsHocUser
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
module ActsAsHocUser
|
3
|
+
class JsonWebToken
|
4
|
+
class << self
|
5
|
+
def encode(payload, exp = 48.hours.from_now)
|
6
|
+
payload[:exp] = exp.to_i
|
7
|
+
JWT.encode(payload, Rails.application.secrets.secret_key_base)
|
8
|
+
end
|
9
|
+
|
10
|
+
def decode(token)
|
11
|
+
body = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
|
12
|
+
HashWithIndifferentAccess.new body
|
13
|
+
rescue
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "acts_as_hoc_user/version"
|
2
|
+
require "acts_as_hoc_user/configuration"
|
3
|
+
require "acts_as_hoc_user/acts_as_hoc_user"
|
4
|
+
module ActsAsHocUser
|
5
|
+
LOCK = Mutex.new
|
6
|
+
class << self
|
7
|
+
def configure(config_hash=nil)
|
8
|
+
if config_hash
|
9
|
+
config_hash.each do |k,v|
|
10
|
+
configuration.send("#{k}=", v) rescue nil if configuration.respond_to?("#{k}=")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
yield(configuration) if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
def configuration
|
18
|
+
@configuration = nil unless defined?(@configuration)
|
19
|
+
@configuration || LOCK.synchronize { @configuration ||= ActsAsHocUser::Configuration.new }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
module ActsAsHocUser
|
3
|
+
#module Generators
|
4
|
+
class HocUserGenerator < ActiveRecord::Generators::Base
|
5
|
+
|
6
|
+
desc "Create a HocUser model and migrations " +
|
7
|
+
"The NAME argument is the name of your model, and the following " +
|
8
|
+
"arguments are the fields to add. Eg. acts_as_hoc_user:hoc_user user name:string"
|
9
|
+
|
10
|
+
argument :fields, :required => false, :type => :array, :desc => "The fields to add.",
|
11
|
+
:banner => "name:string age:integer ..."
|
12
|
+
|
13
|
+
def self.source_root
|
14
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
def generate_migration
|
19
|
+
template "initializer.rb", "config/initializers/acts_as_hoc_user.rb"
|
20
|
+
template "hoc_user.rb.erb", "app/models/#{model_name}.rb"
|
21
|
+
migration_template("create_hoc_user.rb.erb",
|
22
|
+
"db/migrate/#{migration_file_name}",
|
23
|
+
migration_version: migration_version)
|
24
|
+
end
|
25
|
+
|
26
|
+
def model_name
|
27
|
+
name.underscore
|
28
|
+
end
|
29
|
+
|
30
|
+
def migration_colums
|
31
|
+
return fields.map { |field| "t.#{field.split(":").last} :#{field.split(":").first}" } unless fields.nil?
|
32
|
+
return []
|
33
|
+
end
|
34
|
+
|
35
|
+
def model_class_name
|
36
|
+
name.camelize
|
37
|
+
end
|
38
|
+
|
39
|
+
def migration_name
|
40
|
+
"create_#{name.underscore.pluralize}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def migration_file_name
|
44
|
+
"#{migration_name}.rb"
|
45
|
+
end
|
46
|
+
|
47
|
+
def migration_class_name
|
48
|
+
migration_name.camelize
|
49
|
+
end
|
50
|
+
|
51
|
+
def migration_version
|
52
|
+
if Rails.version.start_with? "5"
|
53
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
#end
|
58
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
|
2
|
+
def self.up
|
3
|
+
create_table :<%= table_name %> do |t|
|
4
|
+
t.string :email, index: {unique: true}, null: false
|
5
|
+
t.string :password_digest
|
6
|
+
<% migration_colums.each do |column| -%>
|
7
|
+
<%= column %>
|
8
|
+
<% end -%>
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :<%= table_name %>
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_hoc_user
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gert Lavsen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bcrypt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.7
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.7
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jwt
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.16.a
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.16.a
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: This gem makes it easy to authenticate a user by email and password
|
70
|
+
email:
|
71
|
+
- gert@houseofcode.io
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- acts_as_hoc_user.gemspec
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/acts_as_hoc_user.rb
|
85
|
+
- lib/acts_as_hoc_user/acts_as_hoc_user.rb
|
86
|
+
- lib/acts_as_hoc_user/configuration.rb
|
87
|
+
- lib/acts_as_hoc_user/json_web_token.rb
|
88
|
+
- lib/acts_as_hoc_user/version.rb
|
89
|
+
- lib/generators/acts_as_hoc_user/hoc_user_generator.rb
|
90
|
+
- lib/generators/acts_as_hoc_user/templates/create_hoc_user.rb.erb
|
91
|
+
- lib/generators/acts_as_hoc_user/templates/hoc_user.rb.erb
|
92
|
+
- lib/generators/acts_as_hoc_user/templates/initializer.rb
|
93
|
+
homepage: https://github.com/house-of-code/acts_as_hoc_user
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.6.13
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Easy authentication with JWT
|
117
|
+
test_files: []
|