omniauth-cognito-oauth2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1eb39894c2ede6c8519c9765344f7a58cf91795d82589d247916c2356cc76dc
4
+ data.tar.gz: 88ea6744577de37aaef7d74e9fe0044266f629d7417b86d3199b9a286fb6b624
5
+ SHA512:
6
+ metadata.gz: 700bd068fc1373669c23f3541d1e5153deae8baddd6e2e0af7c4018cdaaa0aabc09226d3e2ccd86a78adb4e3560a21300b501ea3fd23d759a332484c608f4bc6
7
+ data.tar.gz: 6d58d5e8f2ffd7d373ebc2f5d41ef61ee5823db98b168a2301d894cfb0968c1ae74c49af3903cf30ef02296ad5ca15d37250d992e1ed24ddef195d27a9ec4d0e
data/.gitignore ADDED
@@ -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/.gitlab-ci.yml ADDED
@@ -0,0 +1,21 @@
1
+ image: "ruby:2.5"
2
+
3
+
4
+ cache:
5
+ paths:
6
+ - vendor/ruby
7
+
8
+ before_script:
9
+ - ruby -v
10
+ - gem install bundler
11
+ - bundle install -j $(nproc) --path vendor # Install dependencies into ./vendor/ruby
12
+
13
+ rubocop:
14
+ script:
15
+ - bundle exec rubocop
16
+
17
+ rspec:
18
+ script:
19
+ - bundle exec rspec spec
20
+
21
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Layout/LineLength:
2
+ Max: 150
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ omniauth-cognito-oauth2 (0.1.0)
5
+ jwt (~> 2.2)
6
+ omniauth-oauth2 (~> 1.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ coderay (1.1.2)
13
+ diff-lcs (1.3)
14
+ faraday (1.0.0)
15
+ multipart-post (>= 1.2, < 3)
16
+ hashie (3.6.0)
17
+ jaro_winkler (1.5.4)
18
+ jwt (2.2.1)
19
+ method_source (0.9.2)
20
+ multi_json (1.14.1)
21
+ multi_xml (0.6.0)
22
+ multipart-post (2.1.1)
23
+ oauth2 (1.4.2)
24
+ faraday (>= 0.8, < 2.0)
25
+ jwt (>= 1.0, < 3.0)
26
+ multi_json (~> 1.3)
27
+ multi_xml (~> 0.5)
28
+ rack (>= 1.2, < 3)
29
+ omniauth (1.9.0)
30
+ hashie (>= 3.4.6, < 3.7.0)
31
+ rack (>= 1.6.2, < 3)
32
+ omniauth-oauth2 (1.6.0)
33
+ oauth2 (~> 1.1)
34
+ omniauth (~> 1.9)
35
+ parallel (1.19.1)
36
+ parser (2.7.0.1)
37
+ ast (~> 2.4.0)
38
+ pry (0.12.2)
39
+ coderay (~> 1.1.0)
40
+ method_source (~> 0.9.0)
41
+ rack (2.0.8)
42
+ rainbow (3.0.0)
43
+ rake (13.0.1)
44
+ rspec (3.9.0)
45
+ rspec-core (~> 3.9.0)
46
+ rspec-expectations (~> 3.9.0)
47
+ rspec-mocks (~> 3.9.0)
48
+ rspec-core (3.9.1)
49
+ rspec-support (~> 3.9.1)
50
+ rspec-expectations (3.9.0)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.9.0)
53
+ rspec-mocks (3.9.1)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.9.0)
56
+ rspec-support (3.9.2)
57
+ rubocop (0.78.0)
58
+ jaro_winkler (~> 1.5.1)
59
+ parallel (~> 1.10)
60
+ parser (>= 2.6)
61
+ rainbow (>= 2.2.2, < 4.0)
62
+ ruby-progressbar (~> 1.7)
63
+ unicode-display_width (>= 1.4.0, < 1.7)
64
+ ruby-progressbar (1.10.1)
65
+ unicode-display_width (1.6.0)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ omniauth-cognito-oauth2!
72
+ pry (~> 0.12)
73
+ rake (~> 13.0)
74
+ rspec (~> 3.9)
75
+ rubocop (~> 0.78)
76
+
77
+ BUNDLED WITH
78
+ 2.1.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Adam Wenham
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,135 @@
1
+ # Omniauth::Cognito::Oauth2
2
+
3
+ Oauth2 strategy for AWS Cognito
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'omniauth-cognito-oauth2'
11
+ ```
12
+ Then `bundle install`
13
+
14
+ ## Setup
15
+
16
+ You will need:
17
+
18
+ - an AWS Cognito user pool
19
+ - a domain setup for your user pool
20
+ - an environment variable on your dev machine which contains your domain - something like `COGNITO_USER_POOL_DOMAIN=https://your_user_pool_domain.auth.us-west-1.amazoncognito.com` for your variable, and then `ENV['COGNITO_USER_POOL_DOMAIN']` in your code
21
+ - an App Client set up for your user pool, exposing at least `openid` and `email`. (Don't set up your user pool client application with a 'Client Secret' because at the moment they don't work and don't allow you to authenticate.)
22
+ - an environment variable on your dev machine which contains your App Client ID - something like `COGNITO_CLIENT_ID=your_app_client_id` for your variable, and then `ENV['COGNITO_CLIENT_ID']` in your code
23
+
24
+ ## Usage
25
+
26
+ Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
27
+
28
+ ```ruby
29
+ Rails.application.config.middleware.use OmniAuth::Builder do
30
+ provider :cognito_oauth2, ENV['COGNITO_CLIENT_ID'], scope: [:openid, :email],
31
+ setup: lambda{ |env|
32
+ env['omniauth.strategy'].options[:client_options].site = ENV['COGNITO_USER_POOL_DOMAIN']
33
+ }
34
+ end
35
+ ```
36
+
37
+ Since Cognito has many different client sites, we are using a lambda to dynamically set the site that you wish to authenticate against. This will be the domain you have setup for your user pool. These is more info in the Authorization Code Grant section in the AWS docs [here](https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-user-pool-oauth-2-0-grants/).
38
+
39
+ You can now access the OmniAuth Cognito OAuth2 URL: `/auth/cognito_oauth2`
40
+
41
+ ## Usage (Devise)
42
+
43
+ First define your application id and secret in `config/initializers/devise.rb`.
44
+
45
+ ```ruby
46
+ config.omniauth :cognito_oauth2, ENV['COGNITO_CLIENT_ID'], scope: [:openid, :email],
47
+ setup: lambda{ |env|
48
+ env['omniauth.strategy'].options[:client_options].site = ENV['COGNITO_USER_POOL_DOMAIN']
49
+ }
50
+ ```
51
+
52
+ NOTE: If you are using this gem with devise with above snippet in `config/initializers/devise.rb` then do not create `config/initializers/omniauth.rb` which will conflict with devise configurations.
53
+
54
+ Then add the following to 'config/routes.rb' so the callback routes are defined.
55
+
56
+ ```ruby
57
+ devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
58
+ ```
59
+
60
+ Make sure your model is omniauthable. Generally this is "/app/models/user.rb"
61
+
62
+ ```ruby
63
+ devise :omniauthable, omniauth_providers: [:cognito_oauth2]
64
+ ```
65
+
66
+ Then make sure your callbacks controller is setup.
67
+
68
+ ```ruby
69
+ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
70
+ def cognito_oauth2
71
+ @user = User.from_omniauth(request.env["omniauth.auth"])
72
+
73
+ if @user.persisted?
74
+ sign_in_and_redirect @user, event: :authentication
75
+ set_flash_message(:notice, :success, kind: "Cognito") if is_navigational_format?
76
+ else
77
+ # session["devise.cognito_data"] = request.env["omniauth.auth"].except("extra", "provider") # causes cookie overflow - consider creating a session table in your DB to store large session info https://stackoverflow.com/questions/9473808/cookie-overflow-in-rails-application
78
+ set_flash_message(:alert, :failure, kind: "Cognito")
79
+ redirect_to new_user_session_path
80
+ end
81
+ end
82
+
83
+ def failure
84
+ redirect_to new_user_session_path
85
+ end
86
+ end
87
+ ```
88
+
89
+ and bind to or create the user
90
+
91
+ ```ruby
92
+ def self.from_omniauth(auth)
93
+ where(email: auth.info.email, uid: auth.uid, provider: "cognito_oauth2").first_or_create! do |user|
94
+ user.email = auth.info.email
95
+ user.password = Devise.friendly_token[0,20]
96
+ user.provider = "cognito_oauth2"
97
+ user.uid = auth.uid
98
+ end
99
+ end
100
+ ```
101
+
102
+ Devise will automatically generate a link for you on their default signup/login view, but you can make your own like this if necessary:
103
+
104
+ ```erb
105
+ <%= link_to "Sign in with Cognito", user_cognito_oauth2_omniauth_authorize_path %>
106
+
107
+ <%# Devise prior 4.1.0: %>
108
+ <%= link_to "Sign in with Cognito", user_omniauth_authorize_path(:cognito_oauth2) %>
109
+ ```
110
+
111
+ An overview is available at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
112
+
113
+ ## Configuration
114
+
115
+ You can configure several options, which you pass in to the `provider` method via a hash:
116
+
117
+ * `scope`: A comma-separated list of permissions you want to request from the user. See the [AWS Cognito docs](https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-user-pool-oauth-2-0-grants/) for a full list of available permissions. Caveats:
118
+ * The `openid` and `email` scopes are used by default. By defining your own `scope`, you override these defaults.
119
+
120
+ * `redirect_uri`: Override the redirect_uri used by the gem.
121
+
122
+ ## Development
123
+
124
+ 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.
125
+
126
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
127
+
128
+ ## Contributing
129
+
130
+ Bug reports and pull requests are welcome on GitHub at https://gitlab.com/felixfortis/omniauth-cognito-oauth2.
131
+
132
+
133
+ ## License
134
+
135
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'omniauth/cognito_oauth2'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require 'pry'
12
+ Pry.start
data/bin/setup ADDED
@@ -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,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth/strategies/cognito_oauth2'
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module CognitoOauth2
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+ require 'omniauth/strategies/oauth2'
5
+ require 'uri'
6
+
7
+ module OmniAuth
8
+ module Strategies
9
+ # Standard requirements for implementing Oauth2
10
+ class CognitoOauth2 < OmniAuth::Strategies::OAuth2
11
+ option :name, 'cognito_oauth2'
12
+
13
+ option :client_options,
14
+ authorize_url: '/oauth2/authorize',
15
+ token_url: '/oauth2/token'
16
+
17
+ uid { raw_info['sub'] }
18
+
19
+ info do
20
+ {
21
+ email: raw_info['email']
22
+ }
23
+ end
24
+
25
+ def raw_info
26
+ @raw_info ||= access_token.get('/oauth2/userInfo').parsed
27
+ end
28
+
29
+ def callback_url
30
+ options[:redirect_uri] || (full_host + script_name + callback_path)
31
+ end
32
+ end
33
+
34
+ class Error < StandardError; end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth/cognito_oauth2'
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path(
4
+ File.join('..', 'lib', 'omniauth', 'cognito_oauth2', 'version'),
5
+ __FILE__
6
+ )
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'omniauth-cognito-oauth2'
10
+ spec.version = OmniAuth::CognitoOauth2::VERSION
11
+ spec.authors = ['Adam Wenham']
12
+ spec.email = ['adamwenham64@gmail.com']
13
+
14
+ spec.summary = 'An Oauth2 strategy that plays well both alone and with devise, based on the google-oauth2 version'
15
+ spec.homepage = 'https://gitlab.com/felixfortis/omniauth-cognito-oauth2'
16
+ spec.license = 'MIT'
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
18
+
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+
21
+ spec.metadata['homepage_uri'] = spec.homepage
22
+ spec.metadata['source_code_uri'] = spec.homepage
23
+
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_runtime_dependency 'jwt', '~> 2.2'
32
+ spec.add_runtime_dependency 'omniauth-oauth2', '~> 1.6'
33
+
34
+ spec.add_development_dependency 'pry', '~> 0.12'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.9'
37
+ spec.add_development_dependency 'rubocop', '~> 0.78'
38
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-cognito-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Wenham
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.78'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.78'
97
+ description:
98
+ email:
99
+ - adamwenham64@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".gitlab-ci.yml"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - lib/omniauth/cognito_oauth2.rb
117
+ - lib/omniauth/cognito_oauth2/version.rb
118
+ - lib/omniauth/strategies/cognito_oauth2.rb
119
+ - lib/omniauth_cognito_oauth2.rb
120
+ - omniauth-cognito-oauth2.gemspec
121
+ homepage: https://gitlab.com/felixfortis/omniauth-cognito-oauth2
122
+ licenses:
123
+ - MIT
124
+ metadata:
125
+ allowed_push_host: https://rubygems.org
126
+ homepage_uri: https://gitlab.com/felixfortis/omniauth-cognito-oauth2
127
+ source_code_uri: https://gitlab.com/felixfortis/omniauth-cognito-oauth2
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 2.3.0
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.0.6
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: An Oauth2 strategy that plays well both alone and with devise, based on the
147
+ google-oauth2 version
148
+ test_files: []