Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d067b95

Browse files
committed
Merge pull request auth0#22 from sophilabs/v2
Auth API Implementation
2 parents f4c0875 + 515cf83 commit d067b95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2116
-184
lines changed

README.rst

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Auth0 helps you to:
1818
* Add authentication with `multiple authentication sources <https://docs.auth0.com/identityproviders>`_,
1919
either social like **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, amont others**,
2020
or enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider**.
21-
* Add authentication through more traditional **[username/password databases](https://docs.auth0.com/mysql-connection-tutorial)**.
22-
* Add support for **`linking different user accounts <https://docs.auth0.com/link-accounts>`_** with the same user.
21+
* Add authentication through more traditional `username/password databases <https://docs.auth0.com/mysql-connection-tutorial>`_.
22+
* Add support for `linking different user accounts <https://docs.auth0.com/link-accounts>`_ with the same user.
2323
* Support for generating signed `Json Web Tokens <https://docs.auth0.com/jwt>`_ to call your APIs and **flow the user identity** securely.
2424
* Analytics of how, when and where users are logging in.
2525
* Pull data from other sources and add it to the user profile, through `JavaScript rules <https://docs.auth0.com/rules>`_.
@@ -48,6 +48,135 @@ Author
4848

4949
`Auth0`_
5050

51+
============
52+
Installation
53+
============
54+
55+
You can install the auth0 python SDK issuing the following command.
56+
57+
.. code-block::
58+
59+
pip install auth0-python
60+
61+
====================
62+
Management SDK Usage
63+
====================
64+
65+
To use the management library you will need to instantiate an Auth0 object with a domain and a token.
66+
67+
68+
.. code-block:: python
69+
70+
from auth0.v2.management import Auth0
71+
72+
domain = 'myaccount.auth0.com'
73+
token = '{A_JWT_TOKEN}' # You can generate one of these by using the
74+
# token generator at: https://auth0.com/docs/api/v2
75+
76+
auth0 = Auth0('myuser.auth0.com', token)
77+
78+
The ``Auth0()`` object is now ready to take orders!
79+
Let's see how we can use this to get all available connections.
80+
(this action requires the token to have the following scope: ``read:connections``)
81+
82+
.. code-block:: python
83+
84+
auth0.connections.all()
85+
86+
Which will yield a list of connections similar to this:
87+
88+
.. code-block:: python
89+
90+
[
91+
{
92+
'enabled_clients': [u'rOsnWgtw23nje2QCDuDJNVpxlsCylSLE'],
93+
'id': u'con_ErZf9LpXQDE0cNBr',
94+
'name': u'Amazon-Connection',
95+
'options': {u'profile': True, u'scope': [u'profile']},
96+
'strategy': u'amazon'
97+
},
98+
{
99+
'enabled_clients': [u'rOsnWgtw23nje2QCDuDJNVpxlsCylSLE'],
100+
'id': u'con_i8qF5DPiZ3FdadwJ',
101+
'name': u'Username-Password-Authentication',
102+
'options': {u'brute_force_protection': True},
103+
'strategy': u'auth0'
104+
}
105+
]
106+
107+
Modifying an existing connection is equally as easy. Let's change the name
108+
of connection ``'con_ErZf9LpXQDE0cNBr'``.
109+
(The token will need scope: ``update:connections`` to make this one work)
110+
111+
.. code-block:: python
112+
113+
auth0.connections.update('con_ErZf9LpXQDE0cNBr', {'name': 'MyNewName'})
114+
115+
That's it! using the ``get`` method of the connections endpoint we can verify
116+
that the rename actually happened.
117+
118+
.. code-block:: python
119+
120+
modified_connection = auth0.connections.get('con_ErZf9LpXQDE0cNBr')
121+
122+
Which returns something like this
123+
124+
.. code-block:: python
125+
126+
{
127+
'enabled_clients': [u'rOsnWgtw23nje2QCDuDJNVpxlsCylSLE'],
128+
'id': u'con_ErZf9LpXQDE0cNBr',
129+
'name': u'MyNewName',
130+
'options': {u'profile': True, u'scope': [u'profile']},
131+
'strategy': u'amazon'
132+
}
133+
134+
Success!
135+
136+
All endpoints follow a similar structure to the ``connections`` one, and try to follow as
137+
closely as possible the `API documentation <https://auth0.com/docs/api/v2>`_.
138+
139+
========================
140+
Authentication SDK Usage
141+
========================
142+
143+
The Authentication SDK is divided into components mimicking the structure of the
144+
`API's documentation <https://auth0.com/docs/auth-api>`_.
145+
For example:
146+
147+
.. code-block:: python
148+
149+
from auth0.v2.authentication import Social
150+
151+
social = Social('myaccount.auth0.com')
152+
153+
s.login(client_id='...', acces_token='...', connection='facebook')
154+
155+
Available Management Endpoints
156+
==============================
157+
158+
- Clients() ( ``Auth0().clients`` )
159+
- Connections() ( ``Auth0().connections`` )
160+
- DeviceCredentials() ( ``Auth0().device_credentials`` )
161+
- Rules() ( ``Auth0().rules`` )
162+
- Users() ( ``Auth0().users`` )
163+
- Blacklists() ( ``Auth0().blacklists`` )
164+
- Emails() ( ``Auth0().emails`` )
165+
- Jobs() ( ``Auth0().jobs`` )
166+
- Stats() ( ``Auth0().stats`` )
167+
- Tenants() ( ``Auth0().tenants`` )
168+
169+
Available Authentication Endpoints
170+
==================================
171+
172+
- Users ( ``authentication.Users`` )
173+
- Database ( ``authentication.Database`` )
174+
- Delegated ( ``authentication.Delegated`` )
175+
- Enterprise ( ``authentication.Enterprise`` )
176+
- Link ( ``authentication.Link`` )
177+
- Passwordless ( ``authentication.Passwordless`` )
178+
- Social ( ``authentication.Social`` )
179+
51180
==========
52181
Contribute
53182
==========
@@ -71,7 +200,7 @@ file for more info.
71200

72201
.. _Auth0: https://auth0.com
73202

74-
.. |pypi| image:: https://img.shields.io/pypi/v/auth0.svg?style=flat-square&label=latest%20version
203+
.. |pypi| image:: https://img.shields.io/pypi/v/auth0-python.svg?style=flat-square&label=latest%20version
75204
:target: https://pypi.python.org/pypi/auth0-python
76205
:alt: Latest version released on PyPi
77206

auth0/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '2.0.0b4'

auth0/v2/__init__.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
1-
from .auth0 import Auth0
21
from .exceptions import Auth0Error
3-
from .connections import Connections
4-
from .clients import Clients
5-
from .device_credentials import DeviceCredentials
6-
from .blacklists import Blacklists
7-
from .emails import Emails
8-
from .jobs import Jobs
9-
from .rules import Rules
10-
from .stats import Stats
11-
from .tickets import Tickets
12-
from .users import Users
13-
from .tenants import Tenants

auth0/v2/auth0.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

auth0/v2/authentication/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .database import Database
2+
from .delegated import Delegated
3+
from .enterprise import Enterprise
4+
from .link import Link
5+
from .passwordless import Passwordless
6+
from .social import Social
7+
from .users import Users

auth0/v2/authentication/base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import json
2+
import requests
3+
from ..exceptions import Auth0Error
4+
5+
6+
class AuthenticationBase(object):
7+
8+
def post(self, url, data={}, headers={}):
9+
response = requests.post(url=url, data=json.dumps(data),
10+
headers=headers)
11+
return self._process_response(response)
12+
13+
def get(self, url, params={}, headers={}):
14+
return requests.get(url=url, params=params, headers=headers).text
15+
16+
def _process_response(self, response):
17+
text = json.loads(response.text) if response.text else {}
18+
19+
if 'error' in text:
20+
raise Auth0Error(status_code=text['error'],
21+
error_code=text['error'],
22+
message=text['error_description'])
23+
return text

auth0/v2/authentication/database.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from .base import AuthenticationBase
2+
3+
4+
class Database(AuthenticationBase):
5+
6+
"""Database & Active Directory / LDAP Authentication.
7+
8+
Args:
9+
domain (str): Your auth0 domain (e.g: username.auth0.com)
10+
"""
11+
12+
def __init__(self, domain):
13+
self.domain = domain
14+
15+
def login(self, client_id, username, password, connection, id_token=None,
16+
grant_type='password', device=None):
17+
"""Login using username and password
18+
19+
Given the user credentials and the connection specified, it will do
20+
the authentication on the provider and return a dict with the
21+
access_token and id_token. This endpoint only works for database
22+
connections, passwordless connections, Active Directory/LDAP,
23+
Windows Azure AD and ADFS.
24+
"""
25+
26+
return self.post(
27+
'https://%s/oauth/ro' % self.domain,
28+
data={
29+
'client_id': client_id,
30+
'username': username,
31+
'password': password,
32+
'id_token': id_token,
33+
'connection': connection,
34+
'device': device,
35+
'grant_type': grant_type,
36+
'scope': 'openid',
37+
},
38+
headers={'Content-Type': 'application/json'}
39+
)
40+
41+
def signup(self, client_id, email, password, connection):
42+
"""Signup using username and password.
43+
"""
44+
45+
return self.post(
46+
'https://%s/dbconnections/signup' % self.domain,
47+
data={
48+
'client_id': client_id,
49+
'email': email,
50+
'password': password,
51+
'connection': connection,
52+
},
53+
headers={'Content-Type': 'application/json'}
54+
)
55+
56+
def change_password(self, client_id, email, connection, password=None):
57+
"""Asks to change a password for a given user.
58+
"""
59+
60+
return self.post(
61+
'https://%s/dbconnections/change_password' % self.domain,
62+
data={
63+
'client_id': client_id,
64+
'email': email,
65+
'password': password,
66+
'connection': connection,
67+
},
68+
headers={'Content-Type': 'application/json'}
69+
)

auth0/v2/authentication/delegated.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from .base import AuthenticationBase
2+
3+
4+
class Delegated(AuthenticationBase):
5+
6+
"""Delegated authentication endpoints.
7+
8+
Args:
9+
domain (str): Your auth0 domain (e.g: username.auth0.com)
10+
"""
11+
12+
def __init__(self, domain):
13+
self.domain = domain
14+
15+
def get_token(self, client_id, target, api_type, grant_type,
16+
id_token=None, refresh_token=None):
17+
18+
"""Obtain a delegation token.
19+
"""
20+
21+
if id_token and refresh_token:
22+
raise ValueError('Only one of id_token or refresh_token '
23+
'can be None')
24+
25+
data = {
26+
'client_id': client_id,
27+
'grant_type': grant_type,
28+
'target': target,
29+
'scope': 'openid',
30+
'api_type': api_type,
31+
}
32+
33+
if id_token:
34+
data.update({'id_token': id_token})
35+
elif refresh_token:
36+
data.update({'refresh_token': refresh_token})
37+
else:
38+
raise ValueError('Either id_token or refresh_token must '
39+
'have a value')
40+
41+
return self.post(
42+
'https://%s/delegation' % self.domain,
43+
headers={'Content-Type': 'application/json'},
44+
data=data
45+
)

auth0/v2/authentication/enterprise.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .base import AuthenticationBase
2+
3+
4+
class Enterprise(AuthenticationBase):
5+
6+
"""Enterprise endpoints.
7+
8+
Args:
9+
domain (str): Your auth0 domain (e.g: username.auth0.com)
10+
"""
11+
12+
def __init__(self, domain):
13+
self.domain = domain
14+
15+
def saml_metadata(self, client_id):
16+
"""Get SAML2.0 Metadata.
17+
18+
Args:
19+
client_id (str): Id of the client to get the SAML metadata for.
20+
"""
21+
22+
return self.get(url='https://%s/samlp/metadata/%s' % (self.domain,
23+
client_id))
24+
25+
def wsfed_metadata(self):
26+
"""Returns the WS-Federation Metadata.
27+
"""
28+
29+
url = 'https://%s/wsfed/FederationMetadata' \
30+
'/2007-06/FederationMetadata.xml'
31+
32+
return self.get(url=url % self.domain)

0 commit comments

Comments
 (0)