Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rhatherall committed Dec 7, 2023
0 parents commit 5757bc7
Show file tree
Hide file tree
Showing 86 changed files with 1,986 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- "2.7.8"
- "3.0.6"
- "3.1.4"
- "3.2.2"

steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/.bundle/
/doc/
/log/*.log
/pkg/
/tmp/
/spec/dummy/db/*.sqlite3
/spec/dummy/db/*.sqlite3-*
/spec/dummy/log/*.log
/spec/dummy/storage/
/spec/dummy/tmp/

Gemfile.lock
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2023-11-29

- Initial release
84 changes: 84 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our community include:

* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall community

Examples of unacceptable behavior include:

* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.

Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [email protected]. All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series of actions.

**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.

**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within the community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.

Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
18 changes: 18 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

# Specify your gem's dependencies in cognito_idp_rails.gemspec.
gemspec

gem "puma"

gem "sqlite3"

gem "sprockets-rails"

# Start debugger with binding.b [https://github.com/ruby/debug]
# gem "debug", ">= 1.0.0"

gem "rspec-rails", "~> 6.1"

gem "standard", "~> 1.32"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Appercept Limited

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# CognitoIdpRails

Simple integration of Amazon Cognito IdP (User Pools) for Rails applications.

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add cognito_idp_rails

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install cognito_idp_rails

## Usage

After adding the gem to your application, run the install generator:

$ rails generate cognito_idp:install

This generator will add `cognito_idp` to your routes and install an initializer at `config/initializers/cognito_idp.rb`.

Be sure to review and edit the initialized to configure options for your Amazon Cognito User Pool configuration. You
must also provide an implementation for the `on_valid_login` function in the initializer appropriate for any actions you
want to take when a user signed in.

## Development

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.

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).

## Contributing

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).

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

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).
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "bundler/setup"

APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
load "rails/tasks/engine.rake"

load "rails/tasks/statistics.rake"

require "bundler/gem_tasks"
1 change: 1 addition & 0 deletions app/assets/config/cognito_idp_rails_manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//= link_directory ../stylesheets/cognito_idp_rails .css
Empty file.
15 changes: 15 additions & 0 deletions app/assets/stylesheets/cognito_idp_rails/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
4 changes: 4 additions & 0 deletions app/controllers/cognito_idp_rails/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module CognitoIdpRails
class ApplicationController < ActionController::Base
end
end
61 changes: 61 additions & 0 deletions app/controllers/cognito_idp_rails/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "cognito_idp"

module CognitoIdpRails
class SessionsController < ApplicationController
before_action :verify_state, only: [:login_callback]

def login
redirect_to authorization_url, allow_other_host: true
end

def login_callback
client.get_token(grant_type: :authorization_code, code: params[:code], redirect_uri: auth_login_callback_url) do |token|
client.get_user_info(token) do |user_info|
reset_session
configuration.on_valid_login.call(token, user_info, session)
redirect_to configuration.after_login_route, notice: "You have been successfully logged in."
return
end
end
redirect_to configuration.after_login_route, notice: "Login failed."
end

def logout
redirect_to client.logout_uri(logout_uri: auth_logout_callback_url), allow_other_host: true
end

def logout_callback
configuration.on_logout.call(session)
reset_session
redirect_to configuration.after_logout_route, notice: "You have been successfully logged out."
end

private

def authorization_url
client.authorization_uri(redirect_uri: auth_login_callback_url, scope: scope, state: login_state)
end

def client
CognitoIdpRails.client
end

def configuration
CognitoIdpRails.configuration
end

def scope
configuration.scope
end

def login_state
session[:login_state] ||= SecureRandom.urlsafe_base64
end

def verify_state
return if params[:state] == login_state

redirect_to configuration.after_login_route, notice: "Login failed."
end
end
end
Empty file added app/controllers/concerns/.keep
Empty file.
4 changes: 4 additions & 0 deletions app/helpers/cognito_idp_rails/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module CognitoIdpRails
module ApplicationHelper
end
end
4 changes: 4 additions & 0 deletions app/jobs/cognito_idp_rails/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module CognitoIdpRails
class ApplicationJob < ActiveJob::Base
end
end
6 changes: 6 additions & 0 deletions app/mailers/cognito_idp_rails/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module CognitoIdpRails
class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
layout "mailer"
end
end
5 changes: 5 additions & 0 deletions app/models/cognito_idp_rails/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CognitoIdpRails
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
end
Empty file added app/models/concerns/.keep
Empty file.
15 changes: 15 additions & 0 deletions app/views/layouts/cognito_idp_rails/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Cognito idp rails</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "cognito_idp_rails/application", media: "all" %>
</head>
<body>

<%= yield %>

</body>
</html>
26 changes: 26 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path("..", __dir__)
ENGINE_PATH = File.expand_path("../lib/cognito_idp_rails/engine", __dir__)
APP_PATH = File.expand_path("../spec/dummy/config/application", __dir__)

# Set up gems listed in the Gemfile.
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
# require "rails/test_unit/railtie"
require "rails/engine/commands"
Loading

0 comments on commit 5757bc7

Please sign in to comment.