contrib-auth 0.3.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb10ef1984df8036c7cc625c7560808a2da9e8613a953fbf3b2b715dbccab364
4
+ data.tar.gz: 759d43d360a3c22b074963a41e5f69b9899dfb509b7db106da49a480cc7a3139
5
+ SHA512:
6
+ metadata.gz: 41467b432ea48e15fd651813769e525b57e1af206e07c9afb628244a6b3dd71e11d1cd6e73322b07c9129d456544a00ee3211e6871a89b4523dc4ee77bdad2cc
7
+ data.tar.gz: 9076f1eead44bc2ea93994484352ba77dd52558f45ff03d1fbb13ae626825a15f90540e50ef24fd510e1c81762f4625a10c0a9d856e7580ab7cfe28dc4d87132
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021
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,71 @@
1
+ # Contrib::Auth
2
+ Plug-n-play vendor agnostic Rails authentication engine API. (Working in progress, not ready for production environments)
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'contrib-auth'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Setup the provider:
17
+
18
+ ```ruby
19
+ Contrib::Auth.configure do |config|
20
+ config.provider = Contrib::Auth::Provider::GoogleAuth.new(YOUR_API_KEY)
21
+ end
22
+ ```
23
+
24
+ Mount the engine in your app:
25
+
26
+ ```ruby
27
+ Rails.application.routes.draw do
28
+ mount Contrib::Auth::Engine => "/auth"
29
+ end
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ Once installed, the `contrib-auth` will expose authentication endpoints in your application. For instance:
35
+
36
+ ```bash
37
+ curl http://localhost:3000/auth/sign_in_with_password -d '{"email":"nandosousafr@gmail.com","password":"batatinha1234"}' -H 'Content-Type: application/json'
38
+ ```
39
+
40
+ If the credentials are correct, the API will respond `200` with the following response:
41
+
42
+ ```json
43
+ {
44
+ "id_token": "[ID_TOKEN]",
45
+ "refresh_token": "[REFRESH_TOKEN]",
46
+ "expires_in": "3600",
47
+ }
48
+ ```
49
+
50
+ ## Supported providers
51
+
52
+ | Provider | Status |
53
+ | ---------------------------------------------------|:-------------------:|
54
+ | [Firebase](https://firebase.google.com/docs/auth) | Working in progress |
55
+ | Auth0 | Pending |
56
+ | Devise | Pending |
57
+
58
+ ## Contributing
59
+
60
+ Requirements:
61
+ - Docker
62
+
63
+ There are some make tasks available to quickly start working on this project:
64
+
65
+ | Command | Description |
66
+ | ---------------------------------------------------|:------------------------:|
67
+ | make build | Build the docker image |
68
+ | make bash | Access the bash inside the container |
69
+
70
+ ## License
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/setup"
2
+
3
+ load "rails/tasks/statistics.rake"
4
+
5
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/contrib/auth .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,6 @@
1
+ module Contrib
2
+ module Auth
3
+ class ApplicationController < ActionController::API
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ module Contrib
2
+ module Auth
3
+ class AuthenticationController < ApplicationController
4
+ def sign_in_with_password
5
+ @response = Contrib::Auth.api.sign_in_with_password(
6
+ params[:email_or_username],
7
+ params[:password]
8
+ )
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module Contrib
2
+ module Auth
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Contrib
2
+ module Auth
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module Contrib
2
+ module Auth
3
+ class ApplicationMailer < ActionMailer::Base
4
+ default from: 'from@example.com'
5
+ layout 'mailer'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Contrib
2
+ module Auth
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ json.extract!(@response, :id_token, :refresh_token, :expires_in)
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ Contrib::Auth::Engine.routes.draw do
2
+ defaults format: :json do
3
+ post '/sign_in_with_password' => 'authentication#sign_in_with_password'
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ module Contrib
2
+ module Auth
3
+ class Api
4
+ def initialize(provider = nil)
5
+ @provider = provider
6
+ end
7
+
8
+ def sign_in_with_password(email_or_username, password)
9
+ # TODO: Implement retryable
10
+ @provider.sign_in_with_password(email_or_username, password)
11
+ end
12
+
13
+ def reset_password(email_or_username)
14
+ @provider.reset_password(email_or_username)
15
+ end
16
+
17
+ def sign_up_with_email_and_password(email_or_username, password)
18
+ @provider.sign_up_with_email_and_password(email_or_username, password)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module Contrib
2
+ module Auth
3
+ class Configuration
4
+ attr_accessor :provider
5
+
6
+ def initialize(provider = nil)
7
+ self.provider = provider
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Contrib
2
+ module Auth
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Contrib::Auth
5
+
6
+ config.generators do |g|
7
+ g.test_framework :rspec
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,76 @@
1
+ require 'faraday'
2
+
3
+ module Contrib
4
+ module Auth
5
+ module Provider
6
+ class GoogleAuth
7
+ DEFAULT_BASE_ENDPOINT = 'https://identitytoolkit.googleapis.com'.freeze
8
+
9
+ def initialize(api_key, http_client = Faraday.new(DEFAULT_BASE_ENDPOINT))
10
+ @api_key = api_key
11
+ @http_client = http_client
12
+ end
13
+
14
+ # refers to: https://firebase.google.com/docs/reference/rest/auth#section-create-email-password
15
+ def sign_up_with_email_and_password(email_or_username, password)
16
+ response = @http_client.post('/v1/accounts:signUp') do |req|
17
+ req.params[:key] = @api_key
18
+ req.headers['Content-Type'] = 'application/json'
19
+ req.body = JSON.generate(
20
+ email: email_or_username,
21
+ password: password,
22
+ returnSecureToken: true,
23
+ )
24
+ end
25
+
26
+ parsed_response = JSON.parse(response.body)
27
+
28
+ # TODO: Handle errors
29
+ Contrib::Auth::Provider::Responses::SignUpWithEmailAndPassword.new(
30
+ id_token: parsed_response['idToken'],
31
+ refresh_token: parsed_response['refreshToken'],
32
+ expires_in: parsed_response['expiresIn'],
33
+ )
34
+ end
35
+
36
+ # refers to: https://firebase.google.com/docs/reference/rest/auth#section-sign-in-email-password
37
+ def sign_in_with_password(email_or_username, password)
38
+ response = @http_client.post('/v1/accounts:signInWithPassword') do |req|
39
+ req.params[:key] = @api_key
40
+ req.headers['Content-Type'] = 'application/json'
41
+ req.body = JSON.generate(
42
+ email: email_or_username,
43
+ password: password,
44
+ returnSecureToken: true,
45
+ )
46
+ end
47
+
48
+ parsed_response = JSON.parse(response.body)
49
+
50
+ # TODO: Handle errors
51
+ Contrib::Auth::Provider::Responses::SignInWithPassword.new(
52
+ id_token: parsed_response['idToken'],
53
+ refresh_token: parsed_response['refreshToken'],
54
+ expires_in: parsed_response['expiresIn'],
55
+ )
56
+ end
57
+
58
+ # refers to: https://firebase.google.com/docs/reference/rest/auth#section-send-password-reset-email
59
+ def reset_password(email_or_username)
60
+ # TODO: X-Firebase-Locale optional header
61
+ response = @http_client.post('/v1/accounts:sendOobCode') do |req|
62
+ req.params[:key] = @api_key
63
+ req.headers['Content-Type'] = 'application/json'
64
+
65
+ req.body = JSON.generate(
66
+ email: email_or_username,
67
+ requestType: 'PASSWORD_RESET',
68
+ )
69
+ end
70
+
71
+ response.success?
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,19 @@
1
+ module Contrib
2
+ module Auth
3
+ module Provider
4
+ module Responses
5
+ class SignInWithPassword
6
+ attr_accessor :id_token
7
+ attr_accessor :refresh_token
8
+ attr_accessor :expires_in
9
+
10
+ def initialize(params = {})
11
+ params.each do |param, key|
12
+ public_send("#{param}=", key)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module Contrib
2
+ module Auth
3
+ module Provider
4
+ module Responses
5
+ class SignUpWithEmailAndPassword
6
+ attr_accessor :id_token
7
+ attr_accessor :refresh_token
8
+ attr_accessor :expires_in
9
+
10
+ def initialize(params = {})
11
+ params.each do |param, key|
12
+ public_send("#{param}=", key)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require 'contrib/auth/provider/responses/sign_in_with_password'
2
+ require 'contrib/auth/provider/responses/sign_up_with_email_and_password'
@@ -0,0 +1,5 @@
1
+ module Contrib
2
+ module Auth
3
+ VERSION = '0.3.1'
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails'
2
+ require 'jbuilder'
3
+
4
+ require 'contrib/auth/api'
5
+ require 'contrib/auth/provider/google_auth'
6
+ require 'contrib/auth/configuration'
7
+ require 'contrib/auth/provider/responses'
8
+ require 'contrib/auth/version'
9
+ require 'contrib/auth/engine'
10
+
11
+ module Contrib
12
+ module Auth
13
+ class << self
14
+ def config
15
+ @@configuration ||= Configuration.new
16
+ end
17
+
18
+ def configure
19
+ yield config if block_given?
20
+ end
21
+
22
+ def api
23
+ @@api ||= Api.new(config.provider)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :contrib_auth do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contrib-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - Contribyard Developers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.4
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.1.4.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.4
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.1.4.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: jbuilder
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ description: Vendor-agnostic authentication component for Rails APIs
62
+ email:
63
+ - developers@contribyard.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - MIT-LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - app/assets/config/contrib_auth_manifest.js
72
+ - app/assets/stylesheets/contrib/auth/application.css
73
+ - app/controllers/contrib/auth/application_controller.rb
74
+ - app/controllers/contrib/auth/authentication_controller.rb
75
+ - app/helpers/contrib/auth/application_helper.rb
76
+ - app/jobs/contrib/auth/application_job.rb
77
+ - app/mailers/contrib/auth/application_mailer.rb
78
+ - app/models/contrib/auth/application_record.rb
79
+ - app/views/contrib/auth/authentication/sign_in_with_password.json.jbuilder
80
+ - config/routes.rb
81
+ - lib/contrib/auth.rb
82
+ - lib/contrib/auth/api.rb
83
+ - lib/contrib/auth/configuration.rb
84
+ - lib/contrib/auth/engine.rb
85
+ - lib/contrib/auth/provider/google_auth.rb
86
+ - lib/contrib/auth/provider/responses.rb
87
+ - lib/contrib/auth/provider/responses/sign_in_with_password.rb
88
+ - lib/contrib/auth/provider/responses/sign_up_with_email_and_password.rb
89
+ - lib/contrib/auth/version.rb
90
+ - lib/tasks/contrib/auth_tasks.rake
91
+ homepage: https://github.com/contribyard/contrib-auth
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ homepage_uri: https://github.com/contribyard/contrib-auth
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.2.22
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Vendor-agnostic authentication component for Rails APIs
115
+ test_files: []