Skip to content

Commit 19966f5

Browse files
patch0grega
andauthored
Update issuer value to default to the authorization_endpoint (#54)
I was confusing myself when running Hydra v1.11 (rather than v1.9) and decided that the issuer should be based on the `token_endpoint` rather than the `authorization_endpoint`. In 1.11 it is possible to specify the `SELF_PUBLIC_URL` value to split between URLs that should be queried by the browser, and those queried by the hydra client (ie.. the rails app). This helps with discovery, but ends up with the `SELF_ISSUER_URL` being `docker.host.internal` such that the other endpoints are correctly set. This PR simplifies things back to match with v1.9 😅 meaning that we don't have to see the issuer by hand. --------- Co-authored-by: Greg Annandale <greg@raspberrypi.org>
1 parent eff0984 commit 19966f5

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Altered default value of the `issuer` to track the `authorization_endpoint` rather than the `token_endpoint` (#54)
13+
1014
### Fixed
1115

1216
- Ensure `redirect_uri` is set in the OpenID Connect configuration (#53)

README.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RpiAuth.configure do |config|
3838
end
3939
```
4040

41-
The values above will allow you to login using the `gem-dev` client seeded in Hydra provided you run the host application on port `3009`.
41+
The values above will allow you to login using the `gem-dev` client seeded in Hydra provided you run the host application on port `3009`. An example configuration can be found [in the dummy app](spec/dummy/config/initializers/rpi_auth.rb).
4242

4343
You will need to change the values to match your application, ideally through ENV vars eg.
4444

@@ -66,7 +66,7 @@ class ApplicationController < ActionController::Base
6666
end
6767
```
6868

69-
This provides access to the `current_user` method in controllers and helpers.
69+
This provides access to the `current_user` method in controllers and helpers. The dummy app [has an example of this](spec/dummy/app/controllers/application_controller.rb).
7070

7171
Add the `authenticatable` concern to the host application's User model:
7272

@@ -76,7 +76,7 @@ class User < ApplicationRecord
7676
end
7777
```
7878

79-
This model needs to be the same one defined in the initializer, an instance will be created on login.
79+
This model needs to be the same one defined in the initializer, an instance will be created on login. Again, checkout the [user model in the dummy app](spec/dummy/app/models/user.rb).
8080

8181
To login via Hydra your app needs to send the user to `/auth/rpi` via a POST request:
8282

@@ -156,6 +156,33 @@ class in `config/application.rb`.
156156
config.railties_order = [RpiAuth::Engine, :main_app, :all]
157157
```
158158

159+
## Troubleshooting
160+
161+
Diagnosing issues with OpenID Connect can be tricky, so here are some things to try.
162+
163+
### Setting the token URL in development mode
164+
165+
Typically we run both Profile/Hydra and our applications in Docker. Both the browser and the application have to communicate with Hydra, and in a docker situation this means using two different hostnames. The browser can use `localhost`, but inside docker containers `localhost` refers to the container itself, not the machine running Docker. So the container has to use `docker.host.internal` instead. As a result, the application needs to have a separate URL to check tokens on. We configure this as the `auth_token_url`.
166+
167+
Typical local environment variables for development are
168+
169+
```
170+
AUTH_CLIENT_ID=my-hydra-client-dev
171+
AUTH_CLIENT_SECRET=1234567890
172+
AUTH_TOKEN_URL=http://host.docker.internal:9001/
173+
AUTH_URL=http://localhost:9001 # The URL where Hydra is running
174+
HOST_URL=http://localhost:3000 # The URL where your app is running
175+
IDENTITY_URL=http://localhost:3002 # The URL where Profile (Pi Accounts) is running
176+
```
177+
178+
### Matching the Issuer
179+
180+
When tokens are issued, the OpenID Connect library validates that the token's "issuer" (`iss`) value. This library assumes that it matches the `auth_url` value, complete with a trailing slash. If this is not the case, you can set the issuer manually. It should match the value in either the `docker-compose.yml` in the profile repo, or at `http://localhost:9001/.well-known/openid-configuration` when Hydra is running.
181+
182+
### Discovery
183+
184+
The Omniauth OpenID Connect gem can use discovery to work out the majority of the configuration. However this does not work in development, as the discovery URL is assumed to be available over HTTPS which is not the case in this scenario.
185+
159186
## Upgrading between versions.
160187

161188
This project follows semantic versioning, so upgrades between minor and patch

lib/rpi_auth/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def authorization_endpoint
4646
end
4747

4848
def issuer
49-
@issuer ||= token_endpoint.merge('/').to_s
49+
@issuer ||= authorization_endpoint.merge('/').to_s
5050
end
5151

5252
def jwks_uri

spec/dummy/config/initializers/rpi_auth.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,4 @@
99
config.user_model = 'User'
1010

1111
config.bypass_auth = false
12-
13-
# In development, the issuer is set in the docker-compose.yml file in the
14-
# Profile repo. If you see errors like
15-
#
16-
# (rpi) Authentication failure! Invalid ID token: Issuer does not match
17-
#
18-
# then set the issuer here to match the value in the docker-compose file.
19-
# When Hydra is running, the issue value can also be viewed at
20-
# http://localhost:9001/.well-known/openid-configuration
21-
#
22-
# In staging/production this shouldn't be an issue, as all the hostnames are
23-
# the same.
24-
config.issuer = "http://localhost:9001/"
2512
end

spec/rpi_auth/configuration_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@
6464
it 'sets the authorization_endpoint correctly' do
6565
expect(configuration.authorization_endpoint).to eq URI.parse(auth_url).merge('/oauth2/auth')
6666
end
67+
68+
it 'sets the issuer' do
69+
expect(configuration.issuer).to eq URI.parse(expected_url).merge('/').to_s
70+
end
6771
end
6872
end
6973

7074
shared_examples 'sets up the token url defaults' do
71-
it 'sets the issuer' do
72-
expect(configuration.issuer).to eq URI.parse(expected_url).merge('/').to_s
73-
end
74-
7575
it 'sets the token_endpoint' do
7676
expect(configuration.token_endpoint).to eq URI.parse(expected_url).merge('/oauth2/token')
7777
end

0 commit comments

Comments
 (0)