Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth0 modules #20276

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
167 changes: 167 additions & 0 deletions lib/ansible/modules/web_infrastructure/auth0_user.py
@@ -0,0 +1,167 @@
#!/usr/bin/python
#
# (c) 2015, RSD Services S.A
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'version': '1.0'}

DOCUMENTATION = '''
---
module: auth0_user
short_description: Module for managing auth0 users.
description:
- Auth0 user managing module. It can search for a given user and
return it's payload, create/delete user and update application
metadata.
version_added: 2.3
options:
state:
description:
- The state of the user collection. delete_all will remove
all existing users defined in auth0 domain while absent
will remove only one given user identified by his user_id.
default: 'present'
required: true
choices: ['present', 'absent', 'delete_all']
user_data:
description:
- Data used to update given user definition.
aliases: ['body']
user_id:
description:
- User id for a given user.
auth_token:
description:
- Authentication token used for auth0 communication.
required: true
aliases: ['token']
auth0_domain:
description:
- Auth0 domain used for authentication.
required: true
aliases: ['domain']
'''

EXAMPLES = '''
# Update some user attributes on auth0
- auth0_user:
state: present
auth0_token: token
auth0_domain: domain
user_data:
app_metadata:
app_specific_key: value
password: new_password
user_id: userid

# Create new auth0 user for a given domain
- auth0_user:
state: present
auth0_token: token
auth0_domain: domain
user_data:
connection: Username-Password-Authentication
email: john.doe@domain.com
password: SecretPassword
email_verified: true
app_metadata:

user_metadata:
firstName: John,
language: en,
lastName: Doe,

# Remove all users from given auth0 domain
- auth0_user:
state: delete_all
auth0_token: token
auth0_domain: domain
'''

RETURN = '''
#only defaults
'''

import json
import requests

# import module snippets
from ansible.module_utils.basic import AnsibleModule

def main():
module = AnsibleModule(
argument_spec=dict(
auth0_token=dict(required=True, type='str', aliases=['token'], no_log=True),
auth0_domain=dict(required=True, type='str', aliases=['domain']),
state=dict(required=True, default='present',
choices=['present', 'absent', 'delete_all']),
user_id=dict(required=False, type='str'),
user_data=dict(type='dict', required=False, aliases=['body']),

),
supports_check_mode=False
)

auth0_domain = module.params['auth0_domain']
auth0_token = module.params['auth0_token']
state = module.params['state']
user_id = module.params['user_id']
user_data = module.params['user_data']

if state == 'present' or state == 'absent':
if not user_data:
module.fail_json(msg='user_data is required parameter for this state')

if state == 'absent':
if not user_id:
module.fail_json(msg='user_id is required parameter for this state')

http_headers = {
'content-type': 'application/json',
'Authorization': 'Bearer {token}'.format(token=auth0_token)
}

if state == 'present':
if user_id:
url = 'https://%s/api/v2/users/%s'%(auth0_domain, user_id)
req = requests.patch(url, json=user_data, headers=http_headers)
else:
url = 'https://%s/api/v2/users'%(auth0_domain)
req = requests.post(url, json=user_data, headers=http_headers)
elif state == 'absent':
url = 'https://%s/api/v2/users/%s'%(auth0_domain, user_id)
req = requests.delete(url, headers=http_headers)
elif state == 'delete_all':
url = 'https://%s/api/v2/users'%(auth0_domain)
req = requests.delete(url, headers=http_headers)

if req.status_code not in [200, 201, 202, 203, 204]:
module.fail_json(msg='Request to Auth0 failed with return code %s, reason: %s'%
(req.status_code, req.reason))

if req.text:
result = json.loads(req.text)
else:
result = "Rest call has not returned any data."

module.exit_json(results=result)

if __name__ == '__main__':
main()

111 changes: 111 additions & 0 deletions lib/ansible/modules/web_infrastructure/auth0_user_facts.py
@@ -0,0 +1,111 @@
#!/usr/bin/python
#
# (c) 2015, RSD Services S.A
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'version': '1.0'}

DOCUMENTATION = '''
---
module: auth0_user_facts
short_description: Module for gathering facts about auth0 users.
description:
- Auth0 user gathering facts module. It can search for a given user and
return it's payload.
version_added: 2.3
options:
query:
description:
- Query used to select wanted user.
required: true
auth0_token:
description:
- Authentication token used for auth0 communication.
required: true
aliases: ['token']
auth0_domain:
description:
- Auth0 domain used for authentication.
required: true
aliases: ['domain']
search_engine:
description:
- Select required search engine for user queries.
default: 'v2'
'''

EXAMPLES = '''
# Fetch facts about auth0 user/users fetched by a given query.
- auth0_user_facts:
auth0_token: token
auth0_domain: domain-url
query: app_metadata.currentTenantId:t2 AND app_metadata.isTechnical:true
search_engine: v2
register: auth_user
'''

RETURN = '''
#only defaults
'''

import json
import requests

# import module snippets
from ansible.module_utils.basic import AnsibleModule

def main():
module = AnsibleModule(
argument_spec=dict(
auth0_token=dict(required=True, type='str', no_log=True),
auth0_domain=dict(required=True, type='str'),
search_engine=dict(required=False, default='v2', type='str'),
query=dict(required=True, type='str'),
),
supports_check_mode=False
)

auth0_domain = module.params['auth0_domain']
auth0_token = module.params['auth0_token']
query = module.params['query']
search_engine = module.params['search_engine']

http_headers = {
'content-type': 'application/json',
'Authorization': 'Bearer %s'%(auth0_token)
}

url_params = {
'q': query,
'search_engine': search_engine
}

url = 'https://%s/api/v2/users'%(auth0_domain)

req = requests.get(url, params=url_params, headers=http_headers)

if req.status_code != 200:
module.fail_json(msg='Request to Auth0 failed with return code %s, reason: %s'%
(req.status_code, req.reason))

module.exit_json(results=json.loads(req.text))

if __name__ == '__main__':
main()