Skip to content

Commit

Permalink
Merge pull request #119 from smswithoutborders/feature/grpc_api
Browse files Browse the repository at this point in the history
feat: add token overwrite to StoreEntityToken gRPC function.
  • Loading branch information
PromiseFru authored Jul 10, 2024
2 parents caac590 + 35bb30e commit 7844a5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
5 changes: 1 addition & 4 deletions src/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ class Meta:

database = database
table_name = "tokens"
indexes = (
(("platform", "eid", "account_identifier_hash"), True),
(("account_identifier_hash",), True),
)
indexes = ((("platform", "account_identifier_hash"), True),)


if Configurations.MODE in ("production", "development"):
Expand Down
24 changes: 9 additions & 15 deletions src/grpc_entity_internal_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,22 @@ def StoreEntityToken(self, request, context):

response = vault_pb2.StoreEntityTokenResponse

def check_existing_token(entity_obj, account_identifier_hash):
existing_tokens = fetch_entity_tokens(
entity=entity_obj,
def check_existing_token(account_identifier_hash):
token = find_token(
account_identifier_hash=account_identifier_hash,
platform=request.platform,
)

if existing_tokens:
return error_response(
context,
response,
"Entity already has a token associated with account "
f"identifier {request.account_identifier} for {request.platform}",
grpc.StatusCode.ALREADY_EXISTS,
)
if token:
token.account_tokens = encrypt_and_encode(request.token)
token.save()
logger.info("Token overwritten successfully.")

if find_token(account_identifier_hash=account_identifier_hash):
return error_response(
context,
response,
"An entity already has a token associated with the account "
f"identifier '{request.account_identifier}'.",
"A token is already associated with the account identifier "
f"'{request.account_identifier}'.",
grpc.StatusCode.ALREADY_EXISTS,
)

Expand Down Expand Up @@ -216,7 +210,7 @@ def check_existing_token(entity_obj, account_identifier_hash):
HASHING_KEY, request.account_identifier
)

existing_token = check_existing_token(entity_obj, account_identifier_hash)
existing_token = check_existing_token(account_identifier_hash)

if existing_token:
return existing_token
Expand Down

0 comments on commit 7844a5d

Please sign in to comment.