-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'CSK-653' of github.com:pubnub/js-chat into CSK-653
- Loading branch information
Showing
4 changed files
with
92 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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,35 +1,88 @@ | ||
## Description | ||
# Access Manager API service | ||
|
||
Sample code for PubNub Access Manager API | ||
This simple NestJS service contains an endpoint generating authentication keys for selected users that can be used in Chat SDK apps using [PubNub's Access Manager](https://www.pubnub.com/docs/general/security/access-control) to control user-to-channel permissions. | ||
|
||
## Installation | ||
In its simplest use case, the Access Manager API service's controller (`AppController`) defines an `/auth-key/:userId` endpoint and listens to it on port `3000`. When a GET request is made to this endpoint with any `userId`, the `getAccessToken` method in the controller is called, and an authentication token (`authKey`) is generated for that user. | ||
|
||
```bash | ||
$ yarn install | ||
For example, by calling the endpoint `http://localhost:3000/auth-key/jenny`, the following authentication key is generated for user `jenny`. | ||
|
||
```html | ||
{"authKey":"qEF2AkF0GmZF1rdDdHRsD0NyZXOlRGNoYW6sc2dlbmVyYWxfY2hhdC1wbnByZXMYI2xnZW5lcmFsX2NoYXQYI3Jrbm93bGVkZ2VfZXhjaGFuZ2UYI3FxdWlja19oZWxwLXBucHJlcxgjdnRlYW1fZGlzY3Vzc2lvbi1wbnByZXMYI2xqZW5ueS1wbnByZXMY72pxdWlja19oZWxwGCNvdGVhbV9kaXNjdXNzaW9uGCNlamVubnkY725wcmlvcml0eV9xdWV1ZRgjdXByaW9yaXR5X3F1ZXVlLXBucHJlcxgjeBlrbm93bGVkZ2VfZXhjaGFuZ2UtcG5wcmVzGCNDZ3JwoENzcGOgQ3VzcqBEdXVpZKJsamVubnktcG5wcmVzGO9lamVubnkY70NwYXSlRGNoYW6gQ2dycKBDc3BjoEN1c3KgRHV1aWSgRG1ldGGgRHV1aWRlamVubnlDc2lnWCBppKbGcIMw3bI92NVfg6KD0GZyw_MR7AD2ci7r-2y82w=="} | ||
``` | ||
|
||
## Running the app | ||
This Access Manager API service is configured to work with a [Chat SDK](https://www.pubnub.com/docs/chat/chat-sdk/overview) application. Once you provide the Admin Portal keys to your app (publish, subscribe, and secret) in the `chatsdk.service.ts` file (`ChatSdkService`) of this service, its controller (`AppController`) delegates the business logic to the `AppService` through the `getAuthKey` method. The `AppService` uses the `ChatSdkService` to initialize and interact with the Chat SDK. Depending on whether the user exists on the Chat SDK app and what their channel restrictions are, the `AppService` constructs and returns a token with reflected permissions. | ||
|
||
```bash | ||
# development | ||
$ yarn run start | ||
By default, the Access Manager API service integrated with the Chat SDK app will generate authentication tokens that grant users all access to all channels on a selected keyset, contrary to the default Access Manager behavior where enabling it on the Admin Portal immediately restricts all users' access on all channels on a given keyset. When the Chat SDK app sets other permissions for selected users (for example, mutes them on or bans them from a given channel), the permissions schema in the Access Manager API service will reflect that in the autogenerated token for that user and set the token to expire after 15 minutes. | ||
|
||
# watch mode | ||
$ yarn run start:dev | ||
To check how the Access Manager permissions schema is defined in the Access Manager API service, head to the [`app.service.ts`](https://github.com/pubnub/js-chat/tree/master/samples/access-manager-api/src/app.service.ts) file. | ||
|
||
# production mode | ||
$ yarn run start:prod | ||
``` | ||
> **NOTE:** You can use the Access Manager API service in an end-to-end scenario to see how to integrate Access Manager in your Chat SDK app. To do that, use [Channel Monitor](https://www.pubnub.com/docs/bizops-workspace/channel-monitor) from [BizOps Workspace](https://www.pubnub.com/docs/bizops-workspace/basics) to mute or ban users on a given keyset, Access Manager API service to generate authentication tokens for users with correct permissions on channels, and the [React Native Group Chat](https://github.com/pubnub/js-chat/tree/master/samples/react-native-group-chat) app to mock muted/banned users. | ||
## Test | ||
## Prerequisites | ||
|
||
```bash | ||
# unit tests | ||
$ yarn run test | ||
To run the Access Manager API service, make sure you have the following: | ||
|
||
# e2e tests | ||
$ yarn run test:e2e | ||
- [yarn](https://classic.yarnpkg.com/en/docs/install) (>=1.22.19) | ||
- Code Editor (e.g. [Visual Studio Code](https://code.visualstudio.com/download)) | ||
- PubNub [Publish, subscribe, and secret keys](https://www.pubnub.com/docs/basics/initialize-pubnub) that you can obtain from [Admin Portal](https://admin.pubnub.com/) after setting up an [account](https://www.pubnub.com/docs/setup/account-setup). The autogenerated **Demo Keyset** in **My First App** on the Admin Portal already contains the required configuration (enabled **App Context** and selected region). | ||
|
||
# test coverage | ||
$ yarn run test:cov | ||
``` | ||
## Usage | ||
|
||
To run the service, follow these steps: | ||
|
||
1. Open the terminal, select the location, and download the Chat SDK repository. | ||
|
||
```ssh showLineNumbers | ||
git clone [email protected]:pubnub/js-chat.git | ||
``` | ||
|
||
1. Go to the downloaded repository folder. | ||
|
||
```bash showLineNumbers | ||
cd js-chat | ||
``` | ||
|
||
1. Go to the `samples/access-manager-api` folder with the service's source code. | ||
|
||
```bash showLineNumbers | ||
cd ../samples/access-manager-api | ||
``` | ||
|
||
1. Provide the Admin Portal keys to your app (publish, subscribe, and secret) in the `chatsdk.service.ts` file and specify the admin user. | ||
|
||
```ts | ||
async getChatSdkInstance() { | ||
return Chat.init({ | ||
publishKey: 'YOUR_PUB_KEY', | ||
subscribeKey: 'YOUR_SUB_KEY', | ||
secretKey: 'YOUR_SECRET_KEY', | ||
userId: 'secret-user', | ||
}); | ||
} | ||
``` | ||
|
||
1. Run the app in the terminal. | ||
|
||
```bash | ||
# development | ||
$ yarn run start | ||
|
||
# watch mode | ||
$ yarn run start:dev | ||
|
||
# production mode | ||
$ yarn run start:prod | ||
``` | ||
|
||
Alternatively, use these commands for testing: | ||
|
||
```bash | ||
# unit tests | ||
$ yarn run test | ||
|
||
# e2e tests | ||
$ yarn run test:e2e | ||
|
||
# test coverage | ||
$ yarn run test:cov | ||
``` |
This file contains 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