bedrock.security

 1class Token(object):  # pragma: no cover
 2    """
 3    Generic token class
 4
 5    Inherit from this class to add more functionality to your tokens, such as what things the token can access, etc.
 6
 7    For example, if you must get a list of permissions or specific entities that define what you have access to, you can inherit from this class and add those attributes.
 8    If you do this, you must tell your service how to create a token from a JWT string by calling `set_token_constructor` with a function that creates your token in the
 9    lambda handler (`handler.template.py`) and the local handler (`local.handler.py`).
10    ```python
11from bedrock.security import Token
12
13
14class TokenWithAccounts(Token):
15    __schema__ = \"""Auth:
16  type: object
17  properties:
18    accessToken:
19      type: string
20    permissions:
21      type: object
22    accounts:
23      type: array
24      items:
25        type: string\"""
26
27    def __init__(self, access_token: str, accounts: list[str]):
28        super().__init__(access_token=access_token)
29        self.accounts = accounts
30
31    def __repr__(self) -> str:  # pragma: no cover
32        return f"TokenWithAccounts(access_token={self.access_token}, accounts={self.accounts}, permissions={self.permissions})"
33
34    ```
35    """
36
37    def __init__(self, access_token: str, permissions: dict = {}):
38        self.access_token = access_token
39        self.permissions = permissions
40
41    def __repr__(self) -> str:  # pragma: no cover
42        return f"Token(access_token={self.access_token})"
43
44    def has_permission(self, permission: str) -> bool:  # pragma: no cover
45        try:
46            return self.permissions[permission] == "ENABLED"
47        except KeyError:
48            return False
class Token:
 2class Token(object):  # pragma: no cover
 3    """
 4    Generic token class
 5
 6    Inherit from this class to add more functionality to your tokens, such as what things the token can access, etc.
 7
 8    For example, if you must get a list of permissions or specific entities that define what you have access to, you can inherit from this class and add those attributes.
 9    If you do this, you must tell your service how to create a token from a JWT string by calling `set_token_constructor` with a function that creates your token in the
10    lambda handler (`handler.template.py`) and the local handler (`local.handler.py`).
11    ```python
12from bedrock.security import Token
13
14
15class TokenWithAccounts(Token):
16    __schema__ = \"""Auth:
17  type: object
18  properties:
19    accessToken:
20      type: string
21    permissions:
22      type: object
23    accounts:
24      type: array
25      items:
26        type: string\"""
27
28    def __init__(self, access_token: str, accounts: list[str]):
29        super().__init__(access_token=access_token)
30        self.accounts = accounts
31
32    def __repr__(self) -> str:  # pragma: no cover
33        return f"TokenWithAccounts(access_token={self.access_token}, accounts={self.accounts}, permissions={self.permissions})"
34
35    ```
36    """
37
38    def __init__(self, access_token: str, permissions: dict = {}):
39        self.access_token = access_token
40        self.permissions = permissions
41
42    def __repr__(self) -> str:  # pragma: no cover
43        return f"Token(access_token={self.access_token})"
44
45    def has_permission(self, permission: str) -> bool:  # pragma: no cover
46        try:
47            return self.permissions[permission] == "ENABLED"
48        except KeyError:
49            return False

Generic token class

Inherit from this class to add more functionality to your tokens, such as what things the token can access, etc.

For example, if you must get a list of permissions or specific entities that define what you have access to, you can inherit from this class and add those attributes.
If you do this, you must tell your service how to create a token from a JWT string by calling `set_token_constructor` with a function that creates your token in the
lambda handler (`handler.template.py`) and the local handler (`local.handler.py`).


from bedrock.security import Token


class TokenWithAccounts(Token):
    __schema__ = """Auth:
  type: object
  properties:
    accessToken:
      type: string
    permissions:
      type: object
    accounts:
      type: array
      items:
        type: string"""

    def __init__(self, access_token: str, accounts: list[str]):
        super().__init__(access_token=access_token)
        self.accounts = accounts

    def __repr__(self) -> str:  # pragma: no cover
        return f"TokenWithAccounts(access_token={self.access_token}, accounts={self.accounts}, permissions={self.permissions})"
Token(access_token: str, permissions: dict = {})
38    def __init__(self, access_token: str, permissions: dict = {}):
39        self.access_token = access_token
40        self.permissions = permissions
access_token
permissions
def has_permission(self, permission: str) -> bool:
45    def has_permission(self, permission: str) -> bool:  # pragma: no cover
46        try:
47            return self.permissions[permission] == "ENABLED"
48        except KeyError:
49            return False