| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import Any |
| 4 | |
| 5 | from .base import AuthenticationBase |
| 6 | |
| 7 | |
| 8 | class GetToken(AuthenticationBase): |
| 9 | |
| 10 | """/oauth/token related endpoints |
| 11 | |
| 12 | Args: |
| 13 | domain (str): Your auth0 domain (e.g: username.auth0.com) |
| 14 | """ |
| 15 | |
| 16 | def authorization_code( |
| 17 | self, |
| 18 | code: str, |
| 19 | redirect_uri: str | None, |
| 20 | grant_type: str = "authorization_code", |
| 21 | ) -> Any: |
| 22 | """Authorization code grant |
| 23 | |
| 24 | This is the OAuth 2.0 grant that regular web apps utilize in order |
| 25 | to access an API. Use this endpoint to exchange an Authorization Code |
| 26 | for a Token. |
| 27 | |
| 28 | Args: |
| 29 | code (str): The Authorization Code received from the /authorize Calls |
| 30 | |
| 31 | redirect_uri (str, optional): This is required only if it was set at |
| 32 | the GET /authorize endpoint. The values must match |
| 33 | |
| 34 | grant_type (str): Denotes the flow you're using. For authorization code |
| 35 | use authorization_code |
| 36 | |
| 37 | Returns: |
| 38 | access_token, id_token |
| 39 | """ |
| 40 | |
| 41 | return self.authenticated_post( |
| 42 | f"{self.protocol}://{self.domain}/oauth/token", |
| 43 | data={ |
| 44 | "client_id": self.client_id, |
| 45 | "code": code, |
| 46 | "grant_type": grant_type, |
| 47 | "redirect_uri": redirect_uri, |
| 48 | }, |
| 49 | ) |
| 50 | |
| 51 | def authorization_code_pkce( |
| 52 | self, |
| 53 | code_verifier: str, |
| 54 | code: str, |
| 55 | redirect_uri: str | None, |
| 56 | grant_type: str = "authorization_code", |
| 57 | ) -> Any: |
| 58 | """Authorization code pkce grant |
| 59 | |
| 60 | This is the OAuth 2.0 grant that mobile apps utilize in order to access an API. |
| 61 | Use this endpoint to exchange an Authorization Code for a Token. |
| 62 | |
| 63 | Args: |
| 64 | code_verifier (str): Cryptographically random key that was used to generate |
| 65 | the code_challenge passed to /authorize. |
| 66 | |
| 67 | code (str): The Authorization Code received from the /authorize Calls |
| 68 | |
| 69 | redirect_uri (str, optional): This is required only if it was set at |
| 70 | the GET /authorize endpoint. The values must match |
| 71 | |
| 72 | grant_type (str): Denotes the flow you're using. For authorization code pkce |
| 73 | use authorization_code |
| 74 | |
| 75 | Returns: |
| 76 | access_token, id_token |
| 77 | """ |
| 78 | |
| 79 | return self.post( |
| 80 | f"{self.protocol}://{self.domain}/oauth/token", |
| 81 | data={ |
| 82 | "client_id": self.client_id, |
| 83 | "code_verifier": code_verifier, |
| 84 | "code": code, |
| 85 | "grant_type": grant_type, |
| 86 | "redirect_uri": redirect_uri, |
| 87 | }, |
| 88 | ) |
| 89 | |
| 90 | def client_credentials( |
| 91 | self, |
| 92 | audience: str, |
| 93 | grant_type: str = "client_credentials", |
| 94 | organization: str | None = None, |
| 95 | ) -> Any: |
| 96 | """Client credentials grant |
| 97 | |
| 98 | This is the OAuth 2.0 grant that server processes utilize in |
| 99 | order to access an API. Use this endpoint to directly request |
| 100 | an access_token by using the Application Credentials (a Client Id and |
| 101 | a Client Secret). |
| 102 | |
| 103 | Args: |
| 104 | audience (str): The unique identifier of the target API you want to access. |
| 105 | |
| 106 | grant_type (str, optional): Denotes the flow you're using. For client credentials use "client_credentials" |
| 107 | |
| 108 | organization (str, optional): Optional Organization name or ID. When included, the access token returned |
| 109 | will include the org_id and org_name claims |
| 110 | |
| 111 | Returns: |
| 112 | access_token |
| 113 | """ |
| 114 | |
| 115 | return self.authenticated_post( |
| 116 | f"{self.protocol}://{self.domain}/oauth/token", |
| 117 | data={ |
| 118 | "client_id": self.client_id, |
| 119 | "audience": audience, |
| 120 | "grant_type": grant_type, |
| 121 | "organization": organization, |
| 122 | }, |
| 123 | ) |
| 124 | |
| 125 | def login( |
| 126 | self, |
| 127 | username: str, |
| 128 | password: str, |
| 129 | scope: str | None = None, |
| 130 | realm: str | None = None, |
| 131 | audience: str | None = None, |
| 132 | grant_type: str = "http://auth0.com/oauth/grant-type/password-realm", |
| 133 | forwarded_for: str | None = None, |
| 134 | ) -> Any: |
| 135 | """Calls /oauth/token endpoint with password-realm grant type |
| 136 | |
| 137 | |
| 138 | This is the OAuth 2.0 grant that highly trusted apps utilize in order |
| 139 | to access an API. In this flow the end-user is asked to fill in credentials |
| 140 | (username/password) typically using an interactive form in the user-agent |
| 141 | (browser). This information is later on sent to the client and Auth0. |
| 142 | It is therefore imperative that the client is absolutely trusted with |
| 143 | this information. |
| 144 | |
| 145 | Args: |
| 146 | username (str): Resource owner's identifier |
| 147 | |
| 148 | password (str): resource owner's Secret |
| 149 | |
| 150 | scope(str, optional): String value of the different scopes the client is asking for. |
| 151 | Multiple scopes are separated with whitespace. |
| 152 | |
| 153 | realm (str, optional): String value of the realm the user belongs. |
| 154 | Set this if you want to add realm support at this grant. |
| 155 | |
| 156 | audience (str, optional): The unique identifier of the target API you want to access. |
| 157 | |
| 158 | grant_type (str, optional): Denotes the flow you're using. For password realm |
| 159 | use http://auth0.com/oauth/grant-type/password-realm |
| 160 | |
| 161 | forwarded_for (str, optional): End-user IP as a string value. Set this if you want |
| 162 | brute-force protection to work in server-side scenarios. |
| 163 | See https://auth0.com/docs/get-started/authentication-and-authorization-flow/avoid-common-issues-with-resource-owner-password-flow-and-attack-protection |
| 164 | |
| 165 | Returns: |
| 166 | access_token, id_token |
| 167 | """ |
| 168 | headers = None |
| 169 | if forwarded_for: |
| 170 | headers = {"auth0-forwarded-for": forwarded_for} |
| 171 | |
| 172 | return self.authenticated_post( |
| 173 | f"{self.protocol}://{self.domain}/oauth/token", |
| 174 | data={ |
| 175 | "client_id": self.client_id, |
| 176 | "username": username, |
| 177 | "password": password, |
| 178 | "realm": realm, |
| 179 | "scope": scope, |
| 180 | "audience": audience, |
| 181 | "grant_type": grant_type, |
| 182 | }, |
| 183 | headers=headers, |
| 184 | ) |
| 185 | |
| 186 | def refresh_token( |
| 187 | self, |
| 188 | refresh_token: str, |
| 189 | scope: str = "", |
| 190 | grant_type: str = "refresh_token", |
| 191 | ) -> Any: |
| 192 | """Calls /oauth/token endpoint with refresh token grant type |
| 193 | |
| 194 | Use this endpoint to refresh an access token, using the refresh token you got during authorization. |
| 195 | |
| 196 | Args: |
| 197 | refresh_token (str): The refresh token returned from the initial token request. |
| 198 | |
| 199 | scope (str): Use this to limit the scopes of the new access token. |
| 200 | Multiple scopes are separated with whitespace. |
| 201 | |
| 202 | grant_type (str): Denotes the flow you're using. For refresh token |
| 203 | use refresh_token |
| 204 | |
| 205 | Returns: |
| 206 | access_token, id_token |
| 207 | """ |
| 208 | |
| 209 | return self.authenticated_post( |
| 210 | f"{self.protocol}://{self.domain}/oauth/token", |
| 211 | data={ |
| 212 | "client_id": self.client_id, |
| 213 | "refresh_token": refresh_token, |
| 214 | "scope": scope, |
| 215 | "grant_type": grant_type, |
| 216 | }, |
| 217 | ) |
| 218 | |
| 219 | def passwordless_login( |
| 220 | self, username: str, otp: str, realm: str, scope: str, audience: str |
| 221 | ) -> Any: |
| 222 | """Calls /oauth/token endpoint with http://auth0.com/oauth/grant-type/passwordless/otp grant type |
| 223 | |
| 224 | Once the verification code was received, login the user using this endpoint with their |
| 225 | phone number/email and verification code. |
| 226 | |
| 227 | Args: |
| 228 | username (str): The user's phone number or email address. |
| 229 | |
| 230 | otp (str): the user's verification code. |
| 231 | |
| 232 | realm (str): use 'sms' or 'email'. |
| 233 | Should be the same as the one used to start the passwordless flow. |
| 234 | |
| 235 | scope(str): String value of the different scopes the client is asking for. |
| 236 | Multiple scopes are separated with whitespace. |
| 237 | |
| 238 | audience (str): The unique identifier of the target API you want to access. |
| 239 | |
| 240 | Returns: |
| 241 | access_token, id_token |
| 242 | """ |
| 243 | |
| 244 | return self.authenticated_post( |
| 245 | f"{self.protocol}://{self.domain}/oauth/token", |
| 246 | data={ |
| 247 | "client_id": self.client_id, |
| 248 | "username": username, |
| 249 | "otp": otp, |
| 250 | "realm": realm, |
| 251 | "scope": scope, |
| 252 | "audience": audience, |
| 253 | "grant_type": "http://auth0.com/oauth/grant-type/passwordless/otp", |
| 254 | }, |
| 255 | ) |
| 256 | |
| 257 | def backchannel_login( |
| 258 | self, auth_req_id: str, grant_type: str = "urn:openid:params:grant-type:ciba", |
| 259 | ) -> Any: |
| 260 | """Calls /oauth/token endpoint with "urn:openid:params:grant-type:ciba" grant type |
| 261 | |
| 262 | Args: |
| 263 | auth_req_id (str): The id received from /bc-authorize |
| 264 | |
| 265 | grant_type (str): Denotes the flow you're using.For Back Channel login |
| 266 | use urn:openid:params:grant-type:ciba |
| 267 | |
| 268 | Returns: |
| 269 | access_token, id_token, refresh_token, token_type, expires_in, scope and authorization_details |
| 270 | """ |
| 271 | |
| 272 | return self.authenticated_post( |
| 273 | f"{self.protocol}://{self.domain}/oauth/token", |
| 274 | data={ |
| 275 | "client_id": self.client_id, |
| 276 | "auth_req_id": auth_req_id, |
| 277 | "grant_type": grant_type, |
| 278 | }, |
| 279 | ) |
| 280 | |
| 281 | def access_token_for_connection( |
| 282 | self, |
| 283 | subject_token_type: str, |
| 284 | subject_token: str, |
| 285 | requested_token_type: str, |
| 286 | connection: str | None = None, |
| 287 | grant_type: str = "urn:auth0:params:oauth:grant-type:token-exchange:federated-connection-access-token", |
| 288 | login_hint: str = None |
| 289 | ) -> Any: |
| 290 | """Calls /oauth/token endpoint with federated-connection-access-token grant type |
| 291 | |
| 292 | Args: |
| 293 | subject_token_type (str): String containing the type of token. |
| 294 | |
| 295 | subject_token (str): String containing the value of subject_token_type. |
| 296 | |
| 297 | requested_token_type (str): String containing the type of requested token. |
| 298 | |
| 299 | connection (str, optional): Denotes the name of a social identity provider configured to your application |
| 300 | |
| 301 | login_hint (str, optional): A hint to the OpenID Provider regarding the end-user for whom authentication is being requested |
| 302 | |
| 303 | Returns: |
| 304 | access_token, scope, issued_token_type, token_type, expires_in |
| 305 | """ |
| 306 | |
| 307 | data = { |
| 308 | "client_id": self.client_id, |
| 309 | "grant_type": grant_type, |
| 310 | "subject_token_type": subject_token_type, |
| 311 | "subject_token": subject_token, |
| 312 | "requested_token_type": requested_token_type, |
| 313 | "connection": connection, |
| 314 | } |
| 315 | |
| 316 | if login_hint: |
| 317 | data["login_hint"] = login_hint |
| 318 | |
| 319 | return self.authenticated_post( |
| 320 | f"{self.protocol}://{self.domain}/oauth/token", |
| 321 | data=data, |
| 322 | ) |