octopus_auth 0.1.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +55 -0
- data/LICENSE.txt +21 -0
- data/README.md +108 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/octopus_auth/authenticator.rb +34 -0
- data/lib/octopus_auth/configuration.rb +9 -0
- data/lib/octopus_auth/decorators/default.rb +25 -0
- data/lib/octopus_auth/decorators.rb +18 -0
- data/lib/octopus_auth/errors/token_not_found_error.rb +6 -0
- data/lib/octopus_auth/errors.rb +6 -0
- data/lib/octopus_auth/issue.rb +46 -0
- data/lib/octopus_auth/queries/by_scope.rb +34 -0
- data/lib/octopus_auth/queries.rb +6 -0
- data/lib/octopus_auth/revoke.rb +20 -0
- data/lib/octopus_auth/token_generator.rb +18 -0
- data/lib/octopus_auth/version.rb +3 -0
- data/lib/octopus_auth.rb +27 -0
- data/lib/tasks/generate.rb +1 -0
- data/octopus_auth.gemspec +41 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2f79101ae6e88755d81b948be867f3e50c311dd98d92457a9b0dcedeee4c7966
|
4
|
+
data.tar.gz: 5f509145bc1e6b4a073a13b7c0f8db9667aebde05b302fb0e8dce9e82a9670ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a5ae061f4e9e0e6c402c43461439676773e0df96e524130c326f7e5baf10468d2ad0a7b096999f4de02b9179384a968f7ac7ad78cca4aaaaf4b8715ee997f4ac
|
7
|
+
data.tar.gz: '09cd95441c558e584071d75e8315490d46dc591c5f84b102fd19624796a9024c4e7f890a15ad0f1c0ef7dcf7bba90bdbad194723df2ab2ad55f1be64b0bbb461'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
octopus_auth
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.3
|
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.4.3
|
5
|
+
before_install: gem install bundler -v 1.16.0
|
6
|
+
script:
|
7
|
+
- bundle exec rspec
|
8
|
+
deploy:
|
9
|
+
provider: rubygems
|
10
|
+
api_key:
|
11
|
+
secure: NFK+DuUrvFjJFldcCOPNYsmP+CBmkLk/8L/enLA8r6+4qEeVetHySHAV+dWaTGz6FDfZkN0LWQxeJzaQsBXgTKD3Kck14r1hLS7WQwShsjJ+kBNRXEhnlu3lIJxJ8/8AMSezrUwK7dDTD7XDxM/g86IMNpnOPvhmAhuglsKHVHIgGpA8SL+Z3QLEBDMyhYuleskjWXP0DuapXTSHmUWnW5dpkKpDLxI+CoMlHInMImfWoTJDrubDrDNDtxKFP5lXqOGAEuht6OIpteCbXza4bghwHgT5Ao27HpPUqFC9WfpVX1rWA1WAn8gPlGQV/FbO6jdIB94VIODmjOnGCmy9wreQx5mtAZ7HDaXEm2ObYuKp/YSWCSKkgJ2zUamrU8r8b75R8jsqsG38rIuMLOArr93hEMekJZn7TclqRt6R4E4FqFoq1460rWmRk67LFCg5pOouFyLduZNMYS53NhowVL4dxAQ+5RhMK5u07OXmoRYVmqgf+bP2GP82F+8hOfsBOWH24vaOL8yaaqIU3jzJ+1zZga81u+R2lqzUVvEJnDp7NaiPSygj7HPCFhI3RbNLkQd7Rxz3Wq0PeVt/Rt58KpAcCK7+KC+d1qwBKqAFopbPb+UgCcyVNbGcwC1ARA/KRWldnYkjWHfqMzB3VtubBlqdMqGTPaJgK1WLwhAo3Bk=
|
12
|
+
gem: octopus_auth
|
13
|
+
on:
|
14
|
+
tags: true
|
15
|
+
branch: master
|
16
|
+
repo: TINYhr/octopus_auth
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at thien.an.vo.nguyen@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
octopus_auth (0.1.0.rc1)
|
5
|
+
activerecord (>= 3.0.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.3)
|
11
|
+
activesupport (= 5.2.3)
|
12
|
+
activerecord (5.2.3)
|
13
|
+
activemodel (= 5.2.3)
|
14
|
+
activesupport (= 5.2.3)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.3)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
arel (9.0.0)
|
22
|
+
concurrent-ruby (1.1.5)
|
23
|
+
diff-lcs (1.3)
|
24
|
+
i18n (1.6.0)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
minitest (5.11.3)
|
27
|
+
rake (10.5.0)
|
28
|
+
rspec (3.8.0)
|
29
|
+
rspec-core (~> 3.8.0)
|
30
|
+
rspec-expectations (~> 3.8.0)
|
31
|
+
rspec-mocks (~> 3.8.0)
|
32
|
+
rspec-core (3.8.0)
|
33
|
+
rspec-support (~> 3.8.0)
|
34
|
+
rspec-expectations (3.8.3)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.8.0)
|
37
|
+
rspec-mocks (3.8.0)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.8.0)
|
40
|
+
rspec-support (3.8.0)
|
41
|
+
thread_safe (0.3.6)
|
42
|
+
tzinfo (1.2.5)
|
43
|
+
thread_safe (~> 0.1)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
bundler (~> 1.16)
|
50
|
+
octopus_auth!
|
51
|
+
rake (~> 10.0)
|
52
|
+
rspec (~> 3.0)
|
53
|
+
|
54
|
+
BUNDLED WITH
|
55
|
+
1.17.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 An Vo
|
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,108 @@
|
|
1
|
+
# OctopusAuth
|
2
|
+
|
3
|
+
OctopusAuth provides mechanism to:
|
4
|
+
|
5
|
+
* Manage, issue, revoke `access_token`
|
6
|
+
* Authenticate `access_token`
|
7
|
+
|
8
|
+
OctopusAuth excludes data `model`, we could use any orm to persist `access_token`, just make sure it follows interface.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'octopus_auth'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install octopus_auth
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
Configure OctopusAuth before use:
|
29
|
+
|
30
|
+
```
|
31
|
+
OctopusAuth.configure do |config|
|
32
|
+
config.scopes = [:system, :company, :user].freeze
|
33
|
+
config.default_scope = :user
|
34
|
+
config.token_life_time = 2.hours
|
35
|
+
config.token_length = 20
|
36
|
+
config.model_class = AccessToken
|
37
|
+
end
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
Currently, model class `AccessToken` must be an ActiveRecord sub class. with attributes:
|
42
|
+
```
|
43
|
+
:id,
|
44
|
+
:token,
|
45
|
+
:created_at,
|
46
|
+
:issued_at,
|
47
|
+
:active,
|
48
|
+
:expires_at,
|
49
|
+
:scope,
|
50
|
+
:owner_id,
|
51
|
+
:owner_type,
|
52
|
+
:creator_id
|
53
|
+
```
|
54
|
+
|
55
|
+
### Manage token
|
56
|
+
|
57
|
+
`OctopusAuth` support issue, revoke and query access token:
|
58
|
+
|
59
|
+
```
|
60
|
+
access_token = OctopusAuth::Issue.new(:company, 'Company', company_id, user_id).execute
|
61
|
+
access_token = OctopusAuth::Revoke.new(token_as_text).execute
|
62
|
+
access_tokens = OctopusAuth::Queries::ByScope.new(scope, owner_type, owner_id).execute
|
63
|
+
```
|
64
|
+
|
65
|
+
`token` needs
|
66
|
+
|
67
|
+
* A `scope` defined in `config.scopes`, i.e. `:company`
|
68
|
+
* An optional target for that scope, like `('Company', company_id)` We could use polymorphic or any kind of relationship, it's not `OctopusAuth` duty.
|
69
|
+
* And an creator, which should be `user_id` of use in system.
|
70
|
+
|
71
|
+
`OctopusAuth` allow users define their own `AccessToken` model and detaches from it. So `OctopusAuth` don't know anything about token relationships which rely on each business.
|
72
|
+
Every returned token are `OctopusAuth::Decorators::Default` for less rely on `ActiveRecord`
|
73
|
+
|
74
|
+
### Authenticate token
|
75
|
+
|
76
|
+
`OctopusAuth::Authenticator#authenticate` returns `true`/`false`.
|
77
|
+
If true, mean success, block will be called with `success_result` object as below.
|
78
|
+
|
79
|
+
```
|
80
|
+
OctopusAuth::Authenticator.new(token, scope).authenticate do |success_result|
|
81
|
+
track(success_result.token,
|
82
|
+
success_result.scope,
|
83
|
+
success_result.owner_type,
|
84
|
+
success_result.owner_id)
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
### Generate model
|
89
|
+
|
90
|
+
TODO: Write rails/rake tasks to generator model migration
|
91
|
+
|
92
|
+
## Development
|
93
|
+
|
94
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
95
|
+
|
96
|
+
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).
|
97
|
+
|
98
|
+
## Contributing
|
99
|
+
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/octopus_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.
|
101
|
+
|
102
|
+
## License
|
103
|
+
|
104
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
105
|
+
|
106
|
+
## Code of Conduct
|
107
|
+
|
108
|
+
Everyone interacting in the OctopusAuth project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/octopus_auth/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "octopus_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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module OctopusAuth
|
2
|
+
class Authenticator
|
3
|
+
def initialize(token, scope = nil)
|
4
|
+
@token = token
|
5
|
+
@scope = scope || OctopusAuth.configuration.default_scope
|
6
|
+
end
|
7
|
+
|
8
|
+
def authenticate
|
9
|
+
return false if @token.blank?
|
10
|
+
|
11
|
+
access_token = OctopusAuth.configuration.model_class.find_by(token: @token)
|
12
|
+
return false unless access_token && access_token.active?
|
13
|
+
return false if access_token.scope.to_sym != @scope
|
14
|
+
|
15
|
+
if access_token.expires_at > Time.current
|
16
|
+
access_token.update(active: false, expired_at: Time.current)
|
17
|
+
return false
|
18
|
+
end
|
19
|
+
|
20
|
+
yield build_success_result(access_token)
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
ResultObject = Struct.new(:token, :owner_id, :owner_type, :scope)
|
27
|
+
def build_success_result(access_token)
|
28
|
+
ResultObject.new(access_token.token,
|
29
|
+
access_token.owner_id,
|
30
|
+
access_token.owner_type,
|
31
|
+
access_token.scope.to_sym)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module OctopusAuth
|
2
|
+
module Decorators
|
3
|
+
class Default
|
4
|
+
def initialize(token_model)
|
5
|
+
@token_model = token_model
|
6
|
+
end
|
7
|
+
|
8
|
+
OctopusAuth::Decorators::ATTRIBUTES.each do |m|
|
9
|
+
define_method(m) do
|
10
|
+
@token_model.send(m)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_h
|
15
|
+
OctopusAuth::Decorators::ATTRIBUTES.inject({}) do |carry, item|
|
16
|
+
carry[item] = @token_model.send(item)
|
17
|
+
|
18
|
+
carry
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method :to_hash, :to_h
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module OctopusAuth
|
2
|
+
module Decorators
|
3
|
+
ATTRIBUTES = [
|
4
|
+
:id,
|
5
|
+
:token,
|
6
|
+
:created_at, # TODO: [AV] Remove.
|
7
|
+
:issued_at, # Keep here just for compatible reason, use `issued_at` instead
|
8
|
+
:active,
|
9
|
+
:expires_at,
|
10
|
+
:scope,
|
11
|
+
:owner_id,
|
12
|
+
:owner_type,
|
13
|
+
:creator_id
|
14
|
+
]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
require "octopus_auth/decorators/default"
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "octopus_auth/token_generator"
|
2
|
+
|
3
|
+
module OctopusAuth
|
4
|
+
class Issue
|
5
|
+
def initialize(scope, owner_type, owner_id, creator_id)
|
6
|
+
@owner_type = owner_type
|
7
|
+
@owner_id = owner_id
|
8
|
+
@creator_id = creator_id
|
9
|
+
@scope = scope.to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute
|
13
|
+
access_token = OctopusAuth.configuration.model_class.new
|
14
|
+
|
15
|
+
# Set attributes
|
16
|
+
access_token.issued_at = Time.current
|
17
|
+
access_token.expires_at = access_token.issued_at + OctopusAuth.configuration.token_life_time
|
18
|
+
access_token.active = true
|
19
|
+
|
20
|
+
access_token.owner_type = @owner_type
|
21
|
+
access_token.owner_id = @owner_id
|
22
|
+
access_token.creator_id = @creator_id
|
23
|
+
|
24
|
+
access_token.scope = filtered_scope
|
25
|
+
|
26
|
+
access_token.token = generate_token
|
27
|
+
access_token.save!
|
28
|
+
|
29
|
+
OctopusAuth::Decorators::Default.new(access_token)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def filtered_scope
|
35
|
+
if OctopusAuth.configuration.scopes.include?(@scope)
|
36
|
+
return @scope
|
37
|
+
end
|
38
|
+
|
39
|
+
return OctopusAuth.configuration.default_scope
|
40
|
+
end
|
41
|
+
|
42
|
+
def generate_token
|
43
|
+
OctopusAuth::TokenGenerator.unique_token
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module OctopusAuth
|
2
|
+
module Queries
|
3
|
+
class ByScope
|
4
|
+
def initialize(scope, owner_type, owner_id)
|
5
|
+
@scope = scope.to_sym
|
6
|
+
@owner_type = owner_type
|
7
|
+
@owner_id = owner_id
|
8
|
+
end
|
9
|
+
|
10
|
+
def execute
|
11
|
+
relation.map do |token|
|
12
|
+
OctopusAuth::Decorators::Default.new(token)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def relation
|
19
|
+
OctopusAuth.configuration.model_class.where(scope: filtered_scope,
|
20
|
+
owner_type: @owner_type,
|
21
|
+
owner_id: @owner_id,
|
22
|
+
active: true)
|
23
|
+
end
|
24
|
+
|
25
|
+
def filtered_scope
|
26
|
+
if OctopusAuth.configuration.scopes.include?(@scope)
|
27
|
+
return @scope
|
28
|
+
end
|
29
|
+
|
30
|
+
return OctopusAuth.configuration.default_scope
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module OctopusAuth
|
2
|
+
class Revoke
|
3
|
+
def initialize(access_token_value)
|
4
|
+
@access_token = OctopusAuth.configuration.model_class.find_by(token: access_token_value)
|
5
|
+
|
6
|
+
unless @access_token
|
7
|
+
raise OctopusAuth::Errors::TokenNotFoundError.new("API Access token #{access_token_value} is not found")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute
|
12
|
+
@access_token.expired_at = Time.current
|
13
|
+
@access_token.active = false
|
14
|
+
|
15
|
+
@access_token.save!
|
16
|
+
|
17
|
+
OctopusAuth::Decorators::Default.new(@access_token)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module OctopusAuth
|
2
|
+
class TokenGenerator
|
3
|
+
class << self
|
4
|
+
def unique_token
|
5
|
+
loop do
|
6
|
+
token = generate(OctopusAuth.configuration.token_length)
|
7
|
+
break token unless OctopusAuth.configuration.model_class.where(token: token).exists?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def generate(length)
|
14
|
+
SecureRandom.hex(length / 2)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/octopus_auth.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "octopus_auth/version"
|
2
|
+
|
3
|
+
require "octopus_auth/configuration"
|
4
|
+
|
5
|
+
require "octopus_auth/errors"
|
6
|
+
require "octopus_auth/decorators"
|
7
|
+
|
8
|
+
require "octopus_auth/issue"
|
9
|
+
require "octopus_auth/revoke"
|
10
|
+
require "octopus_auth/queries"
|
11
|
+
|
12
|
+
require "octopus_auth/authenticator"
|
13
|
+
|
14
|
+
module OctopusAuth
|
15
|
+
class << self
|
16
|
+
attr_accessor :configuration
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configure
|
20
|
+
self.configuration ||= OctopusAuth::Configuration.new
|
21
|
+
yield(configuration)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.reset
|
25
|
+
self.configuration = OctopusAuth::Configuration.new
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# TODO: [AV] Write model generate tasks
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "octopus_auth/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "octopus_auth"
|
9
|
+
spec.version = OctopusAuth::VERSION
|
10
|
+
spec.authors = ["TINYpulse Devops"]
|
11
|
+
spec.email = ["devops@tinypulse.com"]
|
12
|
+
|
13
|
+
spec.summary = 'Authentication for TINYoctopus'
|
14
|
+
spec.description = 'Authentication for TINYoctopus, include access_token management'
|
15
|
+
spec.homepage = 'https://github.com/TINYhr/octopus_auth'
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata["allowed_push_host"] = 'http://rubygems.com'
|
22
|
+
else
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
"public gem pushes."
|
25
|
+
end
|
26
|
+
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
end
|
32
|
+
spec.bindir = "exe"
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
39
|
+
|
40
|
+
spec.add_dependency "activerecord", ">= 3.0.0"
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: octopus_auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.rc1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TINYpulse Devops
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-31 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
69
|
+
description: Authentication for TINYoctopus, include access_token management
|
70
|
+
email:
|
71
|
+
- devops@tinypulse.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-gemset"
|
79
|
+
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
81
|
+
- CODE_OF_CONDUCT.md
|
82
|
+
- Gemfile
|
83
|
+
- Gemfile.lock
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- bin/console
|
88
|
+
- bin/setup
|
89
|
+
- lib/octopus_auth.rb
|
90
|
+
- lib/octopus_auth/authenticator.rb
|
91
|
+
- lib/octopus_auth/configuration.rb
|
92
|
+
- lib/octopus_auth/decorators.rb
|
93
|
+
- lib/octopus_auth/decorators/default.rb
|
94
|
+
- lib/octopus_auth/errors.rb
|
95
|
+
- lib/octopus_auth/errors/token_not_found_error.rb
|
96
|
+
- lib/octopus_auth/issue.rb
|
97
|
+
- lib/octopus_auth/queries.rb
|
98
|
+
- lib/octopus_auth/queries/by_scope.rb
|
99
|
+
- lib/octopus_auth/revoke.rb
|
100
|
+
- lib/octopus_auth/token_generator.rb
|
101
|
+
- lib/octopus_auth/version.rb
|
102
|
+
- lib/tasks/generate.rb
|
103
|
+
- octopus_auth.gemspec
|
104
|
+
homepage: https://github.com/TINYhr/octopus_auth
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata:
|
108
|
+
allowed_push_host: http://rubygems.com
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 1.3.1
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.7.7
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Authentication for TINYoctopus
|
129
|
+
test_files: []
|