cow_auth 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -8
- data/cow_auth.gemspec +18 -17
- data/lib/cow_auth/{not_authenticated_error.rb → exceptions.rb} +3 -0
- data/lib/cow_auth/session_auth/authenticate_request.rb +1 -1
- data/lib/cow_auth/session_auth/session_endpoints.rb +1 -1
- data/lib/cow_auth/token_auth/authenticate_request.rb +1 -1
- data/lib/cow_auth/token_auth/session_endpoints.rb +3 -1
- data/lib/cow_auth/user.rb +12 -2
- data/lib/cow_auth/user_serializer.rb +7 -0
- data/lib/cow_auth/version.rb +1 -1
- data/lib/cow_auth.rb +1 -1
- metadata +32 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9b457d5e613359cad57b0ef426979f534089cc8
|
4
|
+
data.tar.gz: b11a6ac0f2852dbcf3b5215df6147c9133618d75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 780843129e5e37b30fc2ece5185392bd431ad5d9e48aaa761823922de61e5d15b16dd649accc845e2430d633c54272a8f8e746e56789c7a84c6c570243dc1b4f
|
7
|
+
data.tar.gz: 0c70f12f43f00e3d5dd1d1828f2212f8033f0ca98ac72a5c6b95c6249b2bab4ff488de87092353090bfe89fc4523d92118cc8fec9cf6533646cf4da42372e645
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# CowAuth
|
2
2
|
|
3
|
-
The main goal of this gem is to provide API authentication for Rails (or Rails-like) web applications.
|
3
|
+
The main goal of this gem is to provide session and / or API authentication for Rails (or Rails-like) web applications.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,12 +20,14 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Model
|
22
22
|
|
23
|
-
|
23
|
+
### Generator (Example)
|
24
24
|
|
25
|
-
$ bundle exec rails generate model user email:string sid:string encrypted_password:string first_name:string last_name:string sign_in_count:integer
|
25
|
+
$ bundle exec rails generate model user uuid:string:uniq email:string:uniq sid:string:uniq encrypted_password:string first_name:string last_name:string sign_in_count:integer is_approved:boolean is_deleted:boolean
|
26
|
+
|
27
|
+
### Migration (Example)
|
26
28
|
|
27
29
|
# Modified migration; includes indexes and other stuff you might not want.
|
28
|
-
class CreateUsers < ActiveRecord::Migration[5.
|
30
|
+
class CreateUsers < ActiveRecord::Migration[5.1]
|
29
31
|
def change
|
30
32
|
create_table :users do |t|
|
31
33
|
t.string :uuid, null: false
|
@@ -45,6 +47,12 @@ Example Rails model generator command:
|
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
50
|
+
### Model Inheritance
|
51
|
+
|
52
|
+
class User < CowAuth::User
|
53
|
+
end
|
54
|
+
|
55
|
+
|
48
56
|
### Create User
|
49
57
|
|
50
58
|
User.create! email: 'email', password: 'password'
|
@@ -112,13 +120,19 @@ Add the following lines in the controller(s) that you want to enforce authentica
|
|
112
120
|
|
113
121
|
## Token Authentication
|
114
122
|
|
123
|
+
### Authenticate (Example)
|
124
|
+
|
125
|
+
curl -X POST -i --data-urlencode email=user@domain.tld --data-urlencode password=password https://api.domain.tld/v1/sessions
|
126
|
+
|
127
|
+
curl -X DELETE -i https://api.domain.tld/v1/sessions -H "Authorization: Token token=b5503c9b85b881f8b3ddbd82f511912cb5503c9b85b881f8b3ddbd82f511912c,sid=C3281846f3976809796f91cf6bbb35c53"
|
128
|
+
|
115
129
|
### Authenticated Request
|
116
130
|
|
117
131
|
Note that token and sid are both required.
|
118
132
|
|
119
133
|
Example GET:
|
120
134
|
|
121
|
-
curl -X GET
|
135
|
+
curl -X GET -i https://api.domain.tld/v1/test -H "Authorization: Token token=b5503c9b85b881f8b3ddbd82f511912cb5503c9b85b881f8b3ddbd82f511912c,sid=C3281846f3976809796f91cf6bbb35c53"
|
122
136
|
|
123
137
|
### Controllers
|
124
138
|
|
@@ -139,8 +153,7 @@ Add the following lines in the controller(s) that you want to enforce authentica
|
|
139
153
|
private
|
140
154
|
|
141
155
|
def user_not_authenticated(exception)
|
142
|
-
|
143
|
-
render 'errors/unauthorized', status: :unauthorized
|
156
|
+
render json: { error: exception.message }, status: :unauthorized
|
144
157
|
end
|
145
158
|
end
|
146
159
|
|
@@ -159,7 +172,13 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
159
172
|
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).
|
160
173
|
|
161
174
|
bundle exec gem build cow_auth.gemspec
|
162
|
-
bundle exec gem install cow_auth-0.
|
175
|
+
bundle exec gem install cow_auth-0.1.0.gem
|
176
|
+
|
177
|
+
### Notes
|
178
|
+
|
179
|
+
cow_auth> bundle exec gem build cow_auth.gemspec
|
180
|
+
|
181
|
+
app> bundle
|
163
182
|
|
164
183
|
## Contributing
|
165
184
|
|
data/cow_auth.gemspec
CHANGED
@@ -4,25 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'cow_auth/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
7
|
+
spec.name = 'cow_auth'
|
8
|
+
spec.version = CowAuth::VERSION
|
9
|
+
spec.authors = ['Mickey Cowden']
|
10
|
+
spec.email = ['mickey@vt.edu']
|
11
11
|
|
12
|
-
spec.summary
|
13
|
-
spec.description
|
14
|
-
spec.homepage
|
15
|
-
spec.license
|
12
|
+
spec.summary = 'Authentication gem'
|
13
|
+
spec.description = 'The main goal of this gem is to provide session and / or API authentication for Rails (or Rails-like) web applications.'
|
14
|
+
spec.homepage = 'https://github.com/mickey13/cow_auth'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
|
-
spec.files
|
18
|
-
spec.bindir
|
19
|
-
spec.executables
|
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
20
|
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.
|
23
|
-
|
24
|
-
spec.add_development_dependency '
|
25
|
-
|
26
|
-
spec.
|
27
|
-
|
22
|
+
spec.required_ruby_version = '~> 2.3'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
25
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
26
|
+
spec.add_development_dependency 'minitest', '~> 5.10'
|
27
|
+
spec.add_runtime_dependency 'active_model_serializers', '~> 0.10'
|
28
|
+
spec.add_runtime_dependency 'scrypt', '~> 3.0'
|
28
29
|
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
require 'cow_auth/
|
1
|
+
require 'cow_auth/user_serializer'
|
2
|
+
require 'cow_auth/exceptions'
|
2
3
|
|
3
4
|
module CowAuth
|
4
5
|
module TokenAuth
|
@@ -9,6 +10,7 @@ module CowAuth
|
|
9
10
|
@user = User.find_by(email: params[:email])
|
10
11
|
if @user.try(:authenticate, params[:password])
|
11
12
|
@user.api_sign_in
|
13
|
+
render json: UserSerializer.new(@user), status: :ok
|
12
14
|
else
|
13
15
|
raise CowAuth::NotAuthenticatedError.new('Invalid user credentials.')
|
14
16
|
end
|
data/lib/cow_auth/user.rb
CHANGED
@@ -2,7 +2,6 @@ require 'scrypt'
|
|
2
2
|
|
3
3
|
module CowAuth
|
4
4
|
class User < ActiveRecord::Base
|
5
|
-
|
6
5
|
after_initialize :generate_sid_if_necessary
|
7
6
|
|
8
7
|
validates :email, presence: true
|
@@ -29,6 +28,7 @@ module CowAuth
|
|
29
28
|
end
|
30
29
|
|
31
30
|
def api_sign_in
|
31
|
+
User.assert_redis_handle_present
|
32
32
|
$redis.set(self.redis_key, {
|
33
33
|
auth_token: User.generate_auth_token,
|
34
34
|
expires_at: User.generate_token_expires_at
|
@@ -36,6 +36,7 @@ module CowAuth
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def api_sign_out
|
39
|
+
User.assert_redis_handle_present
|
39
40
|
$redis.del(self.redis_key)
|
40
41
|
end
|
41
42
|
|
@@ -45,7 +46,11 @@ module CowAuth
|
|
45
46
|
|
46
47
|
def self.authenticate_from_token(sid, auth_token)
|
47
48
|
api_key = User.fetch_api_key_from_redis(sid)
|
48
|
-
if api_key.present? &&
|
49
|
+
if api_key.present? &&
|
50
|
+
api_key.key?(:auth_token) &&
|
51
|
+
api_key.key?(:expires_at) &&
|
52
|
+
api_key[:auth_token] == auth_token &&
|
53
|
+
api_key[:expires_at] > Time.zone.now
|
49
54
|
return User.find_by(sid: sid)
|
50
55
|
end
|
51
56
|
return nil
|
@@ -73,8 +78,13 @@ module CowAuth
|
|
73
78
|
end
|
74
79
|
|
75
80
|
def self.fetch_api_key_from_redis(sid)
|
81
|
+
User.assert_redis_handle_present
|
76
82
|
api_key = $redis.get("user_#{sid}")
|
77
83
|
return api_key.present? ? JSON.parse(api_key).try(:symbolize_keys) : nil
|
78
84
|
end
|
85
|
+
|
86
|
+
def self.assert_redis_handle_present
|
87
|
+
raise CowAuth::RedisHandleMissingError.new('"$redis" handle not found.') unless $redis.present?
|
88
|
+
end
|
79
89
|
end
|
80
90
|
end
|
data/lib/cow_auth/version.rb
CHANGED
data/lib/cow_auth.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'cow_auth/version'
|
2
2
|
require 'cow_auth/user'
|
3
|
-
require 'cow_auth/
|
3
|
+
require 'cow_auth/exceptions'
|
4
4
|
require 'cow_auth/session_auth/session_endpoints'
|
5
5
|
require 'cow_auth/session_auth/authenticate_request'
|
6
6
|
require 'cow_auth/token_auth/session_endpoints'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cow_auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mickey Cowden
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,57 +16,72 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.14'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.14'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '12.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '12.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '5.
|
47
|
+
version: '5.10'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '5.
|
54
|
+
version: '5.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: active_model_serializers
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.10'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.10'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: scrypt
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '3.0'
|
62
76
|
type: :runtime
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
69
|
-
description:
|
82
|
+
version: '3.0'
|
83
|
+
description: The main goal of this gem is to provide session and / or API authentication
|
84
|
+
for Rails (or Rails-like) web applications.
|
70
85
|
email:
|
71
86
|
- mickey@vt.edu
|
72
87
|
executables: []
|
@@ -84,12 +99,13 @@ files:
|
|
84
99
|
- bin/setup
|
85
100
|
- cow_auth.gemspec
|
86
101
|
- lib/cow_auth.rb
|
87
|
-
- lib/cow_auth/
|
102
|
+
- lib/cow_auth/exceptions.rb
|
88
103
|
- lib/cow_auth/session_auth/authenticate_request.rb
|
89
104
|
- lib/cow_auth/session_auth/session_endpoints.rb
|
90
105
|
- lib/cow_auth/token_auth/authenticate_request.rb
|
91
106
|
- lib/cow_auth/token_auth/session_endpoints.rb
|
92
107
|
- lib/cow_auth/user.rb
|
108
|
+
- lib/cow_auth/user_serializer.rb
|
93
109
|
- lib/cow_auth/version.rb
|
94
110
|
homepage: https://github.com/mickey13/cow_auth
|
95
111
|
licenses:
|
@@ -101,9 +117,9 @@ require_paths:
|
|
101
117
|
- lib
|
102
118
|
required_ruby_version: !ruby/object:Gem::Requirement
|
103
119
|
requirements:
|
104
|
-
- - "
|
120
|
+
- - "~>"
|
105
121
|
- !ruby/object:Gem::Version
|
106
|
-
version: '
|
122
|
+
version: '2.3'
|
107
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
124
|
requirements:
|
109
125
|
- - ">="
|
@@ -111,8 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
127
|
version: '0'
|
112
128
|
requirements: []
|
113
129
|
rubyforge_project:
|
114
|
-
rubygems_version: 2.6.
|
130
|
+
rubygems_version: 2.6.11
|
115
131
|
signing_key:
|
116
132
|
specification_version: 4
|
117
|
-
summary:
|
133
|
+
summary: Authentication gem
|
118
134
|
test_files: []
|