Skip to content

Commit 5fa7d8f

Browse files
authored
Add Ability to Override the Base URL of Generated Links (#330)
* Add Ability to Override the Base URL of Generated Links * Update config documentation * Fix wording
1 parent ea81a31 commit 5fa7d8f

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

Configuration.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ export PWP__MAIL__MAILER_SENDER='"Spiderman" <thespider@mycompany.org>'
9393
* See also this [Github discussion](https://github.com/pglombardo/PasswordPusher/issues/265#issuecomment-964432942).
9494
* [External Documentation on mailer configuration](https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration) for the underlying technology if you need more details for configuration issues.
9595

96-
# Forcing SSL Links
97-
98-
See also the Proxies section below.
99-
100-
| Environment Variable | Description |
101-
| --------- | ------------------ |
102-
| FORCE_SSL | The existence of this variable will set `config.force_ssl` to `true` and generate HTTPS based secret URLs
103-
10496
# Google Analytics
10597

10698
| Environment Variable | Description |
@@ -109,10 +101,24 @@ See also the Proxies section below.
109101
| GA_ACCOUNT | The Google Analytics account id. E.g. `UA-XXXXXXXX-X` |
110102
| GA_DOMAIN | The domain where the application is hosted. E.g. `pwpush.com` |
111103

104+
# Forcing SSL Links
105+
106+
See also the Proxies section below.
107+
108+
| Environment Variable | Description |
109+
| --------- | ------------------ |
110+
| FORCE_SSL | The existence of this variable will set `config.force_ssl` to `true` and generate HTTPS based secret URLs
111+
112112
# Proxies
113113

114114
An occasional issue is that when using Password Pusher behind a proxy, the generated secret URLs are incorrect. They often have the backend URL & port instead of the public fully qualified URL - or use HTTP instead of HTTPS (or all of the preceding).
115115

116116
To resolve this, make sure your proxy properly forwards the `X-Forwarded-Host`, `X-Forwarded-Port` and `X-Forwarded-Proto` headers.
117117

118118
The values in these headers represent the front end request. When these headers are sent, Password Pusher can then build the correct URLs.
119+
120+
If you are unable to have these headers passed to the application for any reason, you could instead force an override of the base URL using the `PWP__OVERRIDE_BASE_URL` environment variable.
121+
122+
| Environment Variable | Description | Example Value |
123+
| --------- | ------------------ | --- |
124+
| PWP__OVERRIDE_BASE_URL | Set this value (without a trailing slash) to force the base URL of generated links. | 'https://subdomain.domain.dev'

app/helpers/passwords_helper.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
module PasswordsHelper
22
def raw_secret_url(password)
33
push_locale = params['push_locale'] || I18n.locale
4-
raw_url = I18n.with_locale(push_locale) do
5-
password_url(password)
4+
5+
if Settings.override_base_url
6+
raw_url = I18n.with_locale(push_locale) do
7+
Settings.override_base_url + password_path(password)
8+
end
9+
else
10+
raw_url = I18n.with_locale(push_locale) do
11+
password_url(password)
12+
end
13+
14+
# Support forced https links with FORCE_SSL env var
15+
raw_url.gsub(/http/i, 'https') if ENV.key?('FORCE_SSL') && !request.ssl?
616
end
717

8-
# Support forced https links with FORCE_SSL env var
9-
raw_url.gsub(/http/i, 'https') if ENV.key?('FORCE_SSL') && !request.ssl?
1018
raw_url
1119
end
1220

config/settings.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ enable_logins: false
1818
# Used in generating fully qualified URLs.
1919
host_protocol: 'https'
2020

21+
# Set the following value to force the base URL of generated links.
22+
#
23+
# Environment variable override:
24+
# PWP__OVERRIDE_BASE_URL='https://pwpush.mydomain.com'
25+
#
26+
# You could even add a port if needed:
27+
# PWP__OVERRIDE_BASE_URL='https://pwpush.mydomain.com:5100'
28+
#
29+
# Set this value without a trailing slash ('/').
30+
#
31+
# override_base_url: 'https://pwpush.mydomain.com'
32+
2133
# When logins are enabled, an SMTP server is required to send emails to users
2234
# for things such as forgot password, unlock account, confirm account etc.
2335
# If `enable_logins` is set to true above, the following _are required_ to be

0 commit comments

Comments
 (0)