Authentication
application_kit.authenticator.base
Framework-agnostic authentication helpers.
FastAPI security Django decorators
EndpointMode
Bases: str, Enum
EndpointMode controls how an endpoint should require public or private key based on the request verb.
AuthenticateBase
AuthenticateBase(
classes=None,
token_lambda=None,
product=None,
write_permission_needed=None,
endpoint_mode=UNSET,
)
Base class that encompasses token fetching, and perform instance checks
| PARAMETER | DESCRIPTION |
|---|---|
product
|
The products required to be activated on the project.
TYPE:
|
write_permission_needed
|
Deprecated use endpoint_mode instead.
TYPE:
|
endpoint_mode
|
Controls what kind of permissions are required from the ProjectToken.
TYPE:
|
Source code in application_kit/authenticator/base.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
get_project_token
get_project_token(
current_product,
headers,
key_token,
kind,
request_method,
port,
organization_id=None,
project_id=None,
)
Calls authentication service to get a token for an incoming request.
| PARAMETER | DESCRIPTION |
|---|---|
headers
|
The headers mapping from the request
TYPE:
|
kind
|
The kind of token detected.
TYPE:
|
organization_id
|
organization_id coming from the request path, used with user tokens.
TYPE:
|
project_id
|
project_id coming from the request path, used with user tokens.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[ProjectTokenModel, TokenModel]
|
Returns ProjectTokenModel and a TokenModel, the later will be retired. |
Source code in application_kit/authenticator/base.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
perform_instance_checks
perform_instance_checks(
project_token, headers, request_method
)
Performs permissions, and restrictions checks, returns extra response headers.
Source code in application_kit/authenticator/base.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
get_public_key
get_public_key(parameters, headers)
Gets a public key from request parameters.
Source code in application_kit/authenticator/base.py
207 208 209 210 211 212 213 | |
get_private_key
get_private_key(parameters, headers)
Gets a private key from either the request parameters or headers.
Source code in application_kit/authenticator/base.py
216 217 218 219 220 221 222 223 | |
get_user_access_token
get_user_access_token(parameters, headers)
Gets a user access token using the Authorization request header.
Source code in application_kit/authenticator/base.py
226 227 228 229 230 231 232 233 234 235 236 237 | |
application_kit.authenticator.types
RestrictionModel
Bases: ABC, BaseModel
is_fulfilled
abstractmethod
is_fulfilled(headers)
is_fulfilled protocol method.
Source code in application_kit/authenticator/types.py
107 108 109 | |
KeyTokenInstanceModel
Bases: ABC, BaseModel
Base class for Key token instances.
check_restriction
abstractmethod
check_restriction(headers)
Checks that at least one restriction is fulfilled.
Source code in application_kit/authenticator/types.py
222 223 224 | |
check_permissions
abstractmethod
check_permissions(product, permission_names)
Checks that the instance has the permission for the product.
Source code in application_kit/authenticator/types.py
226 227 228 | |
check_write_needed
check_write_needed(method)
Checks if the method requires write permission.
Note
The following methods require write permission:
POST, PUT, DELETE and PATCH
| PARAMETER | DESCRIPTION |
|---|---|
method
|
The http method used by the request.
TYPE:
|
Source code in application_kit/authenticator/types.py
230 231 232 233 234 235 236 237 238 239 240 | |
PrivateKeyTokenInstanceModel
Bases: KeyTokenInstanceModel
Token instance for a private key.
Note
Retrived from authentication service /access_tokens/ endpoint.
check_write_needed
check_write_needed(method)
Checks if the method requires write permission.
Note
The following methods require write permission:
POST, PUT, DELETE and PATCH
| PARAMETER | DESCRIPTION |
|---|---|
method
|
The http method used by the request.
TYPE:
|
Source code in application_kit/authenticator/types.py
230 231 232 233 234 235 236 237 238 239 240 | |
check_permissions
check_permissions(product, permission_names)
Checks that the instance has the permission for the product.
Warning
This is now defunkt the only permission remaning is write for the stores product.
This is used by every products, and translates to simply "has write permission".
Source code in application_kit/authenticator/types.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
PublicKeyTokenInstanceModel
Bases: KeyTokenInstanceModel
Token instance for a public key.
Note
Retrived from authentication service /access_tokens/ endpoint.
check_write_needed
check_write_needed(method)
Checks if the method requires write permission.
Note
The following methods require write permission:
POST, PUT, DELETE and PATCH
| PARAMETER | DESCRIPTION |
|---|---|
method
|
The http method used by the request.
TYPE:
|
Source code in application_kit/authenticator/types.py
230 231 232 233 234 235 236 237 238 239 240 | |
UserTokenInstanceModel
Bases: BaseModel
Token instance for a user.
Note
Retrived from the Authorization: bearer
check_staff_or_superuser
check_staff_or_superuser()
Checks if user is staff or super user. :raise PermissionDenied :return:
Source code in application_kit/authenticator/types.py
358 359 360 361 362 363 364 365 | |
check_superuser
check_superuser()
Checks if user is super user. :raise PermissionDenied
Source code in application_kit/authenticator/types.py
367 368 369 370 371 372 373 | |
check_membership
check_membership(organization_id)
Checks if user is member of the organization with organization_id.
:raise PermissionDenied
Source code in application_kit/authenticator/types.py
375 376 377 378 379 380 381 382 | |
check_ownership
check_ownership(organization_id)
Checks that user has ownership of the organization.
Source code in application_kit/authenticator/types.py
389 390 391 392 393 | |
check_origin
check_origin(headers)
Checks if the Origin is allowed for User tokens browser interactions.
Source code in application_kit/authenticator/types.py
395 396 397 398 399 400 401 402 403 404 405 406 | |
GenericCallable
Bases: Protocol
__call__
__call__(*args, **kwargs)
Generic callable takes Any as parameters and returns Any.
Source code in application_kit/authenticator/types.py
428 429 | |
ProjectReference
Bases: BaseModel
Project reference. Encapsulates the organization ID and the project ID coming from authentication.
Tip
Used to identify the targeted project by the request. For Private or Public keys tokens it's direct, but for User tokens it's a bit more involved.
User can target multiple projects or different organizations that's why the endpoints accepting
User tokens must have organization_id and project_id path arguments.
organization_id
instance-attribute
organization_id
The organization id in the authentication service.
project_id
class-attribute
instance-attribute
project_id = None
The project id in the authentication service.
trace_to_datadog
trace_to_datadog()
Add project_id, and organization_id tags to the current root span.
Source code in application_kit/authenticator/types.py
447 448 449 450 451 452 453 454 | |
ProjectTokenContext
Context object that avoids to put too much things on the request objects.
should_count
class-attribute
instance-attribute
should_count = True
If the current request should be counted.
Internal requests (eg. request Host port is 8000) are excluded from metrics counters.
source
class-attribute
instance-attribute
source = None
The source pulled from either X-SDK-Source header or if ai-context
if AI-Context header is set to true
ProjectTokenModel
ProjectTokenModel(token_instance, reference=None)
This is the main objects to interact with when using authentication.
| PARAMETER | DESCRIPTION |
|---|---|
token_instance
|
The token instance retrived from authentication service using private key
TYPE:
|
reference
|
The project reference ids.
TYPE:
|
Source code in application_kit/authenticator/types.py
483 484 485 486 487 488 489 490 491 492 | |
instance
instance-attribute
instance = token_instance
The token instance.
reference
instance-attribute
reference = reference
The project reference.
context
instance-attribute
context = ProjectTokenContext()
The current context, contains source and should_count.
products
property
products
Returns the products found in the token.
Warn
User tokens have no products.
check_product_allowed
check_product_allowed(product)
Raises ProductNotAllowed
Source code in application_kit/authenticator/types.py
505 506 507 508 509 510 | |
validate_products
validate_products(value)
Validates a product list, ignores unknown products
Source code in application_kit/authenticator/types.py
243 244 245 246 247 248 249 250 251 | |