Skip to content

Commit

Permalink
Fixed wrong function checking challenge time, optimized function load…
Browse files Browse the repository at this point in the history
… for arc and rc, picture S3 in env var
  • Loading branch information
tlegoc committed Aug 9, 2023
1 parent c777885 commit b3af2fe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
20 changes: 3 additions & 17 deletions accept_request_challenge/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import boto3, json, os, jwt, time

import boto3, json, os, time
from jwt import decode

def lambda_handler(event, context):
try:
token = jwt.decode(event['headers']['Authorization'].replace('Bearer ', ''), algorithms=['RS256'],
token = decode(event['headers']['Authorization'].replace('Bearer ', ''), algorithms=['RS256'],
options={"verify_signature": False})

if 'body' not in event:
Expand Down Expand Up @@ -50,20 +50,6 @@ def lambda_handler(event, context):
'body': json.dumps({"message": 'Player didn\'t request this challenge'})
}

# Get challenge
challenge_table = dynamodb.Table(os.environ['CHALLENGES_TABLE'])
challenge = challenge_table.get_item(
Key={
'challenge': body['challenge']
}
)['Item']

if challenge['start'] > int(time.time()) or challenge['end'] < int(time.time()):
return {
'statusCode': 400,
'body': json.dumps({"message": 'Challenge not active'})
}

index = response['Item']['challenges_pending'].index(body['challenge'])

response = user_table.update_item(
Expand Down
14 changes: 12 additions & 2 deletions request_challenge/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json, os, boto3, jwt
import json, os, boto3
from time import time
from jwt import decode


def lambda_handler(event, context):
try:
token = jwt.decode(event['headers']['Authorization'].replace('Bearer ', ''), algorithms=['RS256'],
token = decode(event['headers']['Authorization'].replace('Bearer ', ''), algorithms=['RS256'],
options={"verify_signature": False})

dynamodb = boto3.resource('dynamodb')
Expand Down Expand Up @@ -33,6 +35,14 @@ def lambda_handler(event, context):
'body': json.dumps({"message": 'You have already completed this challenge the maximum number of times'})
}

t = int(time())
print(f'Time : {t}')
if challenge['start'] > t or challenge['end'] < t:
return {
'statusCode': 400,
'body': json.dumps({"message": 'Challenge not active'})
}

# Add the challenge to the user's pending challenges
user_table.update_item(
Key={
Expand Down
1 change: 1 addition & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Globals:
USER_TABLE: !Ref WeiAppUsers
CHALLENGES_TABLE: !Ref WeiAppChallenges
TEAMS_TABLE: !Ref WeiAppTeams
PICTURE_BUCKET: !Ref WeiAppPictureStorage

Resources:
WeiAppApi:
Expand Down

0 comments on commit b3af2fe

Please sign in to comment.