Skip to content

Commit 87dc88b

Browse files
⬆️ @okta/samples generator 1.16.0 (okta#7)
1 parent 8654137 commit 87dc88b

68 files changed

Lines changed: 1296 additions & 1039 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ travis_phantomjs
66
.DS_Store
77
.vscode
88

9-
10-
public/
9+
public
1110

1211
# Intellij
1312
.idea/
@@ -17,5 +16,6 @@ public/
1716
# Maven
1817
.classpath
1918
.project
20-
.settings/
21-
target/
19+
.settings
20+
target
21+
lib/src/main/resources/public/assets

.samples.config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"oktaSample": {
33
"oidc": {
44
"oktaUrl": "http://127.0.0.1:7777",
5-
"clientId": "zYVNoNIeSwul32vpNiOz",
6-
"clientSecret": "8UxQqreZu4WQBK-KIqKQItqvtv5UlWRlqI36l4sb",
5+
"issuer": "http://127.0.0.1:7777/oauth2/default",
6+
"clientId": "0oaom8GeLzZeX7oYZ0g3",
7+
"clientSecret": "XIZPoGtuyhu0V8MsQzGOsOSn6oSff3vO8Vp2nmLY",
78
"redirectUri": "http://localhost:3000/authorization-code/callback"
89
},
910
"server": {
1011
"staticDir": "lib/src/main/resources/public/assets/",
11-
"startSignal": "Server Started",
1212
"port": 3000,
1313
"framework": "Spring MVC",
1414
"environment": "Java"

.travis.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
language: node_js
2-
node_js:
3-
- '5.8'
1+
language: java
2+
jdk:
3+
- oraclejdk8
4+
5+
env:
6+
- NODE_VERSION=5.8
7+
48
install:
59
- npm install
610
script:
@@ -12,7 +16,8 @@ cache:
1216
directories:
1317
- travis_phantomjs
1418
before_install:
15-
- "npm install -g npm@4.0.2"
19+
- "nvm install $NODE_VERSION"
20+
- "npm install -g npm@4.1.2"
1621
- "export PHANTOMJS_VERSION=2.1.1"
1722
- "export PATH=$PWD/travis_phantomjs/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/bin:$PATH"
1823
- "if [ $(phantomjs --version) != $PHANTOMJS_VERSION ]; then rm -rf $PWD/travis_phantomjs; mkdir -p $PWD/travis_phantomjs; fi"

.yo-rc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"environment": "Java",
55
"framework": "Spring MVC",
66
"name": "samples-java-spring-mvc",
7-
"version": "1.12.0",
7+
"version": "1.16.0",
88
"parentType": "sample"
99
}
1010
}

README.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,24 @@ By default, this application uses a mock authorization server which responds to
6868

6969
To start the mock server, run the following in a second terminal window:
7070
```bash
71-
# Starts the mock Okta server at http://127.0.0.01:7777
71+
# Starts the mock Okta server at http://127.0.0.1:7777
7272
[samples-java-spring-mvc]$ npm run mock-okta
7373
```
7474

75-
If you'd like to test this sample against your own Okta org, follow [these steps to setup an OpenID Connect app](docs/assets/oidc-app-setup.md). Then, replace the *oidc* settings in `samples.config.json` to point to your new app:
75+
If you'd like to test this sample against your own Okta org, navigate to the Okta Developer Dashboard and follow these steps:
76+
77+
1. Create a new **Web** application by selecting **Create New Application** from the *Applications* page.
78+
2. After accepting the default configuration, select **Create Application** to redirect back to the *General Settings* of your application.
79+
3. Copy the **Client ID** and **Client Secret**, as it will be needed for the client configuration.
80+
4. Finally, navigate to `https://{yourOktaDomain}.com/oauth2/default` to see if the [Default Authorization Server](https://developer.okta.com/docs/api/resources/oauth2.html#using-the-default-authorization-server) is setup. If not, [let us know](mailto:developers@okta.com).
81+
82+
Then, replace the *oidc* settings in `.samples.config.json` to point to your new app:
7683
```javascript
7784
// .samples.config.json
7885
{
7986
"oidc": {
80-
"oktaUrl": "https://{{yourOktaOrg}}.oktapreview.com",
87+
"oktaUrl": "https://{{yourOktaDomain}}.com",
88+
"issuer": "https://{{yourOktaDomain}}.com/oauth2/default",
8189
"clientId": "{{yourClientId}}",
8290
"clientSecret": "{{yourClientSecret}}",
8391
"redirectUri": "http://localhost:3000/authorization-code/callback"
@@ -103,6 +111,7 @@ class LoginRedirectController {
103111
$onInit() {
104112
this.authClient = new OktaAuth({
105113
url: this.config.oktaUrl,
114+
issuer: this.config.issuer,
106115
clientId: this.config.clientId,
107116
redirectUri: this.config.redirectUri,
108117
scopes: ['openid', 'email', 'profile'],
@@ -122,7 +131,6 @@ There are a number of different ways to construct the login redirect URL.
122131
3. Use [AuthJS](http://developer.okta.com/code/javascript/okta_auth_sdk)
123132

124133
In this sample, we use AuthJS to create the URL and perform the redirect. An `OktaAuth` object is instantiated with the configuration in `.samples.config.json`. When the `login()` function is called from the view, it calls the [`/authorize`](http://developer.okta.com/docs/api/resources/oauth2.html#authentication-request) endpoint to start the [Authorization Code Flow](https://tools.ietf.org/html/rfc6749#section-1.3.1).
125-
126134

127135
You can read more about the `OktaAuth` configuration options here: [OpenID Connect with Okta AuthJS SDK](http://developer.okta.com/code/javascript/okta_auth_sdk#social-authentication-and-openid-connect).
128136

@@ -150,6 +158,7 @@ class LoginCustomController {
150158
clientId: this.config.clientId,
151159
redirectUri: this.config.redirectUri,
152160
authParams: {
161+
issuer: this.config.issuer,
153162
responseType: 'code',
154163
scopes: ['openid', 'email', 'profile'],
155164
},
@@ -241,7 +250,6 @@ public String callback(@RequestParam("state") String state,
241250
### Code Exchange
242251
Next, we exchange the returned authorization code for an `id_token` and/or `access_token`. You can choose the best [token authentication method](http://developer.okta.com/docs/api/resources/oauth2.html#token-request) for your application. In this sample, we use the default token authentication method `client_secret_basic`:
243252
244-
245253
```java
246254
// Application.java
247255
@@ -290,7 +298,7 @@ ntFBNjluFhNLJIUkEFovEDlfuB4tv_M8BM75celdy3jkpOurg
290298
### Validation
291299
After receiving the `id_token`, we [validate](http://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation) the token and its claims to prove its integrity.
292300
293-
In this sample, we use a [JSON Object Signing and Encryption (JOSE)](https://bitbucket.org/b_c/jose4j/wiki/Home) library to decode and validate the token.
301+
In this sample, we use the a [JSON Object Signing and Encryption (JOSE)](https://bitbucket.org/b_c/jose4j/wiki/Home) library to decode and validate the token.
294302
295303
There are a couple things we need to verify:
296304
@@ -308,7 +316,6 @@ For example:
308316
- If the `kid` has been cached, use it to validate the signature.
309317
- If not, make a request to the `jwks_uri`. Cache the new `jwks`, and use the response to validate the signature.
310318
311-
312319
```java
313320
// Application.java
314321
@@ -322,7 +329,7 @@ private Key fetchJwk(String idToken) throws JoseException, IOException, Exceptio
322329
return CACHED_KEYS.get(keyID);
323330
}
324331
325-
String jwksUri = CONFIG.getOktaSample().getOidc().getOktaUrl() + "/oauth2/v1/keys";
332+
String jwksUri = CONFIG.getOktaSample().getOidc().getIssuer() + "/v1/keys";
326333
HttpsJwks httpJkws = new HttpsJwks(jwksUri);
327334
328335
for (JsonWebKey key : httpJkws.getJsonWebKeys()) {
@@ -339,7 +346,6 @@ private Key fetchJwk(String idToken) throws JoseException, IOException, Exceptio
339346
}
340347
```
341348
342-
343349
#### Verify fields
344350
345351
Verify the `id_token` from the [Code Exchange](#code-exchange) contains our expected claims:
@@ -361,7 +367,7 @@ private Map validateToken(String idToken, String nonce) throws Exception {
361367
.setRequireExpirationTime()
362368
.setAllowedClockSkewInSeconds(clock_skew)
363369
.setExpectedAudience(CONFIG.getOidc().getClientId())
364-
.setExpectedIssuer(CONFIG.getOidc().getOktaUrl())
370+
.setExpectedIssuer(CONFIG.getOidc().getIssuer())
365371
.setVerificationKey(key)
366372
.build();
367373
@@ -371,7 +377,6 @@ private Map validateToken(String idToken, String nonce) throws Exception {
371377
}
372378
```
373379
374-
375380
#### Verify issued time
376381
The `iat` value indicates what time the token was "issued at". We verify that this claim is valid by checking that the token was not issued in the future, with some leeway for clock skew.
377382
@@ -421,17 +426,18 @@ In Spring MVC, you can clear the the user session by:
421426
// Application.java
422427
423428
public String logout(HttpServletRequest request) {
424-
request.getSession().invalidate();
425-
user = new User();
426-
return "redirect:/";
427-
}
429+
request.getSession().invalidate();
430+
user = new User();
431+
return "redirect:/";
432+
}
428433
```
434+
429435
The Okta session is terminated in our client-side code.
430436
431437
## Conclusion
432438
You have now successfully authenticated with Okta! Now what? With a user's `id_token`, you have basic claims into the user's identity. You can extend the set of claims by modifying the `response_type` and `scopes` to retrieve custom information about the user. This includes `locale`, `address`, `phone_number`, `groups`, and [more](http://developer.okta.com/docs/api/resources/oidc.html#scopes).
433439
434-
## Support
440+
## Support
435441
436442
Have a question or see a bug? Email developers@okta.com. For feature requests, feel free to open an issue on this repo. If you find a security vulnerability, please follow our [Vulnerability Reporting Process](https://www.okta.com/vulnerability-reporting-policy/).
437443
@@ -442,4 +448,3 @@ Copyright 2017 Okta, Inc. All rights reserved.
442448
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
443449
444450
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
445-

docs/setup-test-org.md

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,9 @@ You will need the following features enabled:
1313

1414
1. Navigate to the admin app
1515

16-
2. Enable CORS for the sample app server `/admin/access/api/corsList`
17-
18-
- Check the *Enable CORS for the following base URLs* checkbox
19-
- Enter `http://localhost:3000` in the textbox
20-
21-
3. Create an OpenId Connect group `/admin/groups`
22-
23-
| Setting | Value |
24-
| ----------------- | ---------------------------- |
25-
| Name | OpenId Connect Group |
26-
| Group Description | OpenId Connect Samples Group |
27-
28-
4. Create an OpenId Connect app `/admin/apps/oauth2-wizard/create?applicationType=WEB`
29-
30-
| Setting | Value |
31-
| ------------------- | --------------------------------------------------- |
32-
| Application Name | OpenId Connect App |
33-
| Redirect URIs | `http://localhost:3000/authorization-code/callback` |
34-
| Allowed grant types | Authorization Code |
35-
36-
5. Assign the `OpenId Connect App` to the `OpenId Connect Group` in the `Groups` tab
37-
38-
39-
6. Add the sample users, activate, and set these passwords `/admin/users`
40-
41-
**Note:** Add all users to the `OpenId Connect Group`
16+
2. Create a new **Web** application `dev/console/apps/new`
17+
3. Copy the **Client ID** and **Client Secret**, as it will be needed for the client configuration.
18+
4. Add the sample users, activate, and set these passwords `/admin/users`
4219

4320
| Name | Username | Password | What is this user for? |
4421
| ----------------- | --------------- | -------- | ----------------------------------------- |

lib/src/main/java/com/okta/samples/Application.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public String callback(@RequestParam("state") String state,
120120
return send401(response, e.getMessage());
121121
}
122122

123-
String tokenEndpoint = CONFIG.getOktaSample().getOidc().getOktaUrl() + "/oauth2/v1/token?";
123+
String tokenEndpoint = CONFIG.getOktaSample().getOidc().getIssuer() + "/v1/token?";
124124
String clientId = CONFIG.getOktaSample().getOidc().getClientId();
125125
String clientSecret = CONFIG.getOktaSample().getOidc().getClientSecret();
126126
byte[] encodedAuth = Base64.encodeBase64((clientId + ":" + clientSecret).getBytes());
@@ -202,7 +202,7 @@ private Map validateToken(String idToken, String nonce) throws Exception {
202202
.setRequireExpirationTime()
203203
.setAllowedClockSkewInSeconds(clock_skew)
204204
.setExpectedAudience(CONFIG.getOktaSample().getOidc().getClientId())
205-
.setExpectedIssuer(CONFIG.getOktaSample().getOidc().getOktaUrl())
205+
.setExpectedIssuer(CONFIG.getOktaSample().getOidc().getIssuer())
206206
.setVerificationKey(key)
207207
.build();
208208

@@ -245,7 +245,7 @@ private Key fetchJwk(String idToken) throws JoseException, IOException, Exceptio
245245
return CACHED_KEYS.get(keyID);
246246
}
247247

248-
String jwksUri = CONFIG.getOktaSample().getOidc().getOktaUrl() + "/oauth2/v1/keys";
248+
String jwksUri = CONFIG.getOktaSample().getOidc().getIssuer() + "/v1/keys";
249249
HttpsJwks httpJkws = new HttpsJwks(jwksUri);
250250

251251
for (JsonWebKey key : httpJkws.getJsonWebKeys()) {

lib/src/main/java/com/okta/samples/Oidc.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
public class Oidc {
1414

1515
private String oktaUrl;
16+
private String issuer;
1617
private String clientId;
1718
private String clientSecret;
1819
private String redirectUri;
1920

2021
public String getOktaUrl() {return oktaUrl;}
2122
public void setOktaUrl(String oktaUrl) {this.oktaUrl = oktaUrl;}
23+
24+
public String getIssuer() {return issuer;}
25+
public void setIssuer(String issuer) {this.issuer = issuer;}
2226

2327
public String getClientId() {return clientId;}
2428
public void setClientId(String clientId) {this.clientId = clientId;}

lib/src/main/resources/templates/login-custom.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
clientId: '{{config.oidc.clientId}}',
5858
redirectUri: '{{config.oidc.redirectUri}}',
5959
authParams: {
60+
issuer: '{{config.oidc.issuer}}',
6061
responseType: 'code',
6162
scopes: ['openid', 'email', 'profile'],
6263
},
@@ -106,10 +107,12 @@ signIn.renderEl({ el: '#app-container' }, () => {});</code></pre>
106107
{{/config.server.useWebpackDevServer}}
107108

108109
<script src="/assets/bundle.js"></script>
110+
<script src="/assets/doc.js"></script>
109111
<script>
110112
var options = {
111113
container: '#app-container',
112114
oktaUrl: '{{{config.oidc.oktaUrl}}}',
115+
issuer: '{{{config.oidc.issuer}}}',
113116
clientId: '{{config.oidc.clientId}}',
114117
redirectUri: '{{{config.oidc.redirectUri}}}'
115118
};
@@ -123,6 +126,7 @@ signIn.renderEl({ el: '#app-container' }, () => {});</code></pre>
123126
};
124127
{{/user}}
125128
129+
doc.initDoc();
126130
bundle.bootstrap(options);
127131
</script>
128132
</body>

lib/src/main/resources/templates/login-redirect.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<div class="ui segment">
5959
<pre><code class="javascript">const auth = new OktaAuth({
6060
url: '{{config.oidc.oktaUrl}}',
61+
issuer: '{{config.oidc.issuer}}',
6162
clientId: '{{config.oidc.clientId}}',
6263
redirectUri: '{{config.oidc.redirectUri}}',
6364
scopes: ['openid', 'email', 'profile'],
@@ -104,10 +105,12 @@ auth.token.getWithRedirect({
104105
{{/config.server.useWebpackDevServer}}
105106

106107
<script src="/assets/bundle.js"></script>
108+
<script src="/assets/doc.js"></script>
107109
<script>
108110
var options = {
109111
container: '#app-container',
110112
oktaUrl: '{{{config.oidc.oktaUrl}}}',
113+
issuer: '{{{config.oidc.issuer}}}',
111114
clientId: '{{config.oidc.clientId}}',
112115
redirectUri: '{{{config.oidc.redirectUri}}}'
113116
};
@@ -121,6 +124,7 @@ auth.token.getWithRedirect({
121124
};
122125
{{/user}}
123126
127+
doc.initDoc();
124128
bundle.bootstrap(options);
125129
</script>
126130
</body>

0 commit comments

Comments
 (0)