Skip to content

Commit

Permalink
[O365 Teams] added msgraph-teams-generate-login-url command (demisto#…
Browse files Browse the repository at this point in the history
…29073)

* added commands

* added commands
  • Loading branch information
michal-dagan authored Aug 20, 2023
1 parent 4117cff commit 2098c47
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
from typing import Optional, Union


''' IMPORTS '''
Expand All @@ -19,8 +18,8 @@
class MsGraphClient:

def __init__(self, tenant_id, auth_id, enc_key, app_name, base_url, verify, proxy, self_deployed,
redirect_uri, auth_code, handle_error, certificate_thumbprint: Optional[str] = None,
private_key: Optional[str] = None, delegated_user: Optional[str] = None
redirect_uri, auth_code, handle_error, certificate_thumbprint: str | None = None,
private_key: str | None = None, delegated_user: str | None = None
):

grant_type = AUTHORIZATION_CODE if auth_code and redirect_uri else CLIENT_CREDENTIALS
Expand All @@ -29,7 +28,7 @@ def __init__(self, tenant_id, auth_id, enc_key, app_name, base_url, verify, prox
base_url=base_url, verify=verify, proxy=proxy, self_deployed=self_deployed,
redirect_uri=redirect_uri, auth_code=auth_code, grant_type=grant_type,
resource=resource, certificate_thumbprint=certificate_thumbprint,
private_key=private_key)
private_key=private_key, command_prefix='msgraph-teams')
self.handle_error = handle_error

self.delegated_user = delegated_user
Expand Down Expand Up @@ -101,7 +100,7 @@ def pages_puller(self, response: dict, page_count: int = 100) -> list:
list: list of all pages
"""
responses = [response]
for i in range(page_count - 1):
for _i in range(page_count - 1):
next_link = response.get('@odata.nextLink')
if next_link:
response = self.ms_client.http_request('GET', full_url=next_link, url_suffix=None)
Expand All @@ -110,7 +109,7 @@ def pages_puller(self, response: dict, page_count: int = 100) -> list:
return responses
return responses

def list_chats(self, user_id: str = None, odata: str = None, limit: str = '20') -> Union[dict, list]:
def list_chats(self, user_id: str = None, odata: str = None, limit: str = '20') -> dict | list:
"""Returning all chats from given user
Args:
Expand Down Expand Up @@ -187,7 +186,7 @@ def update_chat(self, chat_id: str, subject: str) -> dict:
}
return self.ms_client.http_request(method='PATCH', url_suffix=suffix, json_data=json_data)

def list_members(self, chat_id: str, user_id: str = None) -> Union[dict, list]:
def list_members(self, chat_id: str, user_id: str = None) -> dict | list:
"""Returning all members from given chat
Args:
Expand Down Expand Up @@ -227,7 +226,7 @@ def add_member(self, chat_id: str, user_id: str, share_history: bool) -> bool:

return True

def list_messages(self, chat_id: str, user_id: str = None, limit: str = '50') -> Union[dict, list]:
def list_messages(self, chat_id: str, user_id: str = None, limit: str = '50') -> dict | list:
"""Returning all mails from given user
Args:
Expand Down Expand Up @@ -270,7 +269,7 @@ def send_message(self, chat_id: str, body: str) -> dict:
''' HELPER FUNCTIONS '''


def build_chat_object(raw_response: Union[dict, list], user_id: str = None):
def build_chat_object(raw_response: dict | list, user_id: str = None):
"""Building chat entry context
Getting a list from build_chat_object
Expand Down Expand Up @@ -307,7 +306,7 @@ def build_chat(given_chat: dict) -> dict:
entry['UserID'] = user_id
return entry

chat_list = list()
chat_list = []
if isinstance(raw_response, list): # response from list_emails_command
for page in raw_response:
# raw_response is a list containing multiple pages or one page
Expand All @@ -326,7 +325,7 @@ def build_chat(given_chat: dict) -> dict:
return chat_list


def build_member_object(raw_response: Union[dict, list], chat_id: str) -> Union[dict, list]:
def build_member_object(raw_response: dict | list, chat_id: str) -> dict | list:
"""Building member entry context
Getting a list from build_member_object
Expand Down Expand Up @@ -361,7 +360,7 @@ def build_member(given_member: dict) -> dict:
entry['ChatID'] = chat_id
return entry

member_list = list()
member_list = []
if isinstance(raw_response, list): # response from list_emails_command
for page in raw_response:
# raw_response is a list containing multiple pages or one page
Expand All @@ -380,7 +379,7 @@ def build_member(given_member: dict) -> dict:
return member_list


def build_message_object(raw_response: Union[dict, list], chat_id: str) -> Union[dict, list]:
def build_message_object(raw_response: dict | list, chat_id: str) -> dict | list:
"""Building message entry context
Getting a list from build_message_object
Expand Down Expand Up @@ -418,7 +417,7 @@ def build_message(given_message: dict) -> dict:
entry['ChatID'] = chat_id
return entry

message_list = list()
message_list = []
if isinstance(raw_response, list): # response from list_emails_command
for page in raw_response:
# raw_response is a list containing multiple pages or one page
Expand Down Expand Up @@ -457,6 +456,7 @@ def test_function(client, _):
"Please enable the integration and run the !msgraph-teams-test command in order to test it")

client.ms_client.http_request(method='GET', url_suffix='chats')
return_results(CommandResults(readable_output='✅ Success!'))
return response, None, None


Expand Down Expand Up @@ -683,9 +683,6 @@ def main():
if not self_deployed and not enc_key:
raise DemistoException('Key must be provided. For further information see '
'https://xsoar.pan.dev/docs/reference/articles/microsoft-integrations---authentication')
if self_deployed and ((auth_code and not redirect_uri) or (not auth_code and redirect_uri)):
raise DemistoException('Please provide both Application redirect URI and Authorization code '
'for Authorization Code flow, or None for the Client Credentials flow')
elif not enc_key and not (certificate_thumbprint and private_key):
raise DemistoException('Key or Certificate Thumbprint and Private Key must be provided.')

Expand Down Expand Up @@ -715,7 +712,12 @@ def main():
auth_code=auth_code, handle_error=handle_error,
certificate_thumbprint=certificate_thumbprint,
private_key=private_key, delegated_user=delegated_user)
run_command(commands, command, client, demisto.args(), tries)
if command == 'msgraph-teams-generate-login-url':
return_results(generate_login_url(client.ms_client))
elif command == 'msgraph-teams-auth-reset':
return_results(reset_auth())
else:
run_command(commands, command, client, demisto.args(), tries)

except Exception as e:
return_error(str(e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,14 @@ script:
- arguments: []
description: Tests connectivity to Microsoft Graph Teams.
name: msgraph-teams-test
dockerimage: demisto/crypto:1.0.0.66562
- description: Generate the login url used for Authorization code flow.
name: msgraph-teams-generate-login-url
arguments: []
- description: Run this command if for some reason you need to rerun the authentication process.
execution: false
name: msgraph-teams-auth-reset
arguments: []
dockerimage: demisto/crypto:1.0.0.68914
script: ''
subtype: python3
type: python
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Microsoft Graph lets your app get authorized access to a user's Teams app in a personal or organization account.

## Configure O365 Teams (Using Graph API) on Cortex XSOAR

1. Navigate to **Settings** > **Integrations** > **Servers & Services**.
Expand All @@ -25,6 +26,7 @@ Microsoft Graph lets your app get authorized access to a user's Teams app in a p


#### Required Permissions

Chat.Create - Delegated
Chat.Read - Delegated
Chat.ReadBasic - Delegated
Expand All @@ -35,16 +37,20 @@ ChatMessage.Read - Delegated
ChatMessage.Send - Delegated

## Commands

You can execute these commands from the Cortex XSOAR CLI, as part of an automation, or in a playbook.
After you successfully execute a command, a DBot message appears in the War Room with the command details.

### msgraph-teams-list-chats

***
Retrieve the list of chats that the user is part of.


#### Base Command

`msgraph-teams-list-chats`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -66,13 +72,15 @@ Retrieve the list of chats that the user is part of.
| MSGraphTeamsChat.Type | String | The type of chat. |

### msgraph-teams-create-chat

***
Create a new chat.


#### Base Command

`msgraph-teams-create-chat`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -95,13 +103,15 @@ Create a new chat.
| MSGraphTeamsChat.Type | String | The type of chat. |

### msgraph-teams-get-chat

***
Retrieve a single chat (without its messages).


#### Base Command

`msgraph-teams-get-chat`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -122,13 +132,15 @@ Retrieve a single chat (without its messages).
| MSGraphTeamsChat.Type | String | The type of chat. |

### msgraph-teams-update-chat

***
Update the properties of a chat object.


#### Base Command

`msgraph-teams-update-chat`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -149,13 +161,15 @@ Update the properties of a chat object.
| MSGraphTeamsChat.Type | String | The type of chat. |

### msgraph-teams-list-members

***
List all conversation members in a chat.


#### Base Command

`msgraph-teams-list-members`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -174,13 +188,15 @@ List all conversation members in a chat.
| MSGraphTeamsChatMember.ChatID | unknown | The ID of the chat. |

### msgraph-teams-add-member

***
Add a conversationMember to a chat.


#### Base Command

`msgraph-teams-add-member`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -193,14 +209,17 @@ Add a conversationMember to a chat.
#### Context Output

There is no context output for this command.

### msgraph-teams-list-messages

***
Retrieve the list of messages in a chat.


#### Base Command

`msgraph-teams-list-messages`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -222,13 +241,15 @@ Retrieve the list of messages in a chat.
| MSGraphTeamsChatMessage.Body | htmlBody | HTML representation of the content of the chat message. Representation is specified by the contentType inside the body. |

### msgraph-teams-send-message

***
Send a new message in a chat.


#### Base Command

`msgraph-teams-send-message`

#### Input

| **Argument Name** | **Description** | **Required** |
Expand All @@ -249,19 +270,57 @@ Send a new message in a chat.
| MSGraphTeamsChatMessage.Body | htmlBody | HTML representation of the content of the chat message. Representation is specified by the contentType inside the body. |

### msgraph-teams-test

***
Tests connectivity to Microsoft Graph Teams.


#### Base Command

`msgraph-teams-test`

#### Input

| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |


#### Context Output

There is no context output for this command.

### msgraph-teams-auth-reset

***
Run this command if for some reason you need to rerun the authentication process.

#### Base Command

`msgraph-teams-auth-reset`

#### Input

| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |

#### Context Output

There is no context output for this command.

### msgraph-teams-generate-login-url

***
Generate the login url used for Authorization code flow.

#### Base Command

`msgraph-teams-generate-login-url`

#### Input

| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |

#### Context Output

There is no context output for this command.
There is no context output for this command.
6 changes: 6 additions & 0 deletions Packs/MicrosoftGraphTeams/ReleaseNotes/1_0_10.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Integrations

##### O365 Teams (Using Graph API)
- Updated the Docker image to: *demisto/crypto:1.0.0.68914*.
- Added support for the ***msgraph-teams-auth-reset*** and ***msgraph-teams-generate-login-url*** commands.
2 changes: 1 addition & 1 deletion Packs/MicrosoftGraphTeams/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "MicrosoftGraphTeams",
"description": "O365 Teams (Using Graph API) gives you authorized access to a user’s Teams enabling you to facilitate communication through teams as that user, or read conversations and/or messages of that user.",
"support": "community",
"currentVersion": "1.0.9",
"currentVersion": "1.0.10",
"author": "Joachim Bockland",
"url": "",
"email": "",
Expand Down

0 comments on commit 2098c47

Please sign in to comment.