auth_jwt_go 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/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/README.md +57 -0
- data/Rakefile +6 -0
- data/auth_jwt_go.gemspec +23 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/auth_jwt_go.rb +76 -0
- data/lib/auth_jwt_go/authorized_user.rb +9 -0
- data/lib/auth_jwt_go/version.rb +3 -0
- data/lib/generators/auth_jwt_go/install_generator.rb +15 -0
- data/lib/generators/auth_jwt_go/templates/init_auth_jwt.rb +8 -0
- data/lib/generators/auth_jwt_go/templates/request_helpers.rb +25 -0
- data/lib/generators/auth_jwt_go/test_helper_generator.rb +17 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 88f0fe6e0796919739795db92cd3a0543acd4623994ff0a48e87aa6f99817088
|
4
|
+
data.tar.gz: f2d78358db5a9852bd5fa43cfa50ca37a580f332fd3a04eb327341e81d97f9cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eb9a68dda8df17452b62e875296aadf49503f36c73201ba1d9afb4f35d4226cb7e5eace710a15e3e72d091c2a0cc25ba6b301ce40b33bdbc72b6885a1437d47f
|
7
|
+
data.tar.gz: fb2d35162af6de6476a169c59890cf722ae0e934844301f025e5358cb2b8356d1fe3935268021ab6c5dc96651a4df13403a462d7afbe912115d67a8fa19df3ec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# AuthJwt
|
2
|
+
|
3
|
+
This gem allows you to make identifications for your microservices and create the Jwt if you wish so
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'auth_jwt_go'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install auth_jwt_go
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Once installed run the following commands:
|
24
|
+
|
25
|
+
1- It will create a file in the initializer so you can configure the parameters
|
26
|
+
|
27
|
+
rails g auth_jwt_go:install
|
28
|
+
|
29
|
+
2- Create a helper in the spec support to handle authentication tests
|
30
|
+
|
31
|
+
rails g auth_jwt_go:test_helper
|
32
|
+
|
33
|
+
3- In your main controller or base you must add the module
|
34
|
+
|
35
|
+
include AuthJwtGo
|
36
|
+
|
37
|
+
Example
|
38
|
+
class ApiController < ActionController::API
|
39
|
+
include AuthJwtGo
|
40
|
+
end
|
41
|
+
|
42
|
+
4- You can use two methods for accessing your API
|
43
|
+
|
44
|
+
before_action :authorized_app
|
45
|
+
before_action :authorized_user
|
46
|
+
|
47
|
+
Example
|
48
|
+
class ApiController < ActionController::API
|
49
|
+
include AuthJwtGo
|
50
|
+
|
51
|
+
before_action :authorized_app # Authenticate the client app
|
52
|
+
before_action :authorized_user # Identify the user based on the jwt for login
|
53
|
+
end
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/auth_jwt.
|
data/Rakefile
ADDED
data/auth_jwt_go.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'lib/auth_jwt_go/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "auth_jwt_go"
|
5
|
+
spec.version = AuthJwtGo::VERSION
|
6
|
+
spec.authors = ["jonathan rojas"]
|
7
|
+
spec.email = ["jonathangrh.25@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Auth API with Jwt include current_user helper}
|
10
|
+
spec.description = %q{this authenticates your api with jwt include current_user helper}
|
11
|
+
spec.homepage = 'https://github.com/remolacho/auth_jwt_go'
|
12
|
+
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "auth_jwt_go"
|
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
data/lib/auth_jwt_go.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'auth_jwt_go/version'
|
2
|
+
require 'auth_jwt_go/authorized_user'
|
3
|
+
|
4
|
+
module AuthJwtGo
|
5
|
+
class << self
|
6
|
+
mattr_accessor :secret_key_api,
|
7
|
+
:secret_key_jwt,
|
8
|
+
:algorithm,
|
9
|
+
:class_name_model,
|
10
|
+
:model_primary_key,
|
11
|
+
:payload_primary_key
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configure(&block)
|
15
|
+
yield self
|
16
|
+
end
|
17
|
+
|
18
|
+
def authorized_user
|
19
|
+
render json: { message: 'The user has not token active' }, status: 403 unless logged_in?
|
20
|
+
rescue StandardError => e
|
21
|
+
render json: { message: "The error was #{e.to_s}" }, status: 500
|
22
|
+
end
|
23
|
+
|
24
|
+
def authorized_app
|
25
|
+
unless AuthJwtGo::secret_key_api.eql?(auth_client)
|
26
|
+
render json: {message: 'The app has not access' }, status: 401
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def encode_token(payload)
|
31
|
+
return { message: 'The payload is not present'} unless payload.present?
|
32
|
+
{jwt: JWT.encode(payload, AuthJwtGo::secret_key_jwt, AuthJwtGo::algorithm || 'HS256')}
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def logged_in?
|
38
|
+
!!logged_in_user
|
39
|
+
end
|
40
|
+
|
41
|
+
def logged_in_user
|
42
|
+
decode = decoded_token
|
43
|
+
return unless decode
|
44
|
+
|
45
|
+
default_field = 'id'
|
46
|
+
field_payload = AuthJwtGo::payload_primary_key.presence || default_field
|
47
|
+
field_model = AuthJwtGo::model_primary_key.presence || default_field
|
48
|
+
|
49
|
+
@current_user = if AuthJwtGo::class_name_model.present?
|
50
|
+
AuthJwtGo::class_name_model.classify.constantize.find_by(field_model => decode[0][field_payload])
|
51
|
+
else
|
52
|
+
AuthJwtGo::AuthorizedUser.new(decode[0])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def decoded_token
|
57
|
+
return unless auth_jwt.present?
|
58
|
+
token = auth_jwt.split(' ')[1]
|
59
|
+
JWT.decode(token, AuthJwtGo::secret_key_jwt, true, { algorithm: AuthJwtGo::algorithm || 'HS256'} )
|
60
|
+
rescue JWT::DecodeError
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
|
64
|
+
# { Authorization: 'Bearer <token>' }
|
65
|
+
def auth_jwt
|
66
|
+
request.headers['Authorization']
|
67
|
+
end
|
68
|
+
|
69
|
+
def auth_client
|
70
|
+
request.headers['Api-Token']
|
71
|
+
end
|
72
|
+
|
73
|
+
def current_user
|
74
|
+
@current_user
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
module AuthJwtGo
|
3
|
+
module Generators
|
4
|
+
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
desc 'Creates a AuthJwt initializer'
|
9
|
+
|
10
|
+
def copy_initializer
|
11
|
+
copy_file 'init_auth_jwt.rb', 'config/initializers/init_auth_jwt.rb'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
AuthJwtGo.configure do |config|
|
2
|
+
# config.secret_key_api = 'SECRET_API'
|
3
|
+
# config.secret_key_jwt = 'SECRET_JWT'
|
4
|
+
# config.algorithm = 'HS256' # is optional by default 'HS256'
|
5
|
+
# config.class_name_model = 'User' # is optional, if not send it creates a user object based on the payload
|
6
|
+
# config.model_primary_key = 'id' # is optional by default 'id'
|
7
|
+
# config.payload_primary_key = 'user_id' # is optional by default 'id'
|
8
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module RequestHelpers
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
include AuthJwtGo
|
4
|
+
|
5
|
+
private
|
6
|
+
# Only with devise
|
7
|
+
# def sign_in_user(user)
|
8
|
+
# sign_in user
|
9
|
+
# end
|
10
|
+
|
11
|
+
# def encode_jwt(user)
|
12
|
+
# payload = {
|
13
|
+
# user_id: user.id,
|
14
|
+
# email: user.email,
|
15
|
+
# role: user.kind,
|
16
|
+
# exp: 1.days.from_now.to_i
|
17
|
+
# }
|
18
|
+
# encode_token(payload).dig(:jwt)
|
19
|
+
# end
|
20
|
+
|
21
|
+
# def setup_jwt(user)
|
22
|
+
# "Bearer #{encode_jwt(user)}"
|
23
|
+
# end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
module AuthJwtGo
|
3
|
+
module Generators
|
4
|
+
|
5
|
+
class TestHelperGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
desc 'Creates a AuthJwt initializer'
|
9
|
+
|
10
|
+
def test_initializer
|
11
|
+
dir_name = 'spec/support'
|
12
|
+
Dir.mkdir(dir_name) unless File.exists?(dir_name)
|
13
|
+
copy_file 'request_helpers.rb', "#{dir_name}/request_helpers.rb"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: auth_jwt_go
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jonathan rojas
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: this authenticates your api with jwt include current_user helper
|
14
|
+
email:
|
15
|
+
- jonathangrh.25@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- auth_jwt_go.gemspec
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/auth_jwt_go.rb
|
30
|
+
- lib/auth_jwt_go/authorized_user.rb
|
31
|
+
- lib/auth_jwt_go/version.rb
|
32
|
+
- lib/generators/auth_jwt_go/install_generator.rb
|
33
|
+
- lib/generators/auth_jwt_go/templates/init_auth_jwt.rb
|
34
|
+
- lib/generators/auth_jwt_go/templates/request_helpers.rb
|
35
|
+
- lib/generators/auth_jwt_go/test_helper_generator.rb
|
36
|
+
homepage: https://github.com/remolacho/auth_jwt_go
|
37
|
+
licenses: []
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.3.0
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.0.6
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Auth API with Jwt include current_user helper
|
58
|
+
test_files: []
|