Skip to content

Commit

Permalink
Refactor Configuration & Authentication Unit Tests (#8)
Browse files Browse the repository at this point in the history
* #7 major refactor of authorization and token verification

* #7 token verification unit tests

* #7 cleaning up so that a bunch of stuff is placed inside the encrypted credentials.

* #7 updated the README to include information about the secrets that are expected for DEVELOPMENT & PRODUCTION environments.
  • Loading branch information
nadnoslen authored Nov 30, 2018
1 parent 0a8eaf5 commit 48735e8
Show file tree
Hide file tree
Showing 18 changed files with 498 additions and 105 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[*]
charset=utf-8
end_of_line=lf
insert_final_newline=true
indent_style=space
indent_size=2

[*.scss]
indent_style=space
indent_size=2

[{*.yml,*.yaml}]
indent_style=space
indent_size=2

44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ You need the following:

Perform the following from the command line:

1. Create your config/credentials.yml.enc file (see _No Secrets Here - Credentials Instead_ section below).
1. `bundle install` - will install any of the missing gems declared in the `Gemfile`
1. `docker-compose up -d` - start up a Redis and PostgreSQL server in a container
1. `rake db:create` - only required the first time, make sure your database is created
Expand All @@ -26,7 +27,6 @@ Perform the following from the command line:

`rails s` - to serve the API on [http://localhost:3000](http://localhost:3000)


### Database Seeds

For development, feel free to edit the `db/fixtures/development/002_users.rb` file to add yourself.
Expand Down Expand Up @@ -83,29 +83,53 @@ If you're creating Sidekiq jobs please use the generator: `rails g sidekiq:worke

## Configuration Notes

The `config/application.rb` sets the `record_session_activity` boolean which is used to determine whether
we should be logging session activity.
The `config/initializers/ermahgerd.rb` can be used to override a number of configuration options.

The Configuration options are set to their defaults in `lib/ermahgerd/configuration.rb`; check out the
initialize method.

----

## Credentials
## No Secrets Here - Credentials Instead

As of Rails-5.2 secrets are hashed and locked down with the `config/master.key` file. Run `rails credentials:help` for
more information.

Do you need to create a key? Use `rake secret`
This application ships with an already created `config/credentials.yml.enc` and we share the `master.key` amongst
ourselves ... but not with Joe Public (or Josephine Public)

If you're forking this or trying it yourself, you'll want to:

1. `rm config/credentials.yml.enc` to get rid of the current credentials
1. `rails credentials:edit`
1. Add the keys that are described in the section below; don't forget to use the `rake secret` to create your keys

Do you need to edit some secrets? Do it from the command line:
### Keys in `config/credentials.yml.enc`

```bash
$ rails credentials:edit
$ rails credentials:edit # you might have to destroy the existing `config/credentials.yml.enc` if this command fails
```

### Keys in `config/credentials.yml.enc`
`secret_key_base` - used by most Rails apps in one way or another (e.g. BCrypt). Please set this to a
strong key; all environments (development, test, etc.) require this to be set.

`jwk_set` - the set of JWK from Cognito that will be used to decode supplied Authorization tokens. Yours will be found
at `https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json`.
Check out the Cognito docs: [https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html#amazon-cognito-user-pools-using-tokens-step-2]
By default, the TEST environment of this app does not use this JWK set. DEVELOPMENT & PRODUCTION do use this unless
you change the configuration through `config/initializers/ermahgerd.rb`.

`token_aud` - name of the audience in your token from Cognito; makes sure not just any Cognito token
can access this app. You can get this information from your Cognito configuration or the payloads
from your authentication requests to Cognito. By default, the TEST environment of this app does
not use this setting; it makes up a fake audience value. DEVELOPMENT & PRODUCTION do use this unless
you change the configuration through `config/initializers/ermahgerd.rb`.

`secret_key_base` - used by Rails in many ways (e.g. BCrypt)

`jwk_set` - the set of JWK from Cognito that will be used to decode supplied Authorization tokens
`token_iss` - the url that issued the token. You can get this information from your Cognito configuration
or the payloads from your authentication requests to Cognito. By default, the TEST environment of this app does
not use this setting; it makes up a fake audience value. DEVELOPMENT & PRODUCTION do use this unless
you change the configuration through `config/initializers/ermahgerd.rb`.

----

Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/base_jsonapi_resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module Api
module V1
# This is a JSONAPI-Resources ready controller that tests authentication using CognitoAuthorizer concern.
# This is a JSONAPI-Resources ready controller that tests authentication using Ermahgerd::Authorizer concern.
# Authorization is managed through Pundit policies.
class BaseJsonapiResourcesController < ApplicationController
include Ermahgerd::CurrentUser
include JSONAPI::ActsAsResourceController
include CognitoAuthorizer
include Ermahgerd::Authorizer
include Pundit # included for Posterity sake should we override a controller and need to `authorize`

# :authorize_request! is from the CognitoAuthorizer concern ensuring valid `authenticated` requests
Expand All @@ -20,7 +20,7 @@ class BaseJsonapiResourcesController < ApplicationController

# Using the setting in `config/application.rb` determine whether or not to record session activity
def record_session_activity
return unless Rails.configuration.record_session_activity
return unless Ermahgerd.configuration.record_session_activity

# TODO: we need to use the `jti` from the token
# TODO: See https://en.wikipedia.org/wiki/JSON_Web_Token#Standard_fields
Expand Down
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

# Controller that all generated Controllers inherit from.
# The CognitoAuthorizer will provide an `:authorize_request!` hook to ensure supplied tokens are valid.
#
# This controller is configured to rescue from the following errors:
# 1. ActiveRecord::RecordNotFound - that way you can Model.find_by! and have the controller handle the error
Expand Down
53 changes: 0 additions & 53 deletions app/controllers/concerns/cognito_authorizer.rb

This file was deleted.

16 changes: 2 additions & 14 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,13 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.

config.access_token_expire_seconds = ENV.fetch('ACCESS_TOKEN_EXPIRE_SECONDS') { 3600 }

config.access_token_leeway_seconds = ENV.fetch('ACCESS_TOKEN_LEEWAY_SECONDS') { 0 }

config.refresh_token_expire_days = ENV.fetch('REFRESH_TOKEN_EXPIRE_DAYS') { 365 }

# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true

# Whether or not to record `SessionActivity` in the `BaseResourceController`
config.record_session_activity = ENV.fetch('RECORD_SESSION_ACTIVITY') { true }

# The id token's audience
config.token_aud = ENV.fetch('TOKEN_AUD') { 'ermahgerd' }

# The id token's issuer
config.token_iss = ENV.fetch('TOKEN_ISS') { 'https://cognito-idp.ca-central-1.amazonaws.com/us-east-1_example' }
# Ermahgerd Configuration
# ------------------------------------------------------------------------------------------------------------------

config.version = '0.0.1-rc.2'
end
Expand Down
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
HhizsWaU5RfbC43XVL+XCPoBH6VisLZSWKms1RT9BjeIeapwPJ5rYUNyqOw6evT1z+b0+epXTACuz6rjy5LkRm6imxd/kd7CEuD6Vl7RBi5Yn6Ernt3qDqUaHEabOITPFHei2yrR1e1yAE9v0d80QcfH1BOmns5fVxm3wfKOucx/lrtKBZfaDzSjBwYJq/hl2YK5PCdBfRSmn1FNrH+oG4o3t1COuHRbJaqaxeNPTREX9kxJ5xsBj5nUfCxqLkwDbEl8RcsmNGnJpIrVMT7zCJBdSO6fMssDEkzrm1ovFPdWaggxYZob6OTP4b6I3oWsXT13Ai3wYbrSWq9OP4h8q82aHqMUejJsHBBGwAd4OUASJTKWb82xvjXnOuYSt5cMeMuP5gnsILvh2xFXHUPLlT8Xt7/HK6FYFWvjc6i7vBGPuouGA/3jIUsMV7C7EqjhSQtfALKX1bU5q3VIXujMMUwSxj/Gu/V8RSBMY209usWruKt3e+1ZjemI72A5ZPELlUICbEXOcKJCuZ2vIH2C4IMn47TPP8Fm5Sb6suniESlIvK6pNSbJJ5btpa5w9VOqAT4jp0fI9tZ9oRk5xo1KTcf1K0bgxQhsdrig2dPIyjsQMwR35T8c1zs2f1NHpvPzqNmq7xZKZCHqMeoEQWq5OxdRFxtfmtltTw4wDVMMRSEzXAWqkZLF6PsM9SvYpyu1TGOczzolU9vZAhbGzse9k9en8XRiXuSYtBKV5tnfN/yssY/ATEd55Cwz/WUIjTqcpbK1rPp5goYzJBurAEtUa8MXlvedaEAf6bApohLNc23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number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

# Raise exceptions instead of rendering exception templates.
Expand Down
14 changes: 14 additions & 0 deletions config/initializers/ermahgerd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

Ermahgerd.configure do |config|
if Rails.env.test?
# Cognito Token Verification Configuration
# ------------------------------------------------------------------------------------------------------------------

# The id token's audience
config.token_aud = 'ermahgerd'

# The id token's issuer
config.token_iss = 'https://cognito-idp.ca-central-1.amazonaws.com/ca-central-1_exampleXyZ'
end
end
2 changes: 1 addition & 1 deletion db/fixtures/development/002_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
User.seed_once(
:email,
{
email: '[email protected]',
email: 'dan.nelson@cybertooth.io',
first_name: 'Dan',
last_name: 'Nelson',
nickname: 'Hollywood',
Expand Down
6 changes: 6 additions & 0 deletions lib/ermahgerd.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# frozen_string_literal: true

require 'ermahgerd/authorizer'
require 'ermahgerd/configuration'
require 'ermahgerd/current_user'
require 'ermahgerd/errors'

# The Ermahgerd module has a bunch of constants and configuration elements found within.
# There are also a number of very reusable concerns and classes that can be used throughout this project, or
# copied into another project.
module Ermahgerd
HEADER_AUTHORIZATION = 'Authorization'
HEADER_IDENTIFICATION = 'Identification'
end
Loading

0 comments on commit 48735e8

Please sign in to comment.