An entity representing an API key object. Each API key object is scoped to the user and inherits permissions from that user.
| 86 | |
| 87 | |
| 88 | class ApiKeyDB(stormbase.StormFoundationDB, stormbase.UIDFieldMixin): |
| 89 | """ |
| 90 | An entity representing an API key object. |
| 91 | |
| 92 | Each API key object is scoped to the user and inherits permissions from that user. |
| 93 | """ |
| 94 | |
| 95 | RESOURCE_TYPE = ResourceType.API_KEY |
| 96 | UID_FIELDS = ["key_hash"] |
| 97 | |
| 98 | user = me.StringField(required=True) |
| 99 | key_hash = me.StringField(required=True, unique=True) |
| 100 | metadata = me.DictField( |
| 101 | required=False, help_text="Arbitrary metadata associated with this token" |
| 102 | ) |
| 103 | created_at = ComplexDateTimeField( |
| 104 | default=date_utils.get_datetime_utc_now, |
| 105 | help_text="The creation time of this ApiKey.", |
| 106 | ) |
| 107 | enabled = me.BooleanField( |
| 108 | required=True, |
| 109 | default=True, |
| 110 | help_text="A flag indicating whether the ApiKey is enabled.", |
| 111 | ) |
| 112 | |
| 113 | meta = {"indexes": [{"fields": ["user"]}, {"fields": ["key_hash"]}]} |
| 114 | |
| 115 | def __init__(self, *args, **values): |
| 116 | super(ApiKeyDB, self).__init__(*args, **values) |
| 117 | self.uid = self.get_uid() |
| 118 | |
| 119 | def mask_secrets(self, value): |
| 120 | result = copy.deepcopy(value) |
| 121 | |
| 122 | # In theory the key_hash is safe to return as it is one way. On the other |
| 123 | # hand given that this is actually a secret no real point in letting the hash |
| 124 | # escape. Since uid contains key_hash masking that as well. |
| 125 | result["key_hash"] = MASKED_ATTRIBUTE_VALUE |
| 126 | result["uid"] = MASKED_ATTRIBUTE_VALUE |
| 127 | return result |
| 128 | |
| 129 | |
| 130 | MODELS = [UserDB, TokenDB, ApiKeyDB] |