2020-01-20 16:11:02 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
# (c) 2018, Jan Christian Grünhage <jan.christian@gruenhage.xyz>
|
|
|
|
# (c) 2020, Famedly GmbH
|
2020-02-10 17:23:03 +00:00
|
|
|
# GNU Affero General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/agpl-3.0.txt)
|
2020-01-20 16:11:02 +00:00
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {
|
|
|
|
'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'
|
|
|
|
}
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
author: "Jan Christian Grünhage (@jcgruenhage)"
|
2020-02-14 11:42:54 +00:00
|
|
|
module: matrix_logout
|
2020-01-20 16:11:02 +00:00
|
|
|
short_description: Log out of matrix
|
|
|
|
description:
|
|
|
|
- Invalidate an access token by logging out
|
|
|
|
options:
|
|
|
|
hs_url:
|
|
|
|
description:
|
|
|
|
- URL of the homeserver, where the CS-API is reachable
|
|
|
|
required: true
|
|
|
|
token:
|
|
|
|
description:
|
|
|
|
- Authentication token for the API call
|
|
|
|
required: true
|
|
|
|
requirements:
|
2020-02-10 17:23:03 +00:00
|
|
|
- matrix-nio (Python library)
|
2020-01-20 16:11:02 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Invalidate access token
|
2020-02-14 11:42:54 +00:00
|
|
|
matrix_logout:
|
2020-01-20 16:11:02 +00:00
|
|
|
hs_url: "https://matrix.org"
|
|
|
|
token: "{{ matrix_auth_token }}"
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
'''
|
|
|
|
import traceback
|
2020-02-10 17:23:03 +00:00
|
|
|
import asyncio
|
2020-01-20 16:11:02 +00:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
|
|
|
|
|
|
|
MATRIX_IMP_ERR = None
|
|
|
|
try:
|
2020-02-10 17:23:03 +00:00
|
|
|
from nio import AsyncClient
|
2020-01-20 16:11:02 +00:00
|
|
|
except ImportError:
|
|
|
|
MATRIX_IMP_ERR = traceback.format_exc()
|
2020-10-02 17:50:02 +00:00
|
|
|
MATRIX_FOUND = False
|
2020-01-20 16:11:02 +00:00
|
|
|
else:
|
2020-10-02 17:50:02 +00:00
|
|
|
MATRIX_FOUND = True
|
2020-01-20 16:11:02 +00:00
|
|
|
|
2020-02-10 17:23:03 +00:00
|
|
|
async def run_module():
|
2020-01-20 16:11:02 +00:00
|
|
|
module_args = dict(
|
|
|
|
hs_url=dict(type='str', required=True),
|
|
|
|
token=dict(type='str', required=True, no_log=True),
|
|
|
|
)
|
|
|
|
|
|
|
|
result = dict(
|
|
|
|
changed=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2020-10-02 17:50:02 +00:00
|
|
|
if not MATRIX_FOUND:
|
2020-02-10 17:23:03 +00:00
|
|
|
module.fail_json(msg=missing_required_lib('matrix-nio'), exception=MATRIX_IMP_ERR)
|
2020-01-20 16:11:02 +00:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
return result
|
|
|
|
|
|
|
|
# create a client object
|
2020-02-10 17:23:03 +00:00
|
|
|
client = AsyncClient(module.params['hs_url'])
|
2020-02-14 01:24:05 +00:00
|
|
|
client.access_token = module.params['token']
|
2020-02-10 17:23:03 +00:00
|
|
|
# log out
|
|
|
|
await client.logout()
|
|
|
|
# close client sessions
|
|
|
|
await client.close()
|
2020-01-20 16:11:02 +00:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-02-10 17:23:03 +00:00
|
|
|
asyncio.run(run_module())
|
2020-01-20 16:11:02 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|