|
| 1 | +# Authentication |
| 2 | +The Symphony BDK authentication API allows developers to authenticate their bots and apps using RSA authentication mode. |
| 3 | +Please mind we only support RSA authentication. |
| 4 | + |
| 5 | +The following sections will explain you: |
| 6 | +- how to authenticate your bot service account |
| 7 | +- how to authenticate your app to use OBO (On Behalf Of) authentication |
| 8 | + |
| 9 | +## Bot authentication |
| 10 | +In this section we will see how to authenticate a bot service account using RSA authentication. |
| 11 | + |
| 12 | +> Read more about RSA authentication [here](https://developers.symphony.com/symphony-developer/docs/rsa-bot-authentication-workflow) |
| 13 | +
|
| 14 | +Required `config.yaml` setup: |
| 15 | +```yaml |
| 16 | +host: acme.symphony.com |
| 17 | +bot: |
| 18 | + username: bot-username |
| 19 | + privateKey: |
| 20 | + path: /path/to/rsa/private-key.pem |
| 21 | +``` |
| 22 | +
|
| 23 | +### Bot authentication deep-dive |
| 24 | +The code snippet below explains how to manually retrieve your bot authentication session. However, note that by default |
| 25 | +those operations are done behind the scene through the `SymphonyBdk` entry point. |
| 26 | + |
| 27 | +```python |
| 28 | +import logging |
| 29 | +
|
| 30 | +from symphony.bdk.core.config.loader import BdkConfigLoader |
| 31 | +from symphony.bdk.core.symphony_bdk import SymphonyBdk |
| 32 | +
|
| 33 | +async def run(): |
| 34 | + config = BdkConfigLoader.load_from_symphony_dir("config.yaml") |
| 35 | + async with SymphonyBdk(config) as bdk: |
| 36 | + auth_session = bdk.bot_session() |
| 37 | + logging.info(await auth_session.key_manager_token) |
| 38 | + logging.info(await auth_session.session_token) |
| 39 | +``` |
| 40 | + |
| 41 | +### Multiple bot instances |
| 42 | +By design, the `SymphonyBdk` object contains a single bot session. However, you might want to create an application that |
| 43 | +has to handle multiple bot sessions, potentially using different authentication modes. This is possible by creating |
| 44 | +multiple instances of `SymphonyBdk` using different configurations: |
| 45 | +```python |
| 46 | +from symphony.bdk.core.config.loader import BdkConfigLoader |
| 47 | +from symphony.bdk.core.symphony_bdk import SymphonyBdk |
| 48 | +
|
| 49 | +async def run(): |
| 50 | + config_a = BdkConfigLoader.load_from_symphony_dir("config_a.yaml") |
| 51 | + config_b = BdkConfigLoader.load_from_symphony_dir("config_b.yaml") |
| 52 | +
|
| 53 | + async with SymphonyBdk(config_a) as bdk_a, SymphonyBdk(config_b) as bdk_b: |
| 54 | + # use your two service accounts |
| 55 | +``` |
| 56 | + |
| 57 | +## App authentication |
| 58 | +Application authentication is completely optional but remains required if you want to use OBO. |
| 59 | + |
| 60 | +Required `config.yaml` setup: |
| 61 | +```yaml |
| 62 | +host: acme.symphony.com |
| 63 | +app: |
| 64 | + appId: app-id |
| 65 | + privateKey: |
| 66 | + path: /path/to/rsa/private-key.pem |
| 67 | +``` |
| 68 | + |
| 69 | +### OBO (On Behalf Of) authentication |
| 70 | +> Read more about OBO authentication [here](https://developers.symphony.com/symphony-developer/docs/obo-overview) |
| 71 | + |
| 72 | +The following example shows how to retrieve OBO sessions using `username` (type `str`) or `user_id` (type `int`) |
| 73 | +and to call services which have OBO endpoints (users, streams, connections and messages so far): |
| 74 | + |
| 75 | +```python |
| 76 | +from symphony.bdk.core.config.loader import BdkConfigLoader |
| 77 | +from symphony.bdk.core.symphony_bdk import SymphonyBdk |
| 78 | +
|
| 79 | +
|
| 80 | +async def run(): |
| 81 | + config = BdkConfigLoader.load_from_symphony_dir("config.yaml") |
| 82 | + async with SymphonyBdk(config) as bdk: |
| 83 | + obo_auth_session = bdk.obo(username="username") |
| 84 | + async with bdk.obo_services(obo_auth_session) as obo_services: |
| 85 | + obo_services.messages().send_message("stream_id", "<messageML>Hello on behalf of user!</messageML>") |
| 86 | +``` |
| 87 | + |
| 88 | +### BDK running without Bot username (service account) configured |
| 89 | + |
| 90 | +When the bot `username` (service account) is not configured in the Bdk configuration, the bot project will be still |
| 91 | +runnable but only in the OBO mode if the app authentication is well-configured. |
| 92 | + |
| 93 | +The `config.yaml` requires at least the application configuration: |
| 94 | + |
| 95 | +```yaml |
| 96 | +host: acme.symphony.com |
| 97 | +app: |
| 98 | + appId: app-id |
| 99 | + privateKey: |
| 100 | + path: /path/to/private-key.pem |
| 101 | +``` |
| 102 | + |
| 103 | +If users still try to access to Bdk services directly from `SymphonyBdk` facade object, a `BotNotConfiguredError` |
| 104 | +will be thrown. |
| 105 | + |
| 106 | +The example in [part above](#obo-on-behalf-of-authentication) shows how a bot project works without bot `username` |
| 107 | +configured. |
0 commit comments