cognito_idp_rails 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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/app/assets/config/cognito_idp_rails_manifest.js +1 -0
- data/app/assets/stylesheets/cognito_idp_rails/application.css +15 -0
- data/app/controllers/cognito_idp_rails/application_controller.rb +4 -0
- data/app/controllers/cognito_idp_rails/sessions_controller.rb +61 -0
- data/app/helpers/cognito_idp_rails/application_helper.rb +4 -0
- data/app/jobs/cognito_idp_rails/application_job.rb +4 -0
- data/app/mailers/cognito_idp_rails/application_mailer.rb +6 -0
- data/app/models/cognito_idp_rails/application_record.rb +5 -0
- data/app/views/layouts/cognito_idp_rails/application.html.erb +15 -0
- data/config/routes.rb +2 -0
- data/lib/cognito_idp_rails/configuration.rb +13 -0
- data/lib/cognito_idp_rails/engine.rb +11 -0
- data/lib/cognito_idp_rails/routing/mapper_extensions.rb +12 -0
- data/lib/cognito_idp_rails/version.rb +3 -0
- data/lib/cognito_idp_rails.rb +29 -0
- data/lib/generators/cognito_idp_rails/install_generator.rb +18 -0
- data/lib/generators/cognito_idp_rails/templates/cognito_idp_rails_initializer.rb.tt +17 -0
- data/lib/tasks/cognito_idp_rails_tasks.rake +2 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f89e3021ee553c158133054d9b09a94fe02a0b0a490b462947a2b974df4cc0a9
|
4
|
+
data.tar.gz: 4984a4e7baffdce01541c67295575ba6dc518c53c94d66854dce6d51eabe494e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5df85e9025803085b8aabb27a7b389219e87b6d817744ff7045f3075e1eef54b468288f387d559164d895faee2d34a9f17d78a43d8238f8b59e86d7dfe5ee40f
|
7
|
+
data.tar.gz: 85bc14d48e72a38fee9ffdc11e0fbcd3768a39aaebcb8c0fbd45076c26bb254ec01dc006d3556f9e652b7a401bd993e4823fe40771f75d826c87cf68bbf43222
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Appercept Limited
|
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,43 @@
|
|
1
|
+
# CognitoIdpRails
|
2
|
+
|
3
|
+
Simple integration of Amazon Cognito IdP (User Pools) for Rails applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add cognito_idp_rails
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install cognito_idp_rails
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
After adding the gem to your application, run the install generator:
|
18
|
+
|
19
|
+
$ rails generate cognito_idp:install
|
20
|
+
|
21
|
+
This generator will add `cognito_idp` to your routes and install an initializer at `config/initializers/cognito_idp.rb`.
|
22
|
+
|
23
|
+
Be sure to review and edit the initializer to configure options for your Amazon Cognito User Pool configuration. You
|
24
|
+
must also provide an implementation for the `on_valid_login` function in the initializer appropriate for any actions you
|
25
|
+
want to take when a user signed in.
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/appercept/cognito_idp_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/appercept/cognito_idp_rails/blob/main/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the CognitoIdpRails project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/appercept/cognito_idp_rails/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
//= link_directory ../stylesheets/cognito_idp_rails .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,61 @@
|
|
1
|
+
require "cognito_idp"
|
2
|
+
|
3
|
+
module CognitoIdpRails
|
4
|
+
class SessionsController < ApplicationController
|
5
|
+
before_action :verify_state, only: [:login_callback]
|
6
|
+
|
7
|
+
def login
|
8
|
+
redirect_to authorization_url, allow_other_host: true
|
9
|
+
end
|
10
|
+
|
11
|
+
def login_callback
|
12
|
+
client.get_token(grant_type: :authorization_code, code: params[:code], redirect_uri: auth_login_callback_url) do |token|
|
13
|
+
client.get_user_info(token) do |user_info|
|
14
|
+
reset_session
|
15
|
+
configuration.on_valid_login.call(token, user_info, session)
|
16
|
+
redirect_to configuration.after_login_route, notice: "You have been successfully logged in."
|
17
|
+
return
|
18
|
+
end
|
19
|
+
end
|
20
|
+
redirect_to configuration.after_login_route, notice: "Login failed."
|
21
|
+
end
|
22
|
+
|
23
|
+
def logout
|
24
|
+
redirect_to client.logout_uri(logout_uri: auth_logout_callback_url), allow_other_host: true
|
25
|
+
end
|
26
|
+
|
27
|
+
def logout_callback
|
28
|
+
configuration.on_logout.call(session)
|
29
|
+
reset_session
|
30
|
+
redirect_to configuration.after_logout_route, notice: "You have been successfully logged out."
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def authorization_url
|
36
|
+
client.authorization_uri(redirect_uri: auth_login_callback_url, scope: scope, state: login_state)
|
37
|
+
end
|
38
|
+
|
39
|
+
def client
|
40
|
+
CognitoIdpRails.client
|
41
|
+
end
|
42
|
+
|
43
|
+
def configuration
|
44
|
+
CognitoIdpRails.configuration
|
45
|
+
end
|
46
|
+
|
47
|
+
def scope
|
48
|
+
configuration.scope
|
49
|
+
end
|
50
|
+
|
51
|
+
def login_state
|
52
|
+
session[:login_state] ||= SecureRandom.urlsafe_base64
|
53
|
+
end
|
54
|
+
|
55
|
+
def verify_state
|
56
|
+
return if params[:state] == login_state
|
57
|
+
|
58
|
+
redirect_to configuration.after_login_route, notice: "Login failed."
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Cognito idp rails</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
<%= csp_meta_tag %>
|
7
|
+
|
8
|
+
<%= stylesheet_link_tag "cognito_idp_rails/application", media: "all" %>
|
9
|
+
</head>
|
10
|
+
<body>
|
11
|
+
|
12
|
+
<%= yield %>
|
13
|
+
|
14
|
+
</body>
|
15
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module CognitoIdpRails
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :after_login_route, :after_logout_route, :domain, :client_id,
|
4
|
+
:client_secret, :on_logout, :on_valid_login, :scope
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@after_login_route = "/"
|
8
|
+
@after_logout_route = "/"
|
9
|
+
@on_valid_login = lambda { |token, user_info, session| }
|
10
|
+
@on_logout = lambda { |session| }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module CognitoIdpRails
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
initializer "cognito_idp_rails.add_routing_paths" do |app|
|
4
|
+
ActionDispatch::Routing::Mapper.send(:include, CognitoIdpRails::Routing::MapperExtensions)
|
5
|
+
end
|
6
|
+
|
7
|
+
config.generators do |g|
|
8
|
+
g.test_framework :rspec
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module CognitoIdpRails
|
2
|
+
module Routing
|
3
|
+
module MapperExtensions
|
4
|
+
def cognito_idp
|
5
|
+
get("/login", to: "cognito_idp_rails/sessions#login")
|
6
|
+
get("/auth/login_callback", to: "cognito_idp_rails/sessions#login_callback")
|
7
|
+
get("/logout", to: "cognito_idp_rails/sessions#logout")
|
8
|
+
get("/auth/logout_callback", to: "cognito_idp_rails/sessions#logout_callback")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "cognito_idp_rails/engine"
|
2
|
+
require "cognito_idp_rails/version"
|
3
|
+
require "cognito_idp"
|
4
|
+
|
5
|
+
module CognitoIdpRails
|
6
|
+
autoload :Configuration, "cognito_idp_rails/configuration"
|
7
|
+
|
8
|
+
module Routing
|
9
|
+
autoload :MapperExtensions, "cognito_idp_rails/routing/mapper_extensions"
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def client
|
14
|
+
@client ||= CognitoIdp::Client.new(
|
15
|
+
client_id: configuration.client_id,
|
16
|
+
client_secret: configuration.client_secret,
|
17
|
+
domain: configuration.domain
|
18
|
+
)
|
19
|
+
end
|
20
|
+
|
21
|
+
def configuration
|
22
|
+
@configuration ||= Configuration.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def configure
|
26
|
+
yield(configuration)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module CognitoIdpRails
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
desc "Add an initializer and routes for Cognito IdP to your app"
|
7
|
+
source_root File.expand_path("templates", __dir__)
|
8
|
+
|
9
|
+
def copy_initializer
|
10
|
+
template "cognito_idp_rails_initializer.rb.tt", "config/initializers/cognito_idp.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_routes
|
14
|
+
route "cognito_idp"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
CognitoIdpRails.configure do |config|
|
2
|
+
config.client_id = ENV["COGNITO_CLIENT_ID"]
|
3
|
+
config.client_secret = ENV["COGNITO_CLIENT_SECRET"]
|
4
|
+
config.domain = ENV["COGNITO_DOMAIN"]
|
5
|
+
config.on_valid_login = lambda do |token, user_info, session|
|
6
|
+
# 1. Find or create a user.
|
7
|
+
# user = User.where(identifier: user_info.sub).find_or_create do |user|
|
8
|
+
# user.email = user_info.email
|
9
|
+
# end
|
10
|
+
|
11
|
+
# 2. Set any session data for the user.
|
12
|
+
# session[:user_id] = user.id
|
13
|
+
end
|
14
|
+
config.on_logout = lambda do |session|
|
15
|
+
# Your last chance to do something before the session is reset.
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cognito_idp_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Richard Hatherall
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cognito_idp
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 7.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 7.0.0
|
41
|
+
description: Simple Rails integration for authentication through Amazon Cognito IdP
|
42
|
+
(User Pools)
|
43
|
+
email:
|
44
|
+
- richard@appercept.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- app/assets/config/cognito_idp_rails_manifest.js
|
53
|
+
- app/assets/stylesheets/cognito_idp_rails/application.css
|
54
|
+
- app/controllers/cognito_idp_rails/application_controller.rb
|
55
|
+
- app/controllers/cognito_idp_rails/sessions_controller.rb
|
56
|
+
- app/helpers/cognito_idp_rails/application_helper.rb
|
57
|
+
- app/jobs/cognito_idp_rails/application_job.rb
|
58
|
+
- app/mailers/cognito_idp_rails/application_mailer.rb
|
59
|
+
- app/models/cognito_idp_rails/application_record.rb
|
60
|
+
- app/views/layouts/cognito_idp_rails/application.html.erb
|
61
|
+
- config/routes.rb
|
62
|
+
- lib/cognito_idp_rails.rb
|
63
|
+
- lib/cognito_idp_rails/configuration.rb
|
64
|
+
- lib/cognito_idp_rails/engine.rb
|
65
|
+
- lib/cognito_idp_rails/routing/mapper_extensions.rb
|
66
|
+
- lib/cognito_idp_rails/version.rb
|
67
|
+
- lib/generators/cognito_idp_rails/install_generator.rb
|
68
|
+
- lib/generators/cognito_idp_rails/templates/cognito_idp_rails_initializer.rb.tt
|
69
|
+
- lib/tasks/cognito_idp_rails_tasks.rake
|
70
|
+
homepage: https://github.com/appercept/cognito_idp_rails
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata:
|
74
|
+
homepage_uri: https://github.com/appercept/cognito_idp_rails
|
75
|
+
source_code_uri: https://github.com/appercept/cognito_idp_rails
|
76
|
+
changelog_uri: https://github.com/appercept/cognito_idp_rails/CHANGELOG.md
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.4.10
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Simple Rails integration for Amazon Cognito IdP (User Pools)
|
96
|
+
test_files: []
|