|
2 | 2 |
|
3 | 3 | - [Authentication SDK](#authentication-sdk)
|
4 | 4 | - [ID token validation](#id-token-validation)
|
5 |
| - - [Organizations](#organizations) |
| 5 | + - [Authenticating with a application configured to use `private_key_jwt` token endpoint auth method](#authenticating-with-a-application-configured-to-use-private-key-jwt-token-endpoint-auth-method) |
6 | 6 | - [Management SDK](#management-sdk)
|
7 | 7 | - [Connections](#connections)
|
8 | 8 | - [Error handling](#error-handling)
|
@@ -50,102 +50,26 @@ tv.verify(id_token)
|
50 | 50 |
|
51 | 51 | If the token verification fails, a `TokenValidationError` will be raised. In that scenario, the ID token should be deemed invalid and its contents should not be trusted.
|
52 | 52 |
|
53 |
| - |
54 |
| - |
55 |
| -### Organizations |
56 |
| - |
57 |
| -[Organizations](https://auth0.com/docs/organizations) is a set of features that provide better support for developers who build and maintain SaaS and Business-to-Business (B2B) applications. |
58 |
| - |
59 |
| -Note that Organizations is currently only available to customers on our Enterprise and Startup subscription plans. |
60 |
| - |
61 |
| - |
62 |
| -#### Log in to an organization |
63 |
| - |
64 |
| -Log in to an organization by specifying the ``organization`` property when calling ``authorize()``: |
65 |
| - |
66 |
| -```python |
67 |
| -from auth0.v3.authentication.authorize_client import AuthorizeClient |
68 |
| - |
69 |
| -client = AuthorizeClient('my.domain.com') |
70 |
| - |
71 |
| -client.authorize(client_id='client_id', |
72 |
| - redirect_uri='http://localhost', |
73 |
| - organization="org_abc") |
74 |
| -``` |
75 |
| - |
76 |
| -When logging into an organization, it is important to ensure the `org_id` claim of the ID Token matches the expected organization value. The `TokenVerifier` can be be used to ensure the ID Token contains the expected `org_id` claim value: |
77 |
| - |
78 |
| -```python |
79 |
| -from auth0.v3.authentication.token_verifier import TokenVerifier, AsymmetricSignatureVerifier |
80 |
| - |
81 |
| -domain = 'myaccount.auth0.com' |
82 |
| -client_id = 'exampleid' |
83 |
| - |
84 |
| -# After authenticating |
85 |
| -id_token = auth_result['id_token'] |
86 |
| - |
87 |
| -jwks_url = 'https://{}/.well-known/jwks.json'.format(domain) |
88 |
| -issuer = 'https://{}/'.format(domain) |
89 |
| - |
90 |
| -sv = AsymmetricSignatureVerifier(jwks_url) # Reusable instance |
91 |
| -tv = TokenVerifier(signature_verifier=sv, issuer=issuer, audience=client_id) |
92 |
| - |
93 |
| -# pass the expected organization the user logged in to: |
94 |
| -tv.verify(id_token, organization='org_abc') |
95 |
| - |
96 |
| -``` |
97 |
| - |
98 |
| -#### Accept user invitations |
99 |
| - |
100 |
| -Accept a user invitation by specifying the `invitation` property when calling `authorize()`. Note that you must also specify the ``organization`` if providing an ``invitation``. |
101 |
| -The ID of the invitation and organization are available as query parameters on the invitation URL, e.g., ``https://your-domain.auth0.com/login?invitation=invitation_id&organization=org_id&organization_name=org_name`` |
102 |
| - |
103 |
| -```python |
104 |
| -from auth0.v3.authentication.authorize_client import AuthorizeClient |
105 |
| - |
106 |
| -client = AuthorizeClient('my.domain.com') |
107 |
| - |
108 |
| -client.authorize(client_id='client_id', |
109 |
| - redirect_uri='http://localhost', |
110 |
| - organization='org_abc', |
111 |
| - invitation="invitation_123") |
112 |
| -``` |
113 |
| - |
114 |
| -#### Authorizing users from an Organization |
115 |
| - |
116 |
| -If an `org_id` claim is present in the Access Token, then the claim should be validated by the API to ensure that the value received is expected or known. |
117 |
| - |
118 |
| -In particular: |
119 |
| - |
120 |
| -- The issuer (`iss`) claim should be checked to ensure the token was issued by Auth0 |
121 |
| -- The organization ID (`org_id`) claim should be checked to ensure it is a value that is already known to the application. This could be validated against a known list of organization IDs, or perhaps checked in conjunction with the current request URL. e.g. the sub-domain may hint at what organization should be used to validate the Access Token. |
122 |
| - |
123 |
| -Normally, validating the issuer would be enough to ensure that the token was issued by Auth0. In the case of organizations, additional checks should be made so that the organization within an Auth0 tenant is expected. |
124 |
| - |
125 |
| -If the claim cannot be validated, then the application should deem the token invalid. |
126 |
| - |
127 |
| -The snippet below attempts to illustrate how this verification could look like using the external [PyJWT](https://pyjwt.readthedocs.io/en/latest/usage.html#encoding-decoding-tokens-with-rs256-rsa) library. This dependency will take care of pulling the RS256 Public Key that was used by the server to sign the Access Token. It will also validate its signature, expiration, and the audience value. After the basic verification, get the `org_id` claim and check it against the expected value. The code assumes your application is configured to sign tokens using the RS256 algorithm. Check the [Validate JSON Web Tokens](https://auth0.com/docs/tokens/json-web-tokens/validate-json-web-tokens) article to learn more about this verification. |
| 53 | +### Authenticating with a application configured to use `private_key_jwt` token endpoint auth method |
128 | 54 |
|
129 | 55 | ```python
|
130 |
| -import jwt # PyJWT |
131 |
| -from jwt import PyJWKClient |
132 |
| - |
133 |
| -access_token = # access token from the request |
134 |
| -url = 'https://{YOUR AUTH0 DOMAIN}/.well-known/jwks.json' |
135 |
| -jwks_client = PyJWKClient(url) |
136 |
| -signing_key = jwks_client.get_signing_key_from_jwt(access_token) |
137 |
| -data = jwt.decode( |
138 |
| - access_token, |
139 |
| - signing_key.key, |
140 |
| - algorithms=['RS256'], |
141 |
| - audience='{YOUR API AUDIENCE}' |
| 56 | +from auth0.v3.authentication import GetToken |
| 57 | + |
| 58 | +private_key = """-----BEGIN RSA PRIVATE KEY----- |
| 59 | +MIIJKQIBAAKCAgEAwfUb0nUC0aKB3WiytFhnCIg455BYC+dR3MUGadWpIg7S6lbi |
| 60 | +... |
| 61 | +2tjIvH4GN9ZkIGwzxIOP61wkUGwGaIzacOTIWOvqRI0OaYr9U18Ep1trvgGR |
| 62 | +-----END RSA PRIVATE KEY----- |
| 63 | +""" |
| 64 | + |
| 65 | +get_token = GetToken( |
| 66 | + "my-domain.us.auth0.com", |
| 67 | + "my-client-id", |
| 68 | + client_assertion_signing_key=private_key, |
| 69 | +) |
| 70 | +token = get_token.client_credentials( |
| 71 | + "https://my-domain.us.auth0.com/api/v2/" |
142 | 72 | )
|
143 |
| - |
144 |
| -organization = # expected organization ID |
145 |
| -if data['org_id'] != organization: |
146 |
| - raise Exception('Organization (org_id) claim mismatch') |
147 |
| - |
148 |
| -# if this line is reached, validation is successful |
149 | 73 | ```
|
150 | 74 |
|
151 | 75 | ## Management SDK
|
|
0 commit comments