Skip to content

Commit e811fa4

Browse files
authored
[SDK-3680] README redesign (#459)
1 parent d7fc25c commit e811fa4

2 files changed

Lines changed: 195 additions & 719 deletions

File tree

EXAMPLES.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Examples using auth0-java
2+
3+
- [Error handling](#error-handling)
4+
- [HTTP Client configuration](#http-client-configuration)
5+
- [Verifying an ID token](#verifying-an-id-token)
6+
- [Organizations](#organizations)
7+
- [Asynchronous operations](#asynchronous-operations)
8+
9+
## Error handling
10+
11+
The API Clients throw an `Auth0Exception` when an unexpected error happens on a request execution, for example a connection or timeout error.
12+
13+
An `APIException` will be thrown if the network request succeeded, but another error occurred.
14+
15+
```java
16+
Request<UsersPage> request = api.users().list(new UserFilter().withSearchEngine("v1"));
17+
try {
18+
UsersPage usersPage = request.execute();
19+
} catch(APIException apiException) {
20+
apiException.getStatusCode(); // 400
21+
apiException.getError(); // "operation_not_supported"
22+
apiException.getDescription(); // "You are not allowed to use search_engine=v1."
23+
}
24+
```
25+
26+
## HTTP Client configuration
27+
28+
Both the Authentication and Management API clients use the OkHttp networking library. Certain configurations of the client are available via the `HttpOptions` object, which can passed to both API client constructors.
29+
30+
```java
31+
HttpOptions options = new HttpOptions();
32+
33+
// configure timeouts; default is ten seconds for both connect and read timeouts:
34+
options.setConnectTimeout(5);
35+
options.setReadTimeout(15);
36+
37+
// configure proxy:
38+
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("{IP-ADDRESS}", {PORT}));
39+
ProxyOptions proxyOptions = new ProxyOptions(proxy);
40+
options.setProxyOptions(proxyOptions);
41+
42+
// create client
43+
AuthAPI authAPI = new AuthAPI("{CLIENT_ID}", "{CLIENT_SECRET}", options);
44+
```
45+
46+
## Verifying an ID token
47+
48+
This library also provides the ability to validate an OIDC-compliant ID Token, according to the [OIDC Specification](https://openid.net/specs/openid-connect-core-1_0-final.html#IDTokenValidation).
49+
50+
### Verifying an ID Token signed with the RS256 signing algorithm
51+
52+
To verify an ID Token that is signed using the RS256 signing algorithm, you will need to provide an implementation of
53+
`PublicKeyProvider` that will return the public key used to verify the token's signature. The example below demonstrates how to use the `JwkProvider` from the [jwks-rsa-java](https://github.com/auth0/jwks-rsa-java) library:
54+
55+
```java
56+
JwkProvider provider = new JwkProviderBuilder("https://your-domain.auth0.com").build();
57+
SignatureVerifier sigVerifier = SignatureVerifier.forRS256(new PublicKeyProvider() {
58+
@Override
59+
public RSAPublicKey getPublicKeyById(String keyId) throws PublicKeyProviderException {
60+
try {
61+
return (RSAPublicKey) provider.get(keyId).getPublicKey();
62+
} catch (JwkException jwke) {
63+
throw new PublicKeyProviderException("Error obtaining public key", jwke);
64+
}
65+
}
66+
}
67+
68+
IdTokenVerifier idTokenVerifier = IdTokenVerifier.init("https://your-domain.auth0.com/","your-client-id", signatureVerifier).build();
69+
70+
try {
71+
idTokenVerifier.verify("token", "expected-nonce");
72+
} catch(IdTokenValidationException idtve) {
73+
// Handle invalid token exception
74+
}
75+
```
76+
77+
### Verifying an ID Token signed with the HS256 signing algorithm
78+
79+
To verify an ID Token that is signed using the HS256 signing algorithm:
80+
81+
```java
82+
SignatureVerifier signatureVerifier = SignatureVerifier.forHS256("your-client-secret");
83+
IdTokenVerifier idTokenVerifier = IdTokenVerifier.init("https://your-domain.auth0.com/","your-client-id", signatureVerifier).build();
84+
85+
try {
86+
idTokenVerifier.verify("token", "expected-nonce");
87+
} catch(IdTokenValidationException idtve) {
88+
// Handle invalid token exception
89+
}
90+
```
91+
92+
## Organizations
93+
94+
[Organizations](https://auth0.com/docs/organizations) is a set of features that provide better support for developers who build and maintain SaaS and Business-to-Business (B2B) applications.
95+
96+
Note that Organizations is currently only available to customers on our Enterprise and Startup subscription plans.
97+
98+
### Log in to an organization
99+
100+
Log in to an organization by using `withOrganization()` when building the Authorization URL:
101+
102+
```java
103+
AuthAPI auth = new AuthAPI("{YOUR_DOMAIN}", "{YOUR_CLIENT_ID}", "{YOUR_CLIENT_SECRET}");
104+
String url = auth.authorizeUrl("https://me.auth0.com/callback")
105+
.withOrganization("{YOUR_ORGANIZATION_ID")
106+
.build();
107+
```
108+
109+
**Important!** When logging into an organization, it is important to ensure the `org_id` claim of the ID Token matches the expected organization value. The `IdTokenVerifier` can be configured with an expected `org_id` claim value, as the example below demonstrates.
110+
For more information, please read [Work with Tokens and Organizations](https://auth0.com/docs/organizations/using-tokens) on Auth0 Docs.
111+
```java
112+
IdTokenVerifier.init("{ISSUER}", "{AUDIENCE}", signatureVerifier)
113+
.withOrganization("{ORG_ID}")
114+
.build()
115+
.verify(jwt);
116+
```
117+
118+
### Accept user invitations
119+
120+
Accept a user invitation by using `withInvitation()` when building the Authorization URL:
121+
122+
```
123+
AuthAPI auth = new AuthAPI("{YOUR_DOMAIN}", "{YOUR_CLIENT_ID}", "{YOUR_CLIENT_SECRET}");
124+
String url = auth.authorizeUrl("https://me.auth0.com/callback")
125+
.withOrganization("{YOUR_ORGANIZATION_ID")
126+
.withInvitation("{YOUR_INVITATION_ID}")
127+
.build();
128+
```
129+
130+
## Asynchronous operations
131+
132+
Requests can be executed asynchronously, using the `executeAsync()` method, which returns a `CompletableFuture<T>`.
133+
134+
```java
135+
CompletableFuture<User> userFuture = mgmt.users().getUser("auth0|123", new UserFilter()).executeAsync();
136+
User user = userFuture.get();
137+
```

0 commit comments

Comments
 (0)