tiddle 0.1.0 → 0.2.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 +4 -4
- data/.rubocop.yml +23 -0
- data/.travis.yml +3 -0
- data/README.md +5 -1
- data/Rakefile +6 -1
- data/lib/tiddle.rb +4 -4
- data/lib/tiddle/strategy.rb +1 -1
- data/lib/tiddle/token_issuer.rb +14 -7
- data/lib/tiddle/version.rb +1 -1
- data/spec/spec_helper.rb +11 -0
- data/spec/strategy_spec.rb +14 -7
- data/spec/tiddle_spec.rb +6 -3
- data/tiddle.gemspec +3 -0
- metadata +46 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4c58a31ce7dbf835432646bbecf480fe467fede
|
4
|
+
data.tar.gz: 9f23863b1154a07e6a25e1a8c461d0d9f77d991b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a6672cc79273f211699428066c75f1dfa3bc2adb7202e93f919bd8b4d93dd7835e0cd834356a92e51251e0d02100399c173a754dcb42a5c932e9923c0ff27cc
|
7
|
+
data.tar.gz: 92ddb425b8042af5df79e4feac473ca2518affa947f92253047957d65e6465c77efbb3fb63d98283622f5dce80c7fca8f4e9186078638185c0be19253564f8f2
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
AllCops:
|
2
|
+
Include:
|
3
|
+
- 'lib'
|
4
|
+
- 'spec'
|
5
|
+
Exclude:
|
6
|
+
- 'spec/rails_app/**/*'
|
7
|
+
- 'spec/spec_helper.rb'
|
8
|
+
Style/StringLiterals:
|
9
|
+
Enabled: false
|
10
|
+
Style/EmptyLinesAroundBlockBody:
|
11
|
+
Enabled: false
|
12
|
+
Style/BracesAroundHashParameters:
|
13
|
+
EnforcedStyle: context_dependent
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
Style/EmptyLinesAroundClassBody:
|
17
|
+
Enabled: false
|
18
|
+
Style/IndentationConsistency:
|
19
|
+
Enabled: false
|
20
|
+
Style/EmptyLinesAroundModuleBody:
|
21
|
+
Enabled: false
|
22
|
+
Style/MultilineOperationIndentation:
|
23
|
+
EnforcedStyle: indented
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# Tiddle
|
2
2
|
|
3
|
+
[](https://travis-ci.org/adamniedzielski/tiddle)
|
4
|
+
[](https://coveralls.io/r/adamniedzielski/tiddle?branch=master)
|
5
|
+
[](https://codeclimate.com/github/adamniedzielski/tiddle)
|
6
|
+
|
3
7
|
Tiddle provides Devise strategy for token authentication in API-only Ruby on Rails applications. Its main feature is **support for multiple tokens per user**.
|
4
8
|
|
5
9
|
Tiddle is lightweight and non-configurable. It does what it has to do and leaves some manual implementation to you.
|
@@ -50,7 +54,7 @@ class Users::SessionsController < Devise::SessionsController
|
|
50
54
|
|
51
55
|
def create
|
52
56
|
[...]
|
53
|
-
token = Tiddle.create_and_return_token(resource)
|
57
|
+
token = Tiddle.create_and_return_token(resource, request)
|
54
58
|
render json: { authentication_token: token }
|
55
59
|
end
|
56
60
|
|
data/Rakefile
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require 'rspec/core/rake_task'
|
3
|
-
|
3
|
+
require 'rubocop/rake_task'
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(spec: :rubocop)
|
6
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
7
|
+
task.fail_on_error = false
|
8
|
+
end
|
4
9
|
|
5
10
|
task :default => :spec
|
data/lib/tiddle.rb
CHANGED
data/lib/tiddle/strategy.rb
CHANGED
data/lib/tiddle/token_issuer.rb
CHANGED
@@ -11,24 +11,31 @@ module Tiddle
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def create_and_return_token(resource, request)
|
14
|
-
token = resource.authentication_tokens
|
15
|
-
create! body: generate_token,
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
token = resource.authentication_tokens
|
15
|
+
.create! body: generate_token,
|
16
|
+
last_used_at: DateTime.current,
|
17
|
+
ip_address: request.remote_ip,
|
18
|
+
user_agent: request.user_agent
|
19
19
|
|
20
20
|
token.body
|
21
21
|
end
|
22
22
|
|
23
23
|
def expire_token(resource, request)
|
24
|
-
resource.authentication_tokens
|
24
|
+
resource.authentication_tokens
|
25
|
+
.where(body: request.headers["X-#{resource.model_name.to_s.upcase}-TOKEN"])
|
26
|
+
.take!
|
27
|
+
.destroy
|
25
28
|
end
|
26
29
|
|
27
30
|
def purge_old_tokens(resource)
|
28
|
-
resource.authentication_tokens
|
31
|
+
resource.authentication_tokens
|
32
|
+
.order(last_used_at: :desc)
|
33
|
+
.offset(maximum_tokens_per_user)
|
34
|
+
.destroy_all
|
29
35
|
end
|
30
36
|
|
31
37
|
private
|
38
|
+
|
32
39
|
attr_accessor :maximum_tokens_per_user
|
33
40
|
|
34
41
|
def generate_token
|
data/lib/tiddle/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
|
4
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
5
|
+
SimpleCov::Formatter::HTMLFormatter,
|
6
|
+
Coveralls::SimpleCov::Formatter
|
7
|
+
]
|
8
|
+
SimpleCov.start do
|
9
|
+
add_filter "/spec/"
|
10
|
+
end
|
11
|
+
|
1
12
|
ENV["RAILS_ENV"] = 'test'
|
2
13
|
ENV["DATABASE_URL"] = "sqlite3:db/test.sqlite3"
|
3
14
|
|
data/spec/strategy_spec.rb
CHANGED
@@ -8,7 +8,8 @@ describe "Authentication using Tiddle strategy", type: :request do
|
|
8
8
|
context "with valid email and token" do
|
9
9
|
|
10
10
|
it "allows to access endpoints which require authentication" do
|
11
|
-
get secrets_path, {},
|
11
|
+
get secrets_path, {},
|
12
|
+
{ "X-USER-EMAIL" => "test@example.com", "X-USER-TOKEN" => @token }
|
12
13
|
expect(response.status).to eq 200
|
13
14
|
end
|
14
15
|
|
@@ -17,12 +18,14 @@ describe "Authentication using Tiddle strategy", type: :request do
|
|
17
18
|
context "when token was last used more than hour ago" do
|
18
19
|
|
19
20
|
before do
|
20
|
-
@user.authentication_tokens.last
|
21
|
+
@user.authentication_tokens.last
|
22
|
+
.update_attribute(:last_used_at, 2.hours.ago)
|
21
23
|
end
|
22
24
|
|
23
25
|
it "updates last_used_at field" do
|
24
26
|
expect do
|
25
|
-
get secrets_path, {},
|
27
|
+
get secrets_path, {},
|
28
|
+
{ "X-USER-EMAIL" => "test@example.com", "X-USER-TOKEN" => @token }
|
26
29
|
end.to change { @user.authentication_tokens.last.last_used_at }
|
27
30
|
end
|
28
31
|
end
|
@@ -35,7 +38,8 @@ describe "Authentication using Tiddle strategy", type: :request do
|
|
35
38
|
|
36
39
|
it "does not update last_used_at field" do
|
37
40
|
expect do
|
38
|
-
get secrets_path, {},
|
41
|
+
get secrets_path, {},
|
42
|
+
{ "X-USER-EMAIL" => "test@example.com", "X-USER-TOKEN" => @token }
|
39
43
|
end.not_to change { @user.authentication_tokens.last.last_used_at }
|
40
44
|
end
|
41
45
|
end
|
@@ -44,7 +48,8 @@ describe "Authentication using Tiddle strategy", type: :request do
|
|
44
48
|
context "when email contains uppercase letters" do
|
45
49
|
|
46
50
|
it "converts email to lower case and authenticates user" do
|
47
|
-
get secrets_path, {},
|
51
|
+
get secrets_path, {},
|
52
|
+
{ "X-USER-EMAIL" => "TEST@example.com", "X-USER-TOKEN" => @token }
|
48
53
|
expect(response.status).to eq 200
|
49
54
|
end
|
50
55
|
end
|
@@ -53,7 +58,8 @@ describe "Authentication using Tiddle strategy", type: :request do
|
|
53
58
|
context "with invalid email and valid token" do
|
54
59
|
|
55
60
|
it "does not allow to access endpoints which require authentication" do
|
56
|
-
get secrets_path, {},
|
61
|
+
get secrets_path, {},
|
62
|
+
{ "X-USER-EMAIL" => "wrong@example.com", "X-USER-TOKEN" => @token }
|
57
63
|
expect(response.status).to eq 401
|
58
64
|
end
|
59
65
|
end
|
@@ -61,7 +67,8 @@ describe "Authentication using Tiddle strategy", type: :request do
|
|
61
67
|
context "with valid email and invalid token" do
|
62
68
|
|
63
69
|
it "does not allow to access endpoints which require authentication" do
|
64
|
-
get secrets_path, {},
|
70
|
+
get secrets_path, {},
|
71
|
+
{ "X-USER-EMAIL" => "test@example.com", "X-USER-TOKEN" => "wrong" }
|
65
72
|
expect(response.status).to eq 401
|
66
73
|
end
|
67
74
|
end
|
data/spec/tiddle_spec.rb
CHANGED
@@ -19,16 +19,19 @@ describe Tiddle do
|
|
19
19
|
|
20
20
|
it "sets last_used_at field" do
|
21
21
|
Tiddle.create_and_return_token(@user, FakeRequest.new)
|
22
|
-
expect(@user.authentication_tokens.last.last_used_at)
|
22
|
+
expect(@user.authentication_tokens.last.last_used_at)
|
23
|
+
.to be_within(1).of(DateTime.current)
|
23
24
|
end
|
24
25
|
|
25
26
|
it "saves ip address" do
|
26
|
-
Tiddle.create_and_return_token
|
27
|
+
Tiddle.create_and_return_token @user,
|
28
|
+
FakeRequest.new(remote_ip: "123.101.54.1")
|
27
29
|
expect(@user.authentication_tokens.last.ip_address).to eq "123.101.54.1"
|
28
30
|
end
|
29
31
|
|
30
32
|
it "saves user agent" do
|
31
|
-
Tiddle.create_and_return_token
|
33
|
+
Tiddle.create_and_return_token @user,
|
34
|
+
FakeRequest.new(user_agent: "Internet Explorer 4.0")
|
32
35
|
expect(@user.authentication_tokens.last.user_agent).to eq "Internet Explorer 4.0"
|
33
36
|
end
|
34
37
|
end
|
data/tiddle.gemspec
CHANGED
@@ -26,4 +26,7 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency "rspec-rails"
|
27
27
|
spec.add_development_dependency "rails", "~> 4.2.0"
|
28
28
|
spec.add_development_dependency "sqlite3"
|
29
|
+
spec.add_development_dependency "coveralls"
|
30
|
+
spec.add_development_dependency "simplecov"
|
31
|
+
spec.add_development_dependency "rubocop"
|
29
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiddle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Niedzielski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: devise
|
@@ -108,6 +108,48 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: coveralls
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
111
153
|
description:
|
112
154
|
email:
|
113
155
|
- adamsunday@gmail.com
|
@@ -117,6 +159,8 @@ extra_rdoc_files: []
|
|
117
159
|
files:
|
118
160
|
- ".gitignore"
|
119
161
|
- ".rspec"
|
162
|
+
- ".rubocop.yml"
|
163
|
+
- ".travis.yml"
|
120
164
|
- Gemfile
|
121
165
|
- LICENSE.txt
|
122
166
|
- README.md
|