Python flask.sessions.SecureCookieSessionInterface() Examples

The following are 10 code examples of flask.sessions.SecureCookieSessionInterface(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask.sessions , or try the search function .
Example #1
Source File: app.py    From fbctf-2019-challenges with MIT License 5 votes vote down vote up
def unsign(val):
    scsi = SecureCookieSessionInterface()
    ser = scsi.get_signing_serializer(app)
    return ser.loads(val) 
Example #2
Source File: app.py    From fbctf-2019-challenges with MIT License 5 votes vote down vote up
def sign(val):
    scsi = SecureCookieSessionInterface()
    ser = scsi.get_signing_serializer(app)
    return ser.dumps(val)


# END COOKIE UTILS


# MODEL 
Example #3
Source File: flask_session_cookie_manager2.py    From flask-session-cookie-manager with MIT License 5 votes vote down vote up
def encode(cls, secret_key, session_cookie_structure):
        """ Encode a Flask session cookie """
        try:
            app = MockApp(secret_key)

            session_cookie_structure = dict(ast.literal_eval(session_cookie_structure))
            si = SecureCookieSessionInterface()
            s = si.get_signing_serializer(app)

            return s.dumps(session_cookie_structure)
        except Exception as e:
            return "[Encoding error] {}".format(e)
            raise e 
Example #4
Source File: flask_session_cookie_manager2.py    From flask-session-cookie-manager with MIT License 5 votes vote down vote up
def decode(cls, session_cookie_value, secret_key=None):
        """ Decode a Flask cookie  """
        try:
            if(secret_key==None):
                compressed = False
                payload = session_cookie_value

                if payload.startswith('.'):
                    compressed = True
                    payload = payload[1:]

                data = payload.split(".")[0]

                data = base64_decode(data)
                if compressed:
                    data = zlib.decompress(data)

                return data
            else:
                app = MockApp(secret_key)

                si = SecureCookieSessionInterface()
                s = si.get_signing_serializer(app)

                return s.loads(session_cookie_value)
        except Exception as e:
            return "[Decoding error] {}".format(e)
            raise e 
Example #5
Source File: flask_session_cookie_manager3.py    From flask-session-cookie-manager with MIT License 5 votes vote down vote up
def encode(secret_key, session_cookie_structure):
            """ Encode a Flask session cookie """
            try:
                app = MockApp(secret_key)

                session_cookie_structure = dict(ast.literal_eval(session_cookie_structure))
                si = SecureCookieSessionInterface()
                s = si.get_signing_serializer(app)

                return s.dumps(session_cookie_structure)
            except Exception as e:
                return "[Encoding error] {}".format(e)
                raise e 
Example #6
Source File: flask_session_cookie_manager3.py    From flask-session-cookie-manager with MIT License 5 votes vote down vote up
def decode(session_cookie_value, secret_key=None):
            """ Decode a Flask cookie  """
            try:
                if(secret_key==None):
                    compressed = False
                    payload = session_cookie_value

                    if payload.startswith('.'):
                        compressed = True
                        payload = payload[1:]

                    data = payload.split(".")[0]

                    data = base64_decode(data)
                    if compressed:
                        data = zlib.decompress(data)

                    return data
                else:
                    app = MockApp(secret_key)

                    si = SecureCookieSessionInterface()
                    s = si.get_signing_serializer(app)

                    return s.loads(session_cookie_value)
            except Exception as e:
                return "[Decoding error] {}".format(e)
                raise e 
Example #7
Source File: flask_session_cookie_manager3.py    From flask-session-cookie-manager with MIT License 5 votes vote down vote up
def encode(secret_key, session_cookie_structure):
            """ Encode a Flask session cookie """
            try:
                app = MockApp(secret_key)

                session_cookie_structure = dict(ast.literal_eval(session_cookie_structure))
                si = SecureCookieSessionInterface()
                s = si.get_signing_serializer(app)

                return s.dumps(session_cookie_structure)
            except Exception as e:
                return "[Encoding error] {}".format(e)
                raise e 
Example #8
Source File: signedgrant.py    From quay with Apache License 2.0 5 votes vote down vote up
def generate_signed_token(grants, user_context):
    """
    Generates a signed session token with the given grants and user context.
    """
    ser = SecureCookieSessionInterface().get_signing_serializer(app)
    data_to_sign = {
        "grants": grants,
        "user_context": user_context,
    }

    encrypted = ser.dumps(data_to_sign)
    return "{0}{1}".format(SIGNATURE_PREFIX, encrypted) 
Example #9
Source File: signedgrant.py    From quay with Apache License 2.0 5 votes vote down vote up
def validate_signed_grant(auth_header):
    """
    Validates a signed grant as found inside an auth header and returns whether it points to a valid
    grant.
    """
    if not auth_header:
        return ValidateResult(AuthKind.signed_grant, missing=True)

    # Try to parse the token from the header.
    normalized = [part.strip() for part in auth_header.split(" ") if part]
    if normalized[0].lower() != "token" or len(normalized) != 2:
        logger.debug("Not a token: %s", auth_header)
        return ValidateResult(AuthKind.signed_grant, missing=True)

    # Check that it starts with the expected prefix.
    if not normalized[1].startswith(SIGNATURE_PREFIX):
        logger.debug("Not a signed grant token: %s", auth_header)
        return ValidateResult(AuthKind.signed_grant, missing=True)

    # Decrypt the grant.
    encrypted = normalized[1][len(SIGNATURE_PREFIX) :]
    ser = SecureCookieSessionInterface().get_signing_serializer(app)

    try:
        token_data = ser.loads(encrypted, max_age=app.config["SIGNED_GRANT_EXPIRATION_SEC"])
    except BadSignature:
        logger.warning("Signed grant could not be validated: %s", encrypted)
        return ValidateResult(
            AuthKind.signed_grant, error_message="Signed grant could not be validated"
        )

    logger.debug("Successfully validated signed grant with data: %s", token_data)
    return ValidateResult(AuthKind.signed_grant, signed_data=token_data) 
Example #10
Source File: flaskencoder.py    From Saker with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, secret_key=None):
        self.compressed = False
        if secret_key is None:
            self.session_serializer = None
        else:
            app = Flask(__name__)
            app.secret_key = secret_key
            self.session_serializer = SecureCookieSessionInterface().get_signing_serializer(app)