Skip to content

Commit 739fe60

Browse files
committed
Adding http_auth for staging environments
1 parent b083196 commit 739fe60

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

README.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This is a boilerplate to build your next SaaS product. It's a RubyOnRails 6 back
1010
- Postgresql Server as db connector
1111

1212
## Dependencies
13-
This boilerplate works like a charm with the following gems:
13+
This boilerplate works like a charm with the following gemset:
1414
- pg
1515
- devise
1616
- graphql
@@ -60,7 +60,7 @@ rake db:seed
6060

6161
Run the development server:
6262

63-
```
63+
```sh
6464
rails s
6565
```
6666

@@ -78,6 +78,8 @@ The app uses a postgresql database. It implements the connector with the gem `pg
7878
### 2. Authentication
7979
The app uses [devise](https://github.com/plataformatec/devise)'s logic for authentication. For graphQL API we use the JWT token, but to access the rails_admin backend we use standard devise views, but registration is excluded.
8080

81+
Change devise settins under `config/initializers/devise.rb` and `config/initializers/graphql_auth.rb`.
82+
8183
### 3. JSON Web Token
8284
[graphql-auth](https://github.com/o2web/graphql-auth) is a graphql/devise extension which uses JWT tokens for user authentication. It follows [secure by default](https://en.wikipedia.org/wiki/Secure_by_default) principle.
8385

@@ -102,11 +104,37 @@ Annotates Rails/ActiveRecord Models, routes, fixtures, and others based on the d
102104
Start defining your abilities under `app/models/aility.rb`.
103105

104106
### 10. Rails Admin
105-
To access the data of your application you can access the [rails_admin](https://github.com/sferik/rails_admin) dashboard under route `/admin`. It's currently only allowed for users with role superadmin.
107+
To access the data of your application you can access the [rails_admin](https://github.com/sferik/rails_admin) dashboard under route `http://0.0.0.0:3000/admin`. Access is currently only allowed for users with superadmin role.
106108

107109
If you want to give your admin interface a custom branding you can override sass variables or write your own css under `app/assets/stylesheets/rails_admin/custom`.
108110

109-
### 11. Testing
111+
Change rails_admin settins under `config/initializers/rails_admin.rb`.
112+
113+
### 11. I18n
114+
This app has the default language `en` and already set a secondary language `de`. We included the [rails-i18n](https://github.com/svenfuchs/rails-i18n) to support other languages out of the box. Add more languages under `config/initializers/locale.rb`.
115+
116+
#### Setting locale
117+
To switch locale just append `?locale=de` at the end of your url.
118+
119+
#### Devise
120+
For devise we use [devise-i18n](https://github.com/tigrish/devise-i18n) to support other languages.
121+
122+
Change translations under `config/locales/devise`.If you want to support more languages install them with `rails g devise:i18n:locale fr`. (<-- installs French)
123+
124+
#### Rails Admin
125+
To get translations for rails admin out of the box we use [rails_admin-i18n](https://github.com/starchow/rails_admin-i18n).
126+
127+
#### Testing Locales
128+
How to test your locale files and how to find missing one read [this](https://github.com/svenfuchs/rails-i18n#testing-your-locale-file).
129+
130+
### 12. HTTP Authentication
131+
For your staging environment we recommend to use a HTTP Auth protection. To enable it set env var `IS_HTTP_AUTH_PROTECTED` to `true`.
132+
133+
Set user with `HTTP_AUTH_USER` and password with `HTTP_AUTH_PASSWORD`.
134+
135+
We enable HTTP auth currently for all controllers. The `ApplicationController` class includes the concern `HttpAuth`. Feel free to change it.
136+
137+
### 13. Testing
110138

111139
We are using the wonderful framework [rspec](https://github.com/rspec/rspec). The testsuit also uses [factory_bot_rails](https://github.com/thoughtbot/factory_bot_rails) for fixtures.
112140

@@ -124,15 +152,18 @@ Create fake data easily with [faker gem](https://github.com/faker-ruby/faker). C
124152
#### Simplecov
125153
[SimpleCov](https://github.com/simplecov-ruby/simplecov) is a code coverage analysis tool for Ruby. It uses Ruby's built-in Coverage library to gather code coverage data, but makes processing its results much easier by providing a clean API to filter, group, merge, format, and display those results, giving you a complete code coverage suite that can be set up with just a couple lines of code.
126154

127-
Access results with `$ open /coverage/index.html`.
155+
Open test coverage results with
128156

157+
```sh
158+
$ open /coverage/index.html
159+
```
129160

130-
### 12. Linter with Rubocop
161+
### 14. Linter with Rubocop
131162

132163
We are using the wonderful [rubocop](https://github.com/rubocop-hq/rubocop-rails) to lint and autofix the code. Install the rubocop VSCode extension to get best experience during development.
133164

134165

135-
### 13. Deployment
166+
### 15. Deployment
136167
The project runs on every webhoster with ruby installed. The only dependency is a PostgreSQL database. Create a block `production:` in the`config/database.yml` for your connection.
137168

138169
#### Heroku
@@ -141,7 +172,6 @@ The project runs on every webhoster with ruby installed. The only dependency is
141172

142173
Choose the one click installer or push a clone of this repo to heroku by yourself. We added a `Profile` to the project and heroku run the `release:` statement after deploying a new version. Heroku will automatically set the db settings for your project, so there is nothing to do in `config/database.yml`.
143174

144-
145175
**Make sure all ENV vars are set and the database settings are valid.**
146176

147177
#### Bitbucket Pipelines
@@ -151,14 +181,10 @@ Make sure to set ENV vars `$HEROKU_API_KEY` and `$HEROKU_APP_NAME` in bitbuckets
151181

152182
The pipeline has 2 environments: staging and production. Staging pipline is getting triggered in `develop` branch. Production deploy triggered by `master` branch.
153183

154-
It also triggers pipeline when opening a PR.
184+
It also triggers pipeline while opening a PR.
155185

156186
## What's missing?
157-
158-
* We want to move to https://github.com/o2web/graphql-auth
159-
160-
161-
Feel free to join development!
187+
Feel free to make feature requrest or join development!
162188

163189
## Author
164190

app/controllers/application_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Base controlller for application
44
class ApplicationController < ActionController::Base
5+
include HttpAuth
6+
57
force_ssl if: :ssl_configured?
68

79
rescue_from CanCan::AccessDenied do |exception|
@@ -12,6 +14,13 @@ class ApplicationController < ActionController::Base
1214
end
1315
end
1416

17+
# setting locale from URL parameter
18+
around_action :switch_locale
19+
def switch_locale(&action)
20+
locale = params[:locale] || I18n.default_locale
21+
I18n.with_locale(locale, &action)
22+
end
23+
1524
protected
1625

1726
def current_superadmin

app/controllers/concerns/.keep

Whitespace-only changes.

app/controllers/concerns/http_auth.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module HttpAuth
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
before_action :http_authenticate
6+
end
7+
8+
protected
9+
def http_authenticate
10+
return true unless ENV['IS_HTTP_AUTH_PROTECTED'] == 'true'
11+
12+
authenticate_or_request_with_http_basic do |username, password|
13+
username == ENV['HTTP_AUTH_USER'] && password == ENV['HTTP_AUTH_PASSWORD']
14+
end
15+
end
16+
end

env_sample

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ ADMIN_PASSWORD=demo123
77
ADMIN_FIRST_NAME=John
88
DEVISE_JWT_SECRET_KEY=replace-this-key-with-a-secret
99
DEVISE_SECRET_KEY_BASE=replace-this-key-with-a-secret
10-
10+
11+
IS_HTTP_AUTH_PROTECTED=false
12+
HTTP_AUTH_USER=demo
13+
HTTP_AUTH_PASSWORD=demo1234

0 commit comments

Comments
 (0)