lockie 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +30 -0
- data/Rakefile +27 -0
- data/lib/lockie.rb +37 -0
- data/lib/lockie/failure_app.rb +56 -0
- data/lib/lockie/log_helper.rb +8 -0
- data/lib/lockie/model_helper.rb +34 -0
- data/lib/lockie/rails.rb +18 -0
- data/lib/lockie/strategies/email_password.rb +29 -0
- data/lib/lockie/strategies/failed.rb +17 -0
- data/lib/lockie/strategies/jwt.rb +43 -0
- data/lib/lockie/version.rb +3 -0
- data/lib/tasks/lockie_tasks.rake +4 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fc55c292f0425ca3158d054001d53bd61f12e99c5dc43f8d2fc57262cfd367f2
|
4
|
+
data.tar.gz: 7e4e9b6c6df39bf3070617119ff6e191a5e3519228b6693526bfbb8a6ea0bc53
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d5272797247572c2d7455b0be3556cfe7297691803d181c5d0cc77874642f59af78a884d5836f73d52a84734687c24ad34d49396e1185d41befc39d07c9f539
|
7
|
+
data.tar.gz: e3d304b82778ed8762c39873dedfb530eeee1d7732db2156562bfb949efbfa23880f8667d6d5fb7862351453cb721a51fe70680ecd867c9e1c29f29a9b6c114d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Melvin Sembrano
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/melvinsembrano/lockie.svg?branch=master)](https://travis-ci.org/melvinsembrano/lockie)
|
2
|
+
|
3
|
+
# Lockie
|
4
|
+
Short description and motivation.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
How to use my plugin.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'lockie'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
```bash
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
```bash
|
23
|
+
$ gem install lockie
|
24
|
+
```
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
Contribution directions go here.
|
28
|
+
|
29
|
+
## License
|
30
|
+
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,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Lockie'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'bundler/gem_tasks'
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'test'
|
23
|
+
t.pattern = 'test/**/*_test.rb'
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
27
|
+
task default: :test
|
data/lib/lockie.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'warden'
|
3
|
+
require_relative "lockie/rails"
|
4
|
+
require_relative "lockie/log_helper"
|
5
|
+
require_relative "lockie/model_helper"
|
6
|
+
require_relative "lockie/strategies/email_password"
|
7
|
+
require_relative "lockie/strategies/jwt"
|
8
|
+
require_relative "lockie/strategies/failed"
|
9
|
+
|
10
|
+
module Lockie
|
11
|
+
autoload :FailureApp, 'lockie/failure_app'
|
12
|
+
|
13
|
+
class Configuration
|
14
|
+
attr_accessor :model_name
|
15
|
+
attr_accessor :unauthenticated_path
|
16
|
+
attr_accessor :default_strategies
|
17
|
+
attr_accessor :jwt_secret
|
18
|
+
attr_accessor :hash_algorithm
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@model_name = "User"
|
22
|
+
@unauthenticated_path = "/login"
|
23
|
+
@default_strategies = [:email_passwrd, :jwt]
|
24
|
+
@hash_algorithm = "HS256"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class << self
|
29
|
+
def configure
|
30
|
+
yield config
|
31
|
+
end
|
32
|
+
|
33
|
+
def config
|
34
|
+
@config ||= Configuration.new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'action_controller/metal'
|
2
|
+
module Lockie
|
3
|
+
class FailureApp < ActionController::Metal
|
4
|
+
include ActionController::Redirecting
|
5
|
+
|
6
|
+
delegate :flash, to: :request
|
7
|
+
|
8
|
+
def self.call(*args)
|
9
|
+
req = ActionDispatch::Request.new(*args)
|
10
|
+
action(req.env['warden.options'][:action]).call(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
def unauthenticated
|
14
|
+
if request.xhr?
|
15
|
+
api_response(:text)
|
16
|
+
elsif request.format.to_sym == :json || request.content_type.to_s.split("/").last == 'json'
|
17
|
+
api_response(:json)
|
18
|
+
else
|
19
|
+
html_response
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def api_response(format = :text)
|
24
|
+
self.status = 401
|
25
|
+
self.content_type = request.format.to_s
|
26
|
+
if format.eql?(:text)
|
27
|
+
self.response_body = message
|
28
|
+
else
|
29
|
+
self.response_body = { message: message }.to_json
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def html_response
|
34
|
+
flash[type] = message if message
|
35
|
+
self.status = 302
|
36
|
+
redirect_to Lockie.config.unauthenticated_path
|
37
|
+
end
|
38
|
+
|
39
|
+
def message
|
40
|
+
@message ||= request.env['warden.message']
|
41
|
+
end
|
42
|
+
|
43
|
+
def warden_options
|
44
|
+
request.env['warden.options']
|
45
|
+
end
|
46
|
+
|
47
|
+
def type
|
48
|
+
@type ||= warden_options[:type] || :error
|
49
|
+
end
|
50
|
+
|
51
|
+
def warden
|
52
|
+
env['warden']
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
|
3
|
+
module Lockie
|
4
|
+
module ModelHelper
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def auth_object
|
8
|
+
@auth_object ||= Lockie.config.model_name.classify.constantize
|
9
|
+
end
|
10
|
+
|
11
|
+
included do
|
12
|
+
|
13
|
+
def create_token(payload = {})
|
14
|
+
payload = {
|
15
|
+
aud: 'lockie-app',
|
16
|
+
sub: id,
|
17
|
+
sub_type: self.class.name,
|
18
|
+
}.merge(payload)
|
19
|
+
|
20
|
+
JWT.encode(payload, Lockie.config.jwt_secret, Lockie.config.hash_algorithm)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class_methods do
|
25
|
+
|
26
|
+
def find_by_token(token)
|
27
|
+
payload = JWT.decode(token, Lockie.config.jwt_secret, true, { algorithm: Lockie.config.hash_algorithm })
|
28
|
+
auth_id = payload.first.fetch('sub') { nil }
|
29
|
+
find(auth_id)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/lockie/rails.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'lockie/model_helper'
|
2
|
+
|
3
|
+
module Lockie
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
include Lockie::ModelHelper
|
6
|
+
|
7
|
+
config.app_middleware.use Warden::Manager do |manager|
|
8
|
+
manager.default_strategies Lockie.config.default_strategies
|
9
|
+
manager.failure_app = Lockie::FailureApp
|
10
|
+
|
11
|
+
# manager.serialize_into_session(&:id)
|
12
|
+
# manager.serialize_from_session do |id|
|
13
|
+
# auth_object.find(id)
|
14
|
+
# end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Lockie
|
2
|
+
module Strategies
|
3
|
+
class EmailPassword < ::Warden::Strategies::Base
|
4
|
+
include Lockie::ModelHelper
|
5
|
+
include Lockie::LogHelper
|
6
|
+
|
7
|
+
def request
|
8
|
+
@request ||= ActionDispatch::Request.new(env)
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
request.params['email'].present? && request.params['password'].present?
|
13
|
+
end
|
14
|
+
|
15
|
+
def authenticate!
|
16
|
+
auth = auth_object.find_by_email(request.params['email'])
|
17
|
+
|
18
|
+
if auth && auth.authenticate(request.params['password'])
|
19
|
+
success!(auth)
|
20
|
+
else
|
21
|
+
set_message('Invalid username or password')
|
22
|
+
fail!
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
Warden::Strategies.add(:email_password, Lockie::Strategies::EmailPassword)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Lockie
|
2
|
+
module Strategies
|
3
|
+
class Failed < ::Warden::Strategies::Base
|
4
|
+
include Lockie::LogHelper
|
5
|
+
|
6
|
+
def valid?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
def authenticate!
|
11
|
+
set_message 'Unauthorised'
|
12
|
+
fail!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
Warden::Strategies.add(:failed, Lockie::Strategies::Failed)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Lockie
|
2
|
+
module Strategies
|
3
|
+
class Jwt < ::Warden::Strategies::Base
|
4
|
+
include Lockie::ModelHelper
|
5
|
+
include Lockie::LogHelper
|
6
|
+
|
7
|
+
def auth
|
8
|
+
@auth ||= ActionDispatch::Request.new(env)
|
9
|
+
end
|
10
|
+
|
11
|
+
def headers
|
12
|
+
auth.headers
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
headers['Authorization'].present?
|
17
|
+
end
|
18
|
+
|
19
|
+
def token
|
20
|
+
headers['Authorization'].split.last.strip
|
21
|
+
end
|
22
|
+
|
23
|
+
def authenticate!
|
24
|
+
begin
|
25
|
+
auth = auth_object.find_by_token(token)
|
26
|
+
if auth
|
27
|
+
success! auth
|
28
|
+
else
|
29
|
+
set_message "Invalid token"
|
30
|
+
fail!
|
31
|
+
end
|
32
|
+
rescue # => err
|
33
|
+
set_message "Invalid token"
|
34
|
+
fail!
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Warden::Strategies.add(:jwt, Lockie::Strategies::Jwt)
|
43
|
+
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lockie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Melvin Sembrano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: warden
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: jwt
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.3.6
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.3.6
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bcrypt
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.1.7
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.1.7
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.12'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.12'
|
97
|
+
description: Drop in password and JWT token authentication for Ruby on Rails
|
98
|
+
email:
|
99
|
+
- melvinsembrano@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- MIT-LICENSE
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- lib/lockie.rb
|
108
|
+
- lib/lockie/failure_app.rb
|
109
|
+
- lib/lockie/log_helper.rb
|
110
|
+
- lib/lockie/model_helper.rb
|
111
|
+
- lib/lockie/rails.rb
|
112
|
+
- lib/lockie/strategies/email_password.rb
|
113
|
+
- lib/lockie/strategies/failed.rb
|
114
|
+
- lib/lockie/strategies/jwt.rb
|
115
|
+
- lib/lockie/version.rb
|
116
|
+
- lib/tasks/lockie_tasks.rake
|
117
|
+
homepage: https://github.com/melvinsembrano/lockie
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata:
|
121
|
+
allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubygems_version: 3.0.2
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Drop in password and JWT token authentication for Ruby on Rails
|
141
|
+
test_files: []
|