-
Notifications
You must be signed in to change notification settings - Fork 3
OAuth/JWT-autentisering #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fredrbus
wants to merge
25
commits into
main
Choose a base branch
from
jwt-autentisering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c416c83
Add JWT/mTLS authentication
fredrbus 51d2c43
Rewrite RequestContentHashFilter as interceptor
fredrbus 60c0d29
Set "request-path" attrubute in own interceptor
fredrbus 9f2ca44
Make authentication method explicit
fredrbus 4ebd3b0
Add docs for v19
fredrbus 23035ed
Redirect docs root only to current version
fredrbus 3e8b250
Fix scope structure
fredrbus 872aa89
Derive OAuth resource from DigipostClientConfig
fredrbus e9d8a4c
Align examples in docs with recent changes
fredrbus 7629686
Fail fast when signing unhashed content
fredrbus 80329d6
DigipostClientException when token retrieval fails
fredrbus f143616
Clamp token cache to a positive duration
fredrbus 09b16e3
Test MutualTlsTokenProvider with local endpoint
fredrbus 94add03
Replace AuthMode with two factory methods
fredrbus 1f55854
Log interceptor events under one logger
fredrbus 3fc1380
Decouple RequestBearerTokenInterceptor from the token provider
fredrbus b99f529
Req.HttpReq.PathInterceptor to Req.PathInterceptor
fredrbus b8267da
Document params on the factory methods
fredrbus 57dae04
Mention Nyva in docs and link to API-docs
fredrbus 010a04b
Handle token endpoint responses without body
fredrbus effc05d
MutualTlsTokenProvider implements Closeable
fredrbus 0a72b82
Configurable timeouts for MutualTlsTokenProvider
fredrbus 79ecfda
Fix botched markdown-links
fredrbus 19318f1
Remove hash-check in RequestSignatureInterceptor
fredrbus e75fbfc
Mark cert-auth as legacy in docs
fredrbus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| --- | ||
| identifier: index | ||
| layout: default | ||
| redirect_from: / | ||
| --- | ||
|
|
||
| <!-- Documentation --> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| --- | ||
| identifier: index | ||
| layout: default | ||
| redirect_from: / | ||
| --- | ||
|
|
||
| <!-- Documentation --> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| --- | ||
| identifier: index | ||
| layout: default | ||
| redirect_from: / | ||
| --- | ||
|
|
||
| <!-- Documentation --> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| --- | ||
| identifier: index | ||
| layout: default | ||
| redirect_from: / | ||
| --- | ||
|
|
||
| <!-- Documentation --> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| --- | ||
| identifier: index | ||
| layout: default | ||
| redirect_from: / | ||
| --- | ||
|
|
||
| <!-- Documentation --> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| --- | ||
| title: Instantiate and configure the client | ||
| identifier: client_config | ||
| layout: default | ||
| --- | ||
|
|
||
| ### Install | ||
|
|
||
| The client library is available on the [Maven Central Repository](https://central.sonatype.com/artifact/no.digipost/digipost-api-client-java). | ||
| Copy the `<dependency>`-snippet from that website and put it in your pom.xml file. | ||
| Make sure to use the latest version available. | ||
|
|
||
| This client requires Java 11 and `jakarta.xml-bind`. | ||
|
|
||
| ### Configure for production use | ||
|
|
||
| To instantiate the client instance you need to supply your assigned _broker ID_, which | ||
| is set up to be permitted to integrate with the Digipost API. In addition, you must choose | ||
| an authentication method. The client supports two: | ||
|
|
||
| - **OAuth 2.0 over mutual TLS (JWT/mTLS):** the client obtains access tokens over an | ||
| mTLS-secured channel and sends them as bearer tokens. Use | ||
| `DigipostClient.withJwtMtlsAuthentication(...)`. | ||
| - **Certificate-based signing (legacy):** each request is signed with a private key. Use | ||
| `DigipostClient.withCertificateAuthentication(...)`. | ||
|
|
||
| The chosen method is stated explicitly in the factory method you call. | ||
|
|
||
|
|
||
| #### JWT/mTLS authentication | ||
|
|
||
| Before you can use the Digipost API using JWT/mTLS, you must register a client with the | ||
| [Digipost OAuth 2 client authority (Nyva)](https://nyva.digipost.no). Contact the sales team at Digipost to get access to | ||
| the client authority and register your client. More information can be found in the [Digipost API Documentation](https://digipost.github.io/digipost-technical-docs/). | ||
|
|
||
| Configure a `JwtAuthConfig` with your client ID and the client certificate (as a `.p12` | ||
| keystore) used for the mutual-TLS handshake against the token endpoint. The token endpoint | ||
| defaults to the production one, so it only has to be set for other environments. | ||
|
|
||
| ```java | ||
| SenderId senderId = SenderId.of(123456); | ||
|
|
||
| JwtAuthConfig jwtAuthConfig; | ||
| try (InputStream sertifikatInputStream = Files.newInputStream(Paths.get("client-cert.p12"))) { | ||
| jwtAuthConfig = JwtAuthConfig | ||
| .newConfig("your-client-id") | ||
| .pkcs12KeyStore(sertifikatInputStream, "TheSecretPassword") | ||
| .build(); | ||
| } | ||
|
|
||
| DigipostClient client = DigipostClient.withJwtMtlsAuthentication( | ||
| DigipostClientConfig.newConfiguration().build(), senderId.asBrokerId(), jwtAuthConfig); | ||
| ``` | ||
|
|
||
| Access tokens are fetched lazily on first use and cached until shortly before they expire. | ||
| They are requested for the API given by `DigipostClientConfig.digipostApiUri`, so you do | ||
| not configure the API URI in two places. | ||
|
|
||
| The access tokens are fetched with a separate HTTP client, as it has to present the client | ||
| certificate configured above in the TLS handshake against the token endpoint. Its timeouts | ||
| (and proxy, connection pool, ...) can be configured with `tokenEndpointHttpSettings(..)`: | ||
|
|
||
| ```java | ||
| JwtAuthConfig jwtAuthConfig = JwtAuthConfig | ||
| .newConfig("your-client-id") | ||
| .pkcs12KeyStore(sertifikatInputStream, "TheSecretPassword") | ||
| .tokenEndpointHttpSettings( | ||
| HttpClientSettings.DEFAULT.timeouts(HttpClientDefaults.DEFAULT_TIMEOUTS_MS.connect(2000).connectionRequest(1000)), | ||
| HttpClientConnectionSettings.DEFAULT.socketTimeout(5000)) | ||
| .build(); | ||
| ``` | ||
|
|
||
| Both parameters have sensible defaults, so pass `HttpClientSettings.DEFAULT` or | ||
| `HttpClientConnectionSettings.DEFAULT` for the one you do not need to change. The timeouts of | ||
| the client talking to the Digipost API itself are configured separately, with the | ||
| `HttpClientBuilder` accepted by `DigipostClient.withJwtMtlsAuthentication(..)`. | ||
|
|
||
|
|
||
| #### Certificate-based authentication (legacy) | ||
|
|
||
| Create a `Signer` instance, e.g. by using a `.p12` file to read the private key used to | ||
| sign the API requests. | ||
|
|
||
| ```java | ||
| SenderId senderId = SenderId.of(123456); | ||
|
|
||
| Signer signer; | ||
| try (InputStream sertifikatInputStream = Files.newInputStream(Paths.get("certificate.p12"))) { | ||
| signer = Signer.usingKeyFromPKCS12KeyStore(sertifikatInputStream, "TheSecretPassword"); | ||
| } | ||
|
|
||
| DigipostClient client = DigipostClient.withCertificateAuthentication( | ||
| DigipostClientConfig.newConfiguration().build(), senderId.asBrokerId(), signer); | ||
| ``` | ||
|
|
||
| This example will configure the client to communicate with the regular Digipost production | ||
| environment. | ||
|
|
||
| ### Other environments | ||
|
|
||
| If you have access to other environments, this can be configured using | ||
| `DigipostClientConfig`, e.g: | ||
|
|
||
| ```java | ||
| URI apiUri = URI.create("https://api.test.digipost.no"); | ||
| DigipostClientConfig config = DigipostClientConfig.newConfiguration().digipostApiUri(apiUri).build(); | ||
| ``` | ||
|
|
||
| When using JWT/mTLS, also point `JwtAuthConfig` at the token endpoint of that environment: | ||
|
|
||
| ```java | ||
| JwtAuthConfig jwtAuthConfig = JwtAuthConfig | ||
| .newConfig("your-client-id") | ||
| .tokenEndpoint("https://midp.test.digipost.no/oauth2/token") | ||
| .pkcs12KeyStore(sertifikatInputStream, "TheSecretPassword") | ||
| .build(); | ||
| ``` | ||
|
|
||
| #### Norsk Helsenett (NHN) | ||
|
|
||
| The Digipost API is accessible from both internet and Norsk Helsenett (NHN). Both entry points use | ||
| the same API, the only difference is the base URL. | ||
|
|
||
| ```java | ||
| URI nhnApiUri = URI.create("https://api.nhn.digipost.no"); | ||
| DigipostClientConfig config = DigipostClientConfig.newConfiguration().digipostApiUri(nhnApiUri).build(); | ||
| ``` | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.