auth_jwt_go 0.1.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.
@@ -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
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in auth_jwt_go.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'jwt'
9
+ gem "pry-byebug"
@@ -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.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -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
@@ -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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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,9 @@
1
+ module AuthJwtGo
2
+ class AuthorizedUser
3
+ def initialize(fields)
4
+ fields.each do |key, value|
5
+ self.define_singleton_method(key.to_s.underscore) { return value }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module AuthJwtGo
2
+ VERSION = "0.1.0"
3
+ 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: []