File tree

8 files changed

+37
-105
lines changed

8 files changed

+37
-105
lines changed
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
<parent>
2020
<artifactId>project</artifactId>
2121
<groupId>se.curity.examples.oauth</groupId>
22-
<version>1.0.0</version>
22+
<version>3.0.0</version>
2323
</parent>
2424
<modelVersion>4.0.0</modelVersion>
2525

2626
<artifactId>api-example</artifactId>
2727
<name>OAuth protected API Example</name>
2828
<packaging>jar</packaging>
29-
<version>1.0.0</version>
29+
<version>3.0.0</version>
3030

3131
<build>
3232
<plugins>
@@ -79,7 +79,7 @@
7979

8080
<dependencies>
8181
<dependency>
82-
<groupId>se.curity.examples.oauth</groupId>
82+
<groupId>io.curity</groupId>
8383
<artifactId>oauth-filter</artifactId>
8484
<version>${project.version}</version>
8585
</dependency>
@@ -108,8 +108,8 @@
108108
<artifactId>httpclient</artifactId>
109109
</dependency>
110110
<dependency>
111-
<groupId>com.google.code.gson</groupId>
112-
<artifactId>gson</artifactId>
111+
<groupId>org.glassfish</groupId>
112+
<artifactId>javax.json</artifactId>
113113
</dependency>
114114
<dependency>
115115
<groupId>org.eclipse.jetty.aggregate</groupId>
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ public class EmbeddedSparkJwtFilterConfig implements FilterConfig
3434

3535
public EmbeddedSparkJwtFilterConfig(String oauthHost, String oauthPort,
3636
String jsonWebKeysPath,
37-
String scope, String minKidReloadTimeInSeconds)
37+
String scope, String minKidReloadTimeInSeconds,
38+
String issuer, String audience)
3839
{
3940
_params = new HashMap<>();
4041
_params.put("oauthHost", oauthHost);
4142
_params.put("oauthPort", oauthPort);
4243
_params.put("jsonWebKeysPath", jsonWebKeysPath);
4344
_params.put("scope", scope);
4445
_params.put("minKidReloadTimeInSeconds", minKidReloadTimeInSeconds);
46+
_params.put("audience", audience);
47+
_params.put("issuer", issuer);
4548
}
4649

4750
@Override
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import org.slf4j.Logger;
2020
import org.slf4j.LoggerFactory;
21-
import se.curity.examples.oauth.AuthenticatedUser;
22-
import se.curity.examples.oauth.OAuthFilter;
23-
import se.curity.examples.oauth.OAuthJwtFilter;
24-
import se.curity.examples.oauth.OAuthOpaqueFilter;
21+
import io.curity.oauth.AuthenticatedUser;
22+
import io.curity.oauth.OAuthFilter;
23+
import io.curity.oauth.OAuthJwtFilter;
24+
import io.curity.oauth.OAuthOpaqueFilter;
2525
import spark.servlet.SparkApplication;
2626

2727
import javax.servlet.ServletException;
@@ -40,7 +40,7 @@ public void init()
4040
{
4141
_logger.debug("Initializing OAuth protected API");
4242
get("/hello_world", (req, res) ->{
43-
AuthenticatedUser user = (AuthenticatedUser)req.attribute(OAuthFilter.PRINCIPAL);
43+
AuthenticatedUser user = req.attribute(OAuthFilter.PRINCIPAL_ATTRIBUTE_NAME);
4444
return "Hello "+ user.getSubject() + " from an OAuth protected world!";
4545
});
4646
}
@@ -70,7 +70,9 @@ private OAuthFilter getJwtFilter() throws ServletException
7070
"8443",
7171
"/oauth/v2/oauth-anonymous/jwks",
7272
"read",
73-
"3600");
73+
"3600",
74+
"https://localhost:8443/oauth/v2/oauth-anonymous",
75+
"client_id");
7476
OAuthFilter filter = new OAuthJwtFilter();
7577

7678
filter.init(filterParams);
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
-->
3333
<filter>
3434
<filter-name>OAuthJwtFilter</filter-name>
35-
<filter-class>se.curity.examples.oauth.OAuthFilter</filter-class>
35+
<filter-class>io.curity.oauth.OAuthJwtFilter</filter-class>
3636
<init-param>
3737
<param-name>oauthHost</param-name>
3838
<param-value>localhost</param-value>
@@ -66,7 +66,7 @@
6666
6767
<filter>
6868
<filter-name>OAuthOpaqueFilter</filter-name>
69-
<filter-class>se.curity.examples.oauth.OAuthFilter</filter-class>
69+
<filter-class>io.curity.oauth.OAuthOpaqueFilter</filter-class>
7070
<init-param>
7171
<param-name>oauthHost</param-name>
7272
<param-value>localhost</param-value>
@@ -102,4 +102,4 @@
102102
-->
103103

104104

105-
</web-app>
105+
</web-app>
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ The API Example (api-example) and the OAuth Filter (oauth-filter) are licensed u
6060
> This product includes software developed at
6161
> The Apache Software Foundation (http://www.apache.org/).
6262
63-
### Google Gson
64-
65-
* License: [Apache v. 2](#apache-2)
66-
* Modifications: No
67-
* Link: <https://.com/google/gson>
68-
* Notices:
69-
70-
> Copyright (C) 2009 Google Inc.
71-
7263
### Google Guava
7364

7465
* License: [Apache v. 2](#apache-2)
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
<groupId>se.curity.examples.oauth</groupId>
2121
<artifactId>project</artifactId>
2222
<packaging>pom</packaging>
23-
<version>1.0.0</version>
23+
<version>3.0.0</version>
2424
<name>Curity OAuth filter example - Project POM</name>
2525

2626
<organization>
27-
<name>Curity I/O AB</name>
28-
<url>http://curity.io</url>
27+
<name>Curity AB</name>
28+
<url>https://curity.io</url>
2929
</organization>
3030

3131
<properties>
@@ -34,7 +34,6 @@
3434
</properties>
3535

3636
<modules>
37-
<module>oauth-filter</module>
3837
<module>api-example</module>
3938
</modules>
4039

@@ -103,9 +102,9 @@
103102
<version>18.0</version>
104103
</dependency>
105104
<dependency>
106-
<groupId>com.google.code.gson</groupId>
107-
<artifactId>gson</artifactId>
108-
<version>2.3.1</version>
105+
<groupId>org.glassfish</groupId>
106+
<artifactId>javax.json</artifactId>
107+
<version>1.1.4</version>
109108
</dependency>
110109
<dependency>
111110
<groupId>com.google.code.findbugs</groupId>
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@
33
[![Quality](https://img.shields.io/badge/quality-demo-red)](https://curity.io/resources/code-examples/status/)
44
[![Availability](https://img.shields.io/badge/availability-source-blue)](https://curity.io/resources/code-examples/status/)
55

6-
This project contains two modules
7-
8-
1. api-example - an example web api that uses the OAuth filter
9-
2. oauth-filter - a servlet filter that authenticates and authorizes requests using OAuth access tokens.
10-
11-
There are two `OAuthFilter` implementations. `OAuthJwtFilter` and `OAuthOpaqueFilter`.
12-
These implement Servlet filters `Filter` and can be used to protect APIs build using Java.
13-
14-
The example also include a minimal web-server using `Spark` [sparkjava](http://sparkjava.com) that uses these filters
15-
in its `before` clause.
6+
This project contains an example web api that uses Curity's OAuth filter library. The example includes a minimal web-server
7+
using `Spark` [sparkjava](http://sparkjava.com) that uses the filter in its `before` clause.
168

179
Depending on the format of the access token, there are two approaches that can be taken.
1810

1911
1. If the token is a Json Web Token (JWT) then validate the token using a public key
2012
2. If the token is a reference (opaque) token, then validate by calling the OAuth server's
2113
[introspection](https://tools.ietf.org/search/rfc7662) endpoint.
2214

15+
Each approach can be handled by using a proper `OAuthFilter` implementation: `OAuthJwtFilter` or `OAuthOpaqueFilter`.
16+
These implement Servlet filters `Filter` and can be used to protect APIs build using Java.
17+
2318
## Filter overview
2419

2520
The filter is build to perform two tasks.
2621

27-
1. Authenticate the caller by validating the incoming access token
28-
2. Authorize the operation by validating the scopes in the access token against the configured scopes
22+
1. Validate the integrity of the incoming access token.
23+
2. Authorize the operation by validating the scopes in the access token against the configured scopes.
2924

3025
The authorization is very basic, and in this example only checks that all configured scopes are present in the
3126
token. A more advanced scenario would likely want to check the HTTP method, along with sub-paths in order to determine
@@ -63,29 +58,20 @@ configured ones. It is simple to override this method in the implementing classe
6358

6459
When building with `mvn package`, a jar called `api-example-x.y.z.jar` is created in the
6560
`target` directory of the api-example module. A full image jar is added with all dependencies included called
66-
`api-example-1.0.0-jar-with-dependencies.jar`. This is a runnable JAR which can be
61+
`api-example-3.0.0-jar-with-dependencies.jar`. This is a runnable JAR which can be
6762
run with the following command:
6863

6964
```
70-
java -jar api-example/target/api-example-1.0.0-jar-with-dependencies.jar
65+
java -jar api-example/target/api-example-3.0.0-jar-with-dependencies.jar
7166
```
7267

73-
To use a specific HttpClientSupplier, just place a file called `OAuthFilter.properties` in
74-
the working directory. For example:
75-
76-
```
77-
cp /path-to-properties-file/OAuthFilter.properties .
78-
java -jar api-example/target/api-example-1.0.0-jar-with-dependencies.jar
79-
```
80-
81-
See more information about the properties file in the *Providing an external HttpClient* section.
68+
To learn how to provide your own HTTP client, check the [filter's documentation](https://.com/curityio/oauth-filter-for-java#providing-an-external-httpclient).
8269

8370
*Note* Unsafe HTTP clients should *NEVER* be used in production.
8471

8572
## Configuring the Filter
8673

87-
To configure the filter, use the `web.xml` file of your application as shown in the
88-
`server-example` project.
74+
To configure the filter, use the `web.xml` file of your application as shown in the `server-example` project.
8975

9076
### Init-params for the OAuthJwtFilter
9177

@@ -104,34 +90,6 @@ To configure the filter, use the `web.xml` file of your application as shown in
10490
* clientId - your application's client id to use for introspection.
10591
* clientSecret - your application's client secret.
10692

107-
108-
## Providing an external HttpClient
109-
110-
The `OAuthFilter` uses a [HttpClient](https://hc.apache.org/httpcomponents-client-ga/)
111-
to communicate with the authentication server.
112-
113-
The HttpClient may be overridden by the web application by providing a properties
114-
file in the following locations:
115-
116-
* `META-INF/services/OAuthFilter.properties` relative to the classpath
117-
* `OAuthFilter.properties` relative to the working directory
118-
119-
The only accepted property is the name of a supplier class to be used to provide the HttpClient instance:
120-
121-
```properties
122-
openid.httpClientSupplier.className=com.example.HttpClientSupplier
123-
```
124-
125-
Replace `com.example.HttpClientSupplier` with the name of your own supplier class.
126-
127-
This class must be an instance of Java 8's `java.util.function.Supplier` interface,
128-
and it must provide a `org.apache.http.client.HttpClient`.
129-
130-
It also must have a default constructor.
131-
132-
See `se.curity.examples.oauth.DefaultJwkHttpClientSupplier` for an example.
133-
This will be used if no properties file is found.
134-
13593
## More Information
13694

13795
For more information, please contact [Curity](http://curity.io).

0 commit comments

Comments
 (0)