rails_jwt_auth_omniauth 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +151 -0
- data/Rakefile +27 -0
- data/app/controllers/rails_jwt_auth_omniauth/omniauths_controller.rb +24 -0
- data/app/models/concerns/rails_jwt_auth_omniauth/omniauthable.rb +20 -0
- data/config/locales/en.yml +6 -0
- data/lib/rails_jwt_auth_omniauth/engine.rb +16 -0
- data/lib/rails_jwt_auth_omniauth/omniauth_manager.rb +32 -0
- data/lib/rails_jwt_auth_omniauth/omniauth_session.rb +56 -0
- data/lib/rails_jwt_auth_omniauth/version.rb +3 -0
- data/lib/rails_jwt_auth_omniauth.rb +17 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5b089f55ef748ff14d8e1f82e57acf3959e9885f20a245ec94062c6ad137a603
|
4
|
+
data.tar.gz: f8d99b0c40211dfc4a1a960a22dc038d4f3ea1ffc53def101fa8e219c3231cf9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 945e08fbabc1f3d77d52fc02a044412843dd655bc957498284781e426fa2b796714c3b423fff550f59358871e3a85b2c7a69925e9a9e7cd2fce4d1c94d597072
|
7
|
+
data.tar.gz: f54bf0bfe9231a8965acd066f650ef7c048aa527c84e88da78ab6a663dcbf07db43ce54c2f6605cfac355d0db387741439ac3129e72adf27b085d5b2019d30c6
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 rjurado
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# RailsJwtAuthOmniauth
|
2
|
+
|
3
|
+
Addon for rails_jwt_auth gem. Add omniauth capabilities to gem
|
4
|
+
|
5
|
+
> This gem require rails_jwt_auth 2.x version
|
6
|
+
|
7
|
+
## Table of Contents
|
8
|
+
|
9
|
+
- [Installation](#installation)
|
10
|
+
- [Configuration](#configuration)
|
11
|
+
- [Modules](#modules)
|
12
|
+
- [ORMs support](#orms-support)
|
13
|
+
- [Omniauth](#omniauth)
|
14
|
+
- [Testing](#testing-rspec)
|
15
|
+
- [License](#license)
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'rails_jwt_auth'
|
23
|
+
gem 'rails_jwt_auth_omniauth'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
$ bundle
|
30
|
+
```
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
```bash
|
35
|
+
$ gem install rails_jwt_auth_omniauth
|
36
|
+
```
|
37
|
+
|
38
|
+
## Configuration
|
39
|
+
|
40
|
+
You can edit configuration options into `config/initializers/rails_jwt_auth_omniauth.rb` file created by generator.
|
41
|
+
|
42
|
+
| Option | Default value | Description |
|
43
|
+
| ---------------------------------- | ---------------- | ---------------------------------------------------------------------- |
|
44
|
+
|
45
|
+
| omniauth | `nil` | Allow add omniauths providers |
|
46
|
+
|
47
|
+
## Modules
|
48
|
+
|
49
|
+
It's composed of 1 module:
|
50
|
+
|
51
|
+
| Module | Description |
|
52
|
+
| ------------- | --------------------------------------------------------------------------------------------------------------- |
|
53
|
+
| Omniauthable | Allows you to define behaviours for omniauth sessions |
|
54
|
+
|
55
|
+
## ORMs Support
|
56
|
+
|
57
|
+
**ActiveRecord**
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# app/models/user.rb
|
61
|
+
class User < ApplicationRecord
|
62
|
+
include RailsJwtAuth::Authenticatable
|
63
|
+
include RailsJwtAuthOmniath::Omniauthable
|
64
|
+
|
65
|
+
validates :email, presence: true,
|
66
|
+
uniqueness: true,
|
67
|
+
format: URI::MailTo::EMAIL_REGEXP
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
**Mongoid**
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
class User
|
75
|
+
include Mongoid::Document
|
76
|
+
include RailsJwtAuth::Authenticatable
|
77
|
+
include RailsJwtAuthOmniauth::Omniauthable
|
78
|
+
|
79
|
+
field :email, type: String
|
80
|
+
|
81
|
+
validates :email, presence: true,
|
82
|
+
uniqueness: true,
|
83
|
+
format: URI::MailTo::EMAIL_REGEXP
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
## Omniauth
|
88
|
+
|
89
|
+
Allow you to use omniauth providers to login in the platform. Rails_jwt_auth_omniauth will not save `auth_token`
|
90
|
+
from providers and only will create a jwt session.
|
91
|
+
|
92
|
+
To configure omniauth clients:
|
93
|
+
|
94
|
+
Select a provider and define it in your Gemfile and install:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
# Gemfile
|
98
|
+
gem 'omniauth' # Required if omniauth is not dependency in your provider gem
|
99
|
+
gem 'omniauth-google-oauth2'
|
100
|
+
```
|
101
|
+
|
102
|
+
Configuration providers:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
# config/initialize/rails_jwt_auth.rb
|
106
|
+
RailsJwtAuthOmniauth.setup do |config|
|
107
|
+
# ...
|
108
|
+
config.omniauth :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], {
|
109
|
+
provider_ignores_state: true, # this is neccesary for CSRF in extenals requests
|
110
|
+
scope: 'userinfo.email, userinfo.profile'
|
111
|
+
}
|
112
|
+
# You can add multiple omniauth configurations of each provider
|
113
|
+
end
|
114
|
+
```
|
115
|
+
|
116
|
+
In router:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
#cofig/router.rb
|
120
|
+
post '/auth/:provider/callback', to: 'rails_jwt_auth_omniauth/omniauths#callback' # If not use generator
|
121
|
+
```
|
122
|
+
|
123
|
+
In model:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
include RailsJwtAuthOmniauth::Omniauthable
|
127
|
+
def self.from_omniauth(auth)
|
128
|
+
# Define logic to search or create User. This method should return a user to be logged
|
129
|
+
# auth.provider: provider that has processed request
|
130
|
+
# auth['info']: User data from provider
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
In js you will need a library to get auth_code from provider to pass the code to the backend:
|
135
|
+
|
136
|
+
```js
|
137
|
+
// Ej: vue-google-oauth2
|
138
|
+
const authCode = await this.$gAuth.getAuthCode()
|
139
|
+
const response = await this.$http.post(
|
140
|
+
'http://yout-backend-server-api/auth/google_oauth2/callback',
|
141
|
+
{ code: authCode }
|
142
|
+
)
|
143
|
+
```
|
144
|
+
|
145
|
+
## Locales
|
146
|
+
|
147
|
+
Copy `config/locales/en.yml` into your project `config/locales` folder and edit it.
|
148
|
+
|
149
|
+
## License
|
150
|
+
|
151
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RailsJwtAuthOmniauth'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
load 'rails/tasks/statistics.rake'
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
require "rspec/core/rake_task"
|
24
|
+
|
25
|
+
RSpec::Core::RakeTask.new(:spec)
|
26
|
+
|
27
|
+
task :default => :spec
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RailsJwtAuthOmniauth
|
2
|
+
class OmniauthsController < ApplicationController
|
3
|
+
include RailsJwtAuth::RenderHelper
|
4
|
+
|
5
|
+
def callback
|
6
|
+
puts '*' * 100
|
7
|
+
puts auth_hash
|
8
|
+
user = RailsJwtAuth.model_name.constantize.from_omniauth(auth_hash)
|
9
|
+
se = RailsJwtAuthOmniauth::OmniauthSession.new(user)
|
10
|
+
|
11
|
+
if se.generate!
|
12
|
+
render_session se.jwt, se.user
|
13
|
+
else
|
14
|
+
render_422 se.errors.details
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def auth_hash
|
21
|
+
request.env['omniauth.auth']
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RailsJwtAuthOmniauth
|
2
|
+
module Omniauthable
|
3
|
+
class NotImplementedMethod < StandardError; end
|
4
|
+
|
5
|
+
def self.included(klass)
|
6
|
+
klass.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def from_omniauth(_auth)
|
11
|
+
raise NotImplementedMethod.new(
|
12
|
+
I18n.t(
|
13
|
+
'rails_jwt_auth.models.omniauthable.from_omniauth.not_implemented',
|
14
|
+
model:RailsJwtAuth.model
|
15
|
+
)
|
16
|
+
)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module RailsJwtAuthOmniauth
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
initializer(
|
4
|
+
'rails_jwt_auth_omniauth.omniauth',
|
5
|
+
after: :load_config_initializers,
|
6
|
+
before: :build_middleware_stack
|
7
|
+
) do |app|
|
8
|
+
app.middleware.use ActionDispatch::Session::CacheStore
|
9
|
+
RailsJwtAuthOmniauth.omniauth_configs&.each do |provider, config|
|
10
|
+
app.middleware.use config.strategy_class, *config.args do |strategy|
|
11
|
+
config.strategy = strategy
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module RailsJwtAuthOmniauth
|
2
|
+
class StrategyNotFound < NameError
|
3
|
+
def initialize(strategy)
|
4
|
+
@strategy = strategy
|
5
|
+
super("Could not find a strategy with name `#{strategy}'. " \
|
6
|
+
"Please ensure it is required or explicitly set it using the :strategy_class option.")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class OmniauthManager
|
11
|
+
# Based on https://github.com/heartcombo/devise Config
|
12
|
+
|
13
|
+
attr_accessor :strategy
|
14
|
+
attr_reader :args, :options, :provider, :strategy_name
|
15
|
+
|
16
|
+
def initialize(provider, args)
|
17
|
+
@provider = provider
|
18
|
+
@args = args
|
19
|
+
@options = @args.last.is_a?(Hash) ? @args.last : {}
|
20
|
+
@strategy = nil
|
21
|
+
@strategy_class = nil
|
22
|
+
@strategy_name = options[:name] || @provider
|
23
|
+
end
|
24
|
+
|
25
|
+
def strategy_class
|
26
|
+
@strategy_class ||= ::OmniAuth.strategies.find do |strategy|
|
27
|
+
strategy.to_s =~ /#{::OmniAuth::Utils.camelize(strategy_name)}$/ ||
|
28
|
+
strategy.default_options[:name] == strategy_name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rails_jwt_auth/session'
|
2
|
+
|
3
|
+
module RailsJwtAuthOmniauth
|
4
|
+
class OmniauthSession < RailsJwtAuth::Session
|
5
|
+
|
6
|
+
attr_reader :user, :errors, :jwt
|
7
|
+
|
8
|
+
def initialize(user)
|
9
|
+
@user = user
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
validate!
|
14
|
+
|
15
|
+
!errors?
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate!
|
19
|
+
if valid?
|
20
|
+
@user.clean_reset_password if recoverable?
|
21
|
+
@user.clean_lock if lockable?
|
22
|
+
@user.load_auth_token
|
23
|
+
|
24
|
+
unless user.save
|
25
|
+
add_error(RailsJwtAuth.model_name.underscore, :invalid)
|
26
|
+
|
27
|
+
return false
|
28
|
+
end
|
29
|
+
|
30
|
+
generate_jwt(nil)
|
31
|
+
|
32
|
+
true
|
33
|
+
else
|
34
|
+
@user.failed_attempt if lockable?
|
35
|
+
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def validate!
|
43
|
+
# Can't use ActiveModel::Validations since we have dynamic fields
|
44
|
+
@errors = Errors.new({})
|
45
|
+
|
46
|
+
validate_user
|
47
|
+
validate_user_is_confirmed if confirmable?
|
48
|
+
validate_user_is_not_locked if lockable?
|
49
|
+
validate_custom
|
50
|
+
end
|
51
|
+
|
52
|
+
def validate_user
|
53
|
+
add_error(:session, :not_found) if @user.blank?
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails_jwt_auth'
|
2
|
+
require 'rails_jwt_auth_omniauth/engine'
|
3
|
+
require 'rails_jwt_auth_omniauth/omniauth_manager'
|
4
|
+
require 'rails_jwt_auth_omniauth/omniauth_session'
|
5
|
+
|
6
|
+
module RailsJwtAuthOmniauth
|
7
|
+
mattr_accessor :omniauth_configs
|
8
|
+
self.omniauth_configs = {}
|
9
|
+
|
10
|
+
def self.setup
|
11
|
+
yield self
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.omniauth(provider, *args)
|
15
|
+
omniauth_configs[provider] = RailsJwtAuthOmniauth::OmniauthManager.new(provider, args)
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_jwt_auth_omniauth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jmjurado23
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-01-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails_jwt_auth
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: omniauth
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
description: Addon for rails_jwt_auth gem. Add omniauth capabilities to gem
|
42
|
+
email:
|
43
|
+
- jmjurado23@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- app/controllers/rails_jwt_auth_omniauth/omniauths_controller.rb
|
52
|
+
- app/models/concerns/rails_jwt_auth_omniauth/omniauthable.rb
|
53
|
+
- config/locales/en.yml
|
54
|
+
- lib/rails_jwt_auth_omniauth.rb
|
55
|
+
- lib/rails_jwt_auth_omniauth/engine.rb
|
56
|
+
- lib/rails_jwt_auth_omniauth/omniauth_manager.rb
|
57
|
+
- lib/rails_jwt_auth_omniauth/omniauth_session.rb
|
58
|
+
- lib/rails_jwt_auth_omniauth/version.rb
|
59
|
+
homepage: https://github.com/jmjurado23/rails_jwt_auth_omniauth
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.2.31
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Rails jwt authentication Omniauthable.
|
82
|
+
test_files: []
|