You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*[Migrating from v6 to v7](#migrating-from-v6-to-v7)
@@ -32,36 +33,43 @@ In addition, the SDK provides helper utilities centered around tokens and user p
32
33
Read the [official documentation](https://cloud.ibm.com/docs/appid?topic=appid-getting-started) for information about getting started with IBM Cloud App ID Service.
33
34
34
35
## Requirements
36
+
35
37
* npm 6.+
36
38
* node 12.+
37
39
* passport.js 0.6.0+
38
40
39
41
## Migrating from v6 to v7
42
+
40
43
Breaking changes in passport.js require users to pass the `{ keepSessionInfo: true }` object to the middleware when protecting endpoints when using WebAppStrategy.
In SDK version 7, when using webappStrategy, you need to pass { keepSessionInfo: true } to the passport.authenticate method. Update your code as follows:
Below find two examples of using this SDK to protect APIs and Web applications. Both samples are available under `samples` folder in this repository.
62
69
63
70
Note that below examples are using additional npm modules. In order to install required npm modules run below commands in your node.js application.
64
-
```
71
+
72
+
```shell
65
73
npm install --save express
66
74
npm install --save log4js
67
75
npm install --save passport
@@ -70,6 +78,7 @@ npm install --save pug
70
78
```
71
79
72
80
#### Protecting APIs using the APIStrategy
81
+
73
82
APIStrategy expects request to contain an Authorization header with valid access token and optionally identity token. See App ID docs for additional information. The expected header structure is `Authorization=Bearer {access_token} [{id_token}]`
74
83
75
84
In case of invalid/expired tokens the APIStrategy will return HTTP 401 with `Www-Authenticate=Bearer scope="{scope}" error="{error}"`. The `error` component is optional.
@@ -127,7 +136,9 @@ app.listen(port, function(){
127
136
```
128
137
129
138
##### Protecting APIs using the APIStrategy: Access Control
139
+
130
140
Using access control, you can define the scopes that are required to access a specific endpoint.
The audience parameter is optional and should be set to the application clientId
144
156
to guarantee the scopes are for the requested application.
145
157
146
158
#### Protecting web applications using WebAppStrategy
159
+
147
160
WebAppStrategy is based on the OAuth2 authorization_code grant flow and should be used for web applications that use browsers. The strategy provides tools to easily implement authentication and authorization flows. When WebAppStrategy provides mechanisms to detect unauthenticated attempts to access protected resources. The WebAppStrategy will automatically redirect user's browser to the authentication page. After successful authentication user will be taken back to the web application's callback URL (redirectUri), which will once again use WebAppStrategy to obtain access, identity and refresh tokens from App ID service. After obtaining these tokens the WebAppStrategy will store them in HTTP session under WebAppStrategy.AUTH_CONTEXT key. In a scalable cloud environment it is recommended to persist HTTP sessions in a scalable storage like Redis to ensure they're available across server app instances.
Use WebAppStrategy's hasScope method to check if a given request has some specific scopes.
266
283
267
284
#### Anonymous login
285
+
268
286
WebAppStrategy allows users to login to your web application anonymously, meaning without requiring any credentials. After successful login the anonymous user access token will be persisted in HTTP session, making it available as long as HTTP session is kept alive. Once HTTP session is destroyed or expired the anonymous user access token will be destroyed as well.
269
287
270
288
To allow anonymous login for a particular URL use two configuration properties as shown on a code snippet below:
289
+
271
290
*`allowAnonymousLogin` - set this value to true if you want to allow your users to login anonymously when accessing this endpoint. If this property is set to true no authentication will be required. The default value of this property is `false`, therefore you must set it explicitly to allow anonymous login.
272
291
*`allowCreateNewAnonymousUser` - By default a new anonymous user will be created every time this method is invoked unless there's an existing anonymous access_token stored in the current HTTP session. In some cases you want to explicitly control whether you want to automatically create new anonymous user or not. Set this property to `false` if you want to disable automatic creation of new anonymous users. The default value of this property is `true`.
As mentioned previously the anonymous access_token, identity_token and refresh_token (optional) will be automatically persisted in HTTP session by App ID SDK. You can retrieve them from HTTP session via same mechanisms as regular tokens. Access and identity tokens will be kept in HTTP session and will be used until either them or HTTP session expires.
286
305
287
306
### Refresh Token
307
+
288
308
Refresh Token may be used to acquire new access and identity tokens without the need to re-authenticate. Refresh Token is usually configured to have longer expiration than access token. Refresh Token is optional and can be configured in your App ID Dashboard.
289
309
290
310
After a successful login, in addition to access_token and identity_token, a refresh_token will be persisted in the HTTP session as well.
App ID's custom identity flow enables developers to utilize their own authorization protocols, while still leveraging App ID's capabilities. Instead of managing the entirety of the authorization flow, App ID's custom identity flow allows clients to leverage their own authorization protocol to authenticate and authorize their users and then provides a framework for exchanging verified authentication data securely for App ID tokens.
321
342
322
343
To utilize the custom identity flow, the user must first register a public key in PEM form using the App ID Dashboard. The user must generate a signed JWT using any open source library and then the user can then use `TokenManager.getCustomIdentityTokens(jwsTokenString, scopes)` to exchange the token for access and identity tokens. `getCustomIdentityTokens()` is an asynchronous function that returns the access token and identity token. These tokens can be stored in the HTTP session for future use. `custom-identity-app-sample-server.js` contains an example of using the Token Manager.
323
344
324
345
Refer to the [documentation on custom identity](https://cloud.ibm.com/docs/appid?topic=appid-custom-identity) for more details on how to implement App ID's custom identity flow in your application.
325
346
326
-
327
347
### Application Identity and Authorization
328
348
329
-
In case you want to call protected APIs from applications or clients that are non-interactive (i.e., there is no user involved), you can use the App ID application identity and authorization flow to secure your applications.
349
+
In case you want to call protected APIs from applications or clients that are non-interactive (i.e., there is no user involved), you can use the App ID application identity and authorization flow to secure your applications.
330
350
331
351
App ID application authorization implements the OAuth2.0 Client Credentials grant.
332
352
@@ -362,9 +382,11 @@ async function getAppIdentityToken() {
362
382
}
363
383
}
364
384
```
385
+
365
386
For more detailed information on using the application identity and authorization flow, refer to the [App ID documentation](https://cloud.ibm.com/docs/appid?topic=appid-app).
366
387
367
388
### Manage User Profile
389
+
368
390
Using the App ID UserProfileManager, you are able to create, delete, and retrieve user profile attributes as well as get additional info about a user.
Make sure to that Cloud Directory identity provider set to **ON** in the App ID dashboard and that you've included a callback endpoint.
408
432
409
433
### Login using resource owner password flow
434
+
410
435
WebAppStrategy allows users to login to your web application using username/password.
411
436
After successful login, the user access token will be persisted in HTTP session, making it available as long as HTTP session is kept alive. Once HTTP session is destroyed or expired the user access token will be destroyed as well.
412
437
To allow login using username/password add to your app a post route that will be called with the username and password parameters.
*`successRedirect` - set this value to the url you want the user to be redirected after successful authentication, default: the original request url. (in this example:"/form/submit")
421
448
*`failureRedirect` - set this value to the url you want the user to be redirected in case authentication fails, default: the original request url. (in this example:"/form/submit")
422
449
*`failureFlash` - set this value to true if you want to receive the error message that returned from cloud directory service, default: false
423
450
424
451
Note:
452
+
425
453
1. If you submitting the request using a html form, use [body-parser](https://www.npmjs.com/package/body-parser) middleware.
426
454
2. Use [connect-flash](https://www.npmjs.com/package/connect-flash) for getting the returned error message. see the web-app-sample-server.js.
427
455
428
456
### Sign up
457
+
429
458
Pass WebAppStrategy "show" property and set it to WebAppStrategy.SIGN_UP, will launch the App ID sign up form.
1. If your Cloud directory setting ***Allow users to sign-in without email verification** is set to **No**, the process will end without retrieving App ID access and id tokens.
438
470
2. Be sure to set **Allow users to sign up and reset their password" to **ON**, in the settings for Cloud Directory.
439
471
440
-
441
472
### Forgot Password
473
+
442
474
Pass WebAppStrategy "show" property and set it to WebAppStrategy.FORGOT_PASSWORD, will launch the App ID forgot password from.
1. This process will end without retrieving App ID access and id tokens.
451
486
2. Make sure to set "Allow users to sign up and reset their password" and "Forgot password email" to ON, in Cloud Directory settings that are in the App ID dashboard.
452
487
453
488
### Change Details
489
+
454
490
Pass WebAppStrategy "show" property and set it to WebAppStrategy.CHANGE_DETAILS, will launch the App ID change details from.
This SDK uses the log4js package for logging. By default the logging level is set to `info`. To create your own logging configuration for your application, add a log4js.json file and set the `process.env.LOG4JS_CONFIG` environment variable to your json file.
616
673
617
-
To learn more about log4js, visit the documentation here (https://log4js-node.github.io/log4js-node/).
674
+
To learn more about log4js, visit the documentation here (<https://log4js-node.github.io/log4js-node/>).
618
675
619
676
## Got Questions?
677
+
620
678
Join us on [Slack](https://www.ibm.com/cloud/blog/announcements/get-help-with-ibm-cloud-app-id-related-questions-on-slack) and chat with our dev team.
621
679
622
680
### License
623
-
This package contains code licensed under the Apache License, Version 2.0 (the "License"). You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 and may also view the License in the LICENSE file within this package.
681
+
682
+
This package contains code licensed under the Apache License, Version 2.0 (the "License"). You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0> and may also view the License in the LICENSE file within this package.
0 commit comments