warrant 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/.rubocop.yml +13 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +16 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/warrant/models/user.rb +11 -0
- data/lib/warrant/models/user_warrant.rb +14 -0
- data/lib/warrant/models/userset.rb +13 -0
- data/lib/warrant/models/userset_warrant.rb +14 -0
- data/lib/warrant/version.rb +5 -0
- data/lib/warrant/warrant_client.rb +91 -0
- data/lib/warrant/warrant_configuration.rb +13 -0
- data/lib/warrant.rb +26 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d582aad391d045a0560fa31d72874b6daab745730fde4166a8a928372b0dfd1a
|
4
|
+
data.tar.gz: b8b88e6cb72cca841e4261460a9562c3f6228894cab278c88de4bb69b4838aeb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af98bc58266547012886473b49d3d8e0a4434e922dedc6d7372fb4b1d90f95d7d77cdba276281dde5f0316ffa4ba60b575d9331ba0f30774fbe2cc30d7cbafe3
|
7
|
+
data.tar.gz: d99f3dccc78f9855ff239671ebde3ac5903e26311e4c1a2d2122ff864fa642b1801d03529a713037de26c510dc6030e32c2fb276bb779206aba8e532d44830de
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Warrant (Forerunner Labs, Inc.)
|
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,80 @@
|
|
1
|
+
# Warrant Ruby Library
|
2
|
+
|
3
|
+
Use [Warrant](https://warrant.dev/) in ruby projects.
|
4
|
+
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/warrant.svg)](https://badge.fury.io/rb/warrant)
|
6
|
+
[![Discord](https://img.shields.io/discord/865661082203193365?label=discord)](https://discord.gg/QNCMKWzqET)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'warrant'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install warrant
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'warrant'
|
28
|
+
Warrant.api_key = 'api_test_f5dsKVeYnVSLHGje44zAygqgqXiLJBICbFzCiAg1E='
|
29
|
+
```
|
30
|
+
|
31
|
+
### `createUser(user_id, username)`
|
32
|
+
|
33
|
+
This method creates a user entity in Warrant with the specified `userId`. Provide an optional `username` to make it easier to identify users in the Warrant dashboard.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# Create user with `user.id` as the user_id and `user.email` as the username
|
37
|
+
Warrant::WarrantClient.create_user(user.id, user.email)
|
38
|
+
|
39
|
+
# Create user with generated id
|
40
|
+
Warrant::WarrantClient.create_user()
|
41
|
+
```
|
42
|
+
|
43
|
+
### `createWarrant(objectType, objectId, relation, user)`
|
44
|
+
|
45
|
+
This method creates a warrant which specifies that the provided `user` (or userset) has `relation` on the object of type `objectType` with id `objectId`.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# Create a warrant allowing user.id to "view" the store with id store.id
|
49
|
+
Warrant::WarrantClient.create_warrant('store', store.id, 'view', { userId: user.id })
|
50
|
+
```
|
51
|
+
|
52
|
+
### `createSession(userId)`
|
53
|
+
|
54
|
+
This method creates a session in Warrant for the user with the specified `userId` and returns a session token which can be used to make authorized requests to the Warrant API only for the specified user. This session token can safely be used to make requests to the Warrant API's authorization endpoint to determine user access in web and mobile client applications.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
# Create session token for user
|
58
|
+
Warrant::WarrantClient.create_session(user.id)
|
59
|
+
```
|
60
|
+
|
61
|
+
### `isAuthorized(objectType, objectId, relation, userId)`
|
62
|
+
|
63
|
+
This method returns `true` if the user with the specified `userId` has the specified `relation` to the object of type `objectType` with id `objectId` and `false` otherwise.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# Example: user 123 can only view store 824
|
67
|
+
Warrant::WarrantClient.is_authorized('store', '824', 'view', '123') # true
|
68
|
+
Warrant::WarrantClient.is_authorized('store', '824', 'edit', '123') # false
|
69
|
+
```
|
70
|
+
|
71
|
+
We’ve used a random API key in these code examples. Replace it with your [actual publishable API keys](https://app.warrant.dev) to
|
72
|
+
test this code through your own Warrant account.
|
73
|
+
|
74
|
+
For more information on how to use the Warrant API, please refer to the [Warrant API reference](https://docs.warrant.dev).
|
75
|
+
|
76
|
+
Note that we may release new [minor and patch](https://semver.org/) versions of this library with small but backwards-incompatible fixes to the type declarations. These changes will not affect Warrant itself.
|
77
|
+
|
78
|
+
## Warrant Documentation
|
79
|
+
|
80
|
+
- [Warrant Docs](https://docs.warrant.dev/)
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "warrant"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Warrant
|
4
|
+
class UserWarrant
|
5
|
+
attr_reader :object_type, :object_id, :relation, :user
|
6
|
+
|
7
|
+
def initialize(object_type, object_id, relation, user_id)
|
8
|
+
@object_type = object_type
|
9
|
+
@object_id = object_id
|
10
|
+
@relation = relation
|
11
|
+
@user = User.new(user_id)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Warrant
|
4
|
+
class Userset
|
5
|
+
attr_reader :object_type, :object_id, :relation
|
6
|
+
|
7
|
+
def initialize(object_type, object_id, relation)
|
8
|
+
@object_type = object_type
|
9
|
+
@object_id = object_id
|
10
|
+
@relation = relation
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Warrant
|
4
|
+
class UsersetWarrant
|
5
|
+
attr_reader :object_type, :object_id, :relation, :user
|
6
|
+
|
7
|
+
def initialize(object_type, object_id, relation, user)
|
8
|
+
@object_type = object_type
|
9
|
+
@object_id = object_id
|
10
|
+
@relation = relation
|
11
|
+
@user = Userset.new(user['objectType'], user['objectId'], user['relation'])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Warrant
|
4
|
+
class WarrantClient
|
5
|
+
class << self
|
6
|
+
def create_user(user_id = '', username = '')
|
7
|
+
uri = URI.parse("#{Warrant.config.api_base}/v1/users")
|
8
|
+
params = {
|
9
|
+
userId: user_id,
|
10
|
+
username: username
|
11
|
+
}
|
12
|
+
res = post(uri, params)
|
13
|
+
res_json = JSON.parse(res.body)
|
14
|
+
|
15
|
+
case res
|
16
|
+
when Net::HTTPSuccess
|
17
|
+
User.new(res_json['userId'])
|
18
|
+
else
|
19
|
+
res_json
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_warrant(object_type, object_id, relation, user)
|
24
|
+
uri = URI.parse("#{Warrant.config.api_base}/v1/warrants")
|
25
|
+
params = {
|
26
|
+
objectType: object_type,
|
27
|
+
objectId: object_id,
|
28
|
+
relation: relation,
|
29
|
+
user: user
|
30
|
+
}
|
31
|
+
res = post(uri, params)
|
32
|
+
res_json = JSON.parse(res.body)
|
33
|
+
|
34
|
+
case res
|
35
|
+
when Net::HTTPSuccess
|
36
|
+
if res_json['user']['userId']
|
37
|
+
UserWarrant.new(res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user']['userId'])
|
38
|
+
elsif res_json['user']['objectType']
|
39
|
+
UsersetWarrant.new(res_json['objectType'], res_json['objectId'], res_json['relation'], res_json['user'])
|
40
|
+
end
|
41
|
+
else
|
42
|
+
res_json
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_session(user_id)
|
47
|
+
uri = URI.parse("#{Warrant.config.api_base}/v1/users/#{user_id}/sessions")
|
48
|
+
res = post(uri)
|
49
|
+
res_json = JSON.parse(res.body)
|
50
|
+
|
51
|
+
case res
|
52
|
+
when Net::HTTPSuccess
|
53
|
+
res_json['token']
|
54
|
+
else
|
55
|
+
res_json
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def is_authorized(object_type, object_id, relation, user_id)
|
60
|
+
uri = URI.parse("#{Warrant.config.api_base}/v1/authorize")
|
61
|
+
params = {
|
62
|
+
objectType: object_type,
|
63
|
+
objectId: object_id,
|
64
|
+
relation: relation,
|
65
|
+
user: {
|
66
|
+
userId: user_id
|
67
|
+
}
|
68
|
+
}
|
69
|
+
res = post(uri, params)
|
70
|
+
res_json = JSON.parse(res.body)
|
71
|
+
|
72
|
+
if res.is_a? Net::HTTPSuccess
|
73
|
+
true
|
74
|
+
else
|
75
|
+
false
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def post(uri, params = {})
|
82
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
83
|
+
http.use_ssl = true
|
84
|
+
headers = {
|
85
|
+
"Authorization": "ApiKey #{Warrant.config.api_key}"
|
86
|
+
}
|
87
|
+
http.post(uri.path, params.to_json, headers)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/warrant.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "warrant/version"
|
4
|
+
|
5
|
+
require "net/http"
|
6
|
+
require "json"
|
7
|
+
require "forwardable"
|
8
|
+
|
9
|
+
require "warrant/models/user"
|
10
|
+
require "warrant/models/userset"
|
11
|
+
require "warrant/models/user_warrant"
|
12
|
+
require "warrant/models/userset_warrant"
|
13
|
+
require "warrant/warrant_configuration"
|
14
|
+
require "warrant/warrant_client"
|
15
|
+
|
16
|
+
module Warrant
|
17
|
+
@config = Warrant::WarrantConfiguration.new
|
18
|
+
|
19
|
+
class << self
|
20
|
+
extend Forwardable
|
21
|
+
|
22
|
+
attr_reader :config
|
23
|
+
|
24
|
+
def_delegators :@config, :api_key, :api_key=
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: warrant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Warrant
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby library for the Warrant API at https://warrant.dev.
|
14
|
+
email: hello@warrant.dev
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".rubocop.yml"
|
20
|
+
- CHANGELOG.md
|
21
|
+
- CODE_OF_CONDUCT.md
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- lib/warrant.rb
|
29
|
+
- lib/warrant/models/user.rb
|
30
|
+
- lib/warrant/models/user_warrant.rb
|
31
|
+
- lib/warrant/models/userset.rb
|
32
|
+
- lib/warrant/models/userset_warrant.rb
|
33
|
+
- lib/warrant/version.rb
|
34
|
+
- lib/warrant/warrant_client.rb
|
35
|
+
- lib/warrant/warrant_configuration.rb
|
36
|
+
homepage: https://github.com/warrant-dev/warrant-ruby
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata:
|
40
|
+
homepage_uri: https://github.com/warrant-dev/warrant-ruby
|
41
|
+
source_code_uri: https://github.com/warrant-dev/warrant-ruby
|
42
|
+
changelog_uri: https://github.com/warrant-dev/warrant-ruby/CHANGELOG.md
|
43
|
+
documentation_uri: https://docs.warrant.dev/
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.3.0
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.2.14
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Warrant Ruby Library
|
63
|
+
test_files: []
|