Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create session cookie #14

Open
Jish2 opened this issue Nov 1, 2023 · 0 comments
Open

create session cookie #14

Jish2 opened this issue Nov 1, 2023 · 0 comments

Comments

@Jish2
Copy link
Member

Jish2 commented Nov 1, 2023

Throw this code in it probably works

import json
import crypto
import boto3

# throw this in a helper folder or sum
def serialize_cookie(cookie_dict):
    cookie_string = ''
    for key, value in cookie_dict.items():
        cookie_string += f'{key}={value}; '
    
    return cookie_string.rstrip('; ')


secrets_manager_client = boto3.client('secretsmanager')

secret_name = 'resume-book-session-cookie-secret'

def lambda_handler(event, context):
    try:
        response = secrets_manager_client.get_secret_value(SecretId=secret_name)
        encryption_key = response['SecretString']
    except Exception as e:
        return {
            "statusCode": 500,
            "body": json.dumps("Error: Failed to retrieve encryption key")
        }

    user_data = ... # get username and password here
    serialized_user_data = json.dumps(user_data)

    cipher = crypto.Cipher(algorithm=crypto.Algorithms.AES, mode=crypto.Modes.CFB, key=encryption_key.encode(), iv=encryption_key.encode())
    encrypted_user_data = cipher.encrypt(serialized_user_data.encode()).hex()

    user_cookie = {
        "name": "userSession",
        "value": encrypted_user_data,
        "httpOnly": True,
        "max-age": 3600,
        "secure": True,
        "sameSite": "Lax"
    }

    serialized_cookie = serialize_cookie(user_cookie)

    response = {
        "statusCode": 200,
        "headers": {
            "Set-Cookie": serialized_cookie,
            "Content-Type": "text/plain"
        },
        "body": "Cookie has been set."
    }

    return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant