<commit_before>package OpenXPKI::Server::Session::DriverRole;
use Moose::Role;
use utf8;

# CPAN modules
use JSON;

# Project modules
use OpenXPKI::Exception;

=head1 NAME

OpenXPKI::Server::Session::DriverRole - Moose role that every session driver
implementation has to consume

=cut

################################################################################
# Required in session implementations that consume this role
#
requires 'save';              # argument: $session, should write the attributes to the storage
requires 'load';              # argument: $id, should load data from storage and return a HashRef
requires 'delete_all_before'; # argument: $epoch, should delete all sessions which were created before the given timestamp

################################################################################
# Methods
#
# Please note that some method names are intentionally chosen to contain action
# prefixes like "get_" to distinct them from the accessor methods of the session
# attributes (data).
#

=head1 REQUIRED METHODS

The following methods are implemented in driver classes that consume this
Moose role.

=head2 save

Writes the session data to the backend storage.

B<Parameters>

=over

=item * $session - a L<OpenXPKI::Server::Session::Data> object

=back

=head2 load

Loads session data from the backend storage.

Returns a L<OpenXPKI::Server::Session::Data> object or I<undef> if the requested
session was not found..

B<Parameters>

=over

=item * $id - ID of the session whose data is to be loaded

=back

=head2 delete_all_before

Deletes all sessions from the backend storage which were created before the
given timestamp.

B<Parameters>

=over

=item * $epoch - timestamp

=back

=head1 STATIC METHODS

=head2 new

Constructor that creates a new session with an empty data object.

=cut

=head1 METHODS

=head2 freeze

Serializes the given HashRef into a string. The first characters of the
string until the first colon indicate the type of serialization (encoder).

Returns a string with the serialized data.

=cut
sub freeze {
    my ($self, $data) = @_;
    return "JSON:".encode_json($data);
}

=head2 thaw

Deserializes the session attributes from a string and returns them as C<HashRef>.

The first characters of the string until the first colon must indicate the type
of serialization (encoder).

=cut
sub thaw {
    my ($self, $frozen) = @_;

    OpenXPKI::Exception->throw(message => "Unknown format of serialized data")
        unless $frozen =~ /^JSON:/;
    $frozen =~ s/^JSON://;

    my $data = decode_json($frozen);
    return $data;
}

1;
<commit_msg>Add compatibility for serialized sessions in existing workflows<commit_after>package OpenXPKI::Server::Session::DriverRole;
use Moose::Role;
use utf8;

# CPAN modules
use JSON;

# Project modules
use OpenXPKI::Exception;

=head1 NAME

OpenXPKI::Server::Session::DriverRole - Moose role that every session driver
implementation has to consume

=cut

################################################################################
# Required in session implementations that consume this role
#
requires 'save';              # argument: $session, should write the attributes to the storage
requires 'load';              # argument: $id, should load data from storage and return a HashRef
requires 'delete_all_before'; # argument: $epoch, should delete all sessions which were created before the given timestamp

################################################################################
# Methods
#
# Please note that some method names are intentionally chosen to contain action
# prefixes like "get_" to distinct them from the accessor methods of the session
# attributes (data).
#

=head1 REQUIRED METHODS

The following methods are implemented in driver classes that consume this
Moose role.

=head2 save

Writes the session data to the backend storage.

B<Parameters>

=over

=item * $session - a L<OpenXPKI::Server::Session::Data> object

=back

=head2 load

Loads session data from the backend storage.

Returns a L<OpenXPKI::Server::Session::Data> object or I<undef> if the requested
session was not found..

B<Parameters>

=over

=item * $id - ID of the session whose data is to be loaded

=back

=head2 delete_all_before

Deletes all sessions from the backend storage which were created before the
given timestamp.

B<Parameters>

=over

=item * $epoch - timestamp

=back

=head1 STATIC METHODS

=head2 new

Constructor that creates a new session with an empty data object.

=cut

=head1 METHODS

=head2 freeze

Serializes the given HashRef into a string. The first characters of the
string until the first colon indicate the type of serialization (encoder).

Returns a string with the serialized data.

=cut
sub freeze {
    my ($self, $data) = @_;
    return "JSON:".encode_json($data);
}

=head2 thaw

Deserializes the session attributes from a string and returns them as C<HashRef>.

The first characters of the string until the first colon must indicate the type
of serialization (encoder).

=cut
sub thaw {
    my ($self, $frozen) = @_;

    # backwards compatibility
    if ($frozen =~ /^HASH\n/ ) {
        use OpenXPKI::Serialization::Simple;
        return OpenXPKI::Serialization::Simple->new->deserialize($frozen);
    }

    OpenXPKI::Exception->throw(message => "Unknown format of serialized data")
        unless $frozen =~ /^JSON:/;
    $frozen =~ s/^JSON://;

    my $data = decode_json($frozen);
    return $data;
}

1;

{'fraction_non_alphanumeric': 0.13676309044306115, 'fraction_numerical': 0.004393994873672647, 'mean_word_length': 4.452095808383233, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2644734', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1487, 'n_words': 634}