forked from opplafy/bae-tenant-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_service.py
52 lines (37 loc) · 1.66 KB
/
token_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Future Internet Consulting and Development Solutions S.L.
# This file is part of BAE Tenant Service plugin.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import base64
import requests
from urlparse import urljoin
from wstore.asset_manager.resource_plugins.plugin_error import PluginError
from settings import IDM_USER, IDM_PASSWD, IDM_URL, CLIENT_ID, CLIENT_SECRET
TOKEN_SERVICE_ENDPOINT = '/oauth2/password'
def get_token():
url = urljoin(IDM_URL, TOKEN_SERVICE_ENDPOINT)
params = {
'grant_type': 'password',
'username': IDM_USER,
'password': IDM_PASSWD
}
credentials = base64.b64encode('{}:{}'.format(CLIENT_ID, CLIENT_SECRET))
resp = requests.post(url, data=params, headers={
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + credentials
})
if resp.status_code != 200:
raise PluginError('It was not possible to generate a valid access token')
return resp.json()['access_token']