Commit 6eaea508 authored by Xavier Guimard's avatar Xavier Guimard

Avoid double utf8 management (#827)

parent 1dc99ce8
......@@ -2,20 +2,20 @@ package Lemonldap::NG::Common::Apache::Session::Serialize::JSON;
use strict;
use vars qw($VERSION);
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
our $VERSION = '1.9.0';
sub serialize {
my $session = shift;
$session->{serialized} = encode_json( $session->{data} );
$session->{serialized} = to_json( $session->{data} );
}
sub unserialize {
my $session = shift;
my $data = decode_json( $session->{serialized} );
my $data = from_json( $session->{serialized} );
die "Session could not be unserialized" unless defined $data;
$session->{data} = $data;
}
......@@ -40,8 +40,8 @@ Lemonldap::NG::Common::Apache::Session::Serialize::JSON - Use JSON to zip up dat
=head1 DESCRIPTION
This module fulfills the serialization interface of Apache::Session.
It serializes the data in the session object by use of JSON C<encode_json>
and C<decode_json>. The serialized data is UTF-8 text.
It serializes the data in the session object by use of JSON C<to_json>
and C<from_json>. The serialized data is UTF-8 text.
=head1 SEE ALSO
......
......@@ -2,7 +2,7 @@ package Lemonldap::NG::Common::Conf::CDBI;
use strict;
use utf8;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use Lemonldap::NG::Common::Conf::_DBI;
our $VERSION = '1.4.0';
......@@ -14,7 +14,7 @@ sub store {
my $req;
my $lastCfg = $self->lastCfg;
$fields = encode_json($fields);
$fields = to_json($fields);
if ( $lastCfg == $cfgNum ) {
$req = $self->_dbh->prepare(
......@@ -47,7 +47,7 @@ sub load {
}
my $r;
if ( $row->[0] =~ /^\s*\{/s ) {
eval { $r = decode_json( $row->[0] ); };
eval { $r = from_json( $row->[0] ); };
}
else { # Old format
require Storable;
......
package Lemonldap::NG::Common::Conf::File;
use strict;
use utf8;
use Lemonldap::NG::Common::Conf::Constants; #inherits
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use Encode;
our $VERSION = '1.4.0';
......@@ -23,7 +22,7 @@ sub prereq {
my $self = shift;
unless ( $self->{dirName} ) {
$Lemonldap::NG::Common::Conf::msg .=
'"dirName" is required in "File" configuration type ! \n';
"'dirName' is required in 'File' configuration type ! \n";
return 0;
}
unless ( -d $self->{dirName} ) {
......@@ -89,7 +88,8 @@ sub store {
return UNKNOWN_ERROR;
}
binmode(FILE);
print FILE JSON->new->canonical(1)->encode($fields);
my $f = to_json($fields);
print FILE $f;
close FILE;
umask($mask);
return $fields->{cfgNum};
......@@ -107,8 +107,7 @@ sub load {
}
binmode FILE;
$f = join( '', <FILE> );
$f = encode( 'UTF-8', $f );
eval { $ret = decode_json($f) };
eval { $ret = from_json($f) };
if ($@) {
print STDERR "$@\n";
$Lemonldap::NG::Common::Conf::msg .=
......
......@@ -3,7 +3,7 @@ package Lemonldap::NG::Common::Conf::Serializer;
use strict;
use utf8;
use Encode;
use JSON::MaybeXS;
use JSON::MaybeXS qw(JSON to_json from_json);
use Lemonldap::NG::Common::Conf::Constants;
our $VERSION = '1.9.0';
......@@ -96,7 +96,7 @@ sub unserialize {
unless ( utf8::is_utf8($v) ) {
$v = encode( 'UTF-8', $v );
}
$conf->{$k} = ( $v =~ /./ ? eval { decode_json($v) } : {} );
$conf->{$k} = ( $v =~ /./ ? eval { from_json($v) } : {} );
if ($@) {
$Lemonldap::NG::Common::Conf::msg .=
"Unable to decode $k, probably old format. Trying... ($@)";
......
......@@ -2,7 +2,7 @@ package Lemonldap::NG::Common::PSGI;
use 5.10.0;
use Mouse;
use JSON::MaybeXS;
use JSON::MaybeXS qw(JSON to_json from_json);
use Lemonldap::NG::Common::PSGI::Constants;
use Lemonldap::NG::Common::PSGI::Request;
......@@ -164,9 +164,9 @@ sub sendHtml {
SCRIPT_NAME => $req->scriptname,
STATIC_PREFIX => $sp,
AVAILABLE_LANGUAGES => $self->languages,
LINKS => $self->links ? encode_json( $self->links ) : '""',
LINKS => $self->links ? to_json( $self->links ) : '""',
MENULINKS => $self->menuLinks
? encode_json( $self->menuLinks )
? to_json( $self->menuLinks )
: '""',
VERSION => $VERSION,
);
......
......@@ -2,7 +2,7 @@ package Lemonldap::NG::Common::PSGI::Request;
use strict;
use Mouse;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use URI::Escape;
our $VERSION = '1.9.0';
......@@ -113,6 +113,7 @@ has CONTENT_LENGTH => (
$self->_psgiInput->read( $self->{body},
$self->{CONTENT_LENGTH}, 0 );
}
utf8::upgrade($self->{body});
}
}
);
......@@ -130,7 +131,7 @@ sub jsonBodyToObj {
return undef;
}
return $self->body if ( ref( $self->body ) );
my $j = eval { decode_json( $self->body ) };
my $j = eval { from_json( $self->body ) };
if ( $@ or $! ) {
$self->error("$@$!");
return undef;
......
......@@ -55,10 +55,11 @@ for ( my $i = 0 ; $i < @test ; $i++ ) {
my $cfg;
ok( $cfg = $h->load(1), "Test $i can be read" )
or print STDERR $Lemonldap::NG::Common::Conf::msg;
ok( $cfg->{test} eq $test[$i]->{test}, "Test $i is restored" );
ok( $cfg->{test} eq $test[$i]->{test}, "Test $i is restored" )
or print STDERR "Expect $cfg->{test} eq $test[$i]->{test}\n";
$count += 2;
}
unlink 't/lmConf-1.js';
#unlink 't/lmConf-1.js';
done_testing($count);
package Lemonldap::NG::Manager::Cli::Lib;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use Mouse;
use utf8;
use Lemonldap::NG::Manager;
......@@ -137,7 +137,7 @@ sub jsonResponse {
or die "Manager lib has refused my get, aborting";
die "Manager lib does not return a 200 code, aborting"
unless ( $res->[0] == 200 );
my $href = decode_json( $res->[2]->[0] ) or die 'Response is not JSON';
my $href = from_json( $res->[2]->[0] ) or die 'Response is not JSON';
return $href;
}
......@@ -147,7 +147,7 @@ sub jsonPostResponse {
or die "Manager lib has refused my post, aborting";
die "Manager lib does not return a 200 code, aborting"
unless ( $res->[0] == 200 );
my $href = decode_json( $res->[2]->[0] ) or die 'Response is not JSON';
my $href = from_json( $res->[2]->[0] ) or die 'Response is not JSON';
return $href;
}
......@@ -157,7 +157,7 @@ sub jsonPutResponse {
or die "Manager lib has refused my put, aborting";
die "Manager lib does not return a 200 code, aborting"
unless ( $res->[0] == 200 );
my $href = decode_json( $res->[2]->[0] ) or die 'Response is not JSON';
my $href = from_json( $res->[2]->[0] ) or die 'Response is not JSON';
return $href;
}
......
......@@ -14,7 +14,6 @@ use Lemonldap::NG::Common::PSGI::Constants;
use Lemonldap::NG::Manager::Constants;
use Crypt::OpenSSL::RSA;
use Convert::PEM;
use JSON::MaybeXS;
use feature 'state';
......
......@@ -4,7 +4,7 @@
# Check also metadatas request (root of a conf)
use Test::More;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use strict;
require 't/test-lib.pm';
......@@ -47,7 +47,7 @@ foreach my $query (@bad) {
my $href;
#print STDERR Dumper($res->[2]);use Data::Dumper;
ok( $href = decode_json( $res->[2]->[0] ), 'Response is JSON' );
ok( $href = from_json( $res->[2]->[0] ), 'Response is JSON' );
ok( $href->{error}, "Receive an explanation message ($href->{error})" );
count(3);
}
......@@ -61,7 +61,7 @@ while (<F>) {
}
close F;
ok( $hstruct = decode_json($hstruct), 'struct.json is JSON' );
ok( $hstruct = from_json($hstruct), 'struct.json is JSON' );
ok( ref $hstruct eq 'ARRAY', 'struct.json is an array' );
count(2);
......
......@@ -4,7 +4,7 @@
use Test::More;
use strict;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use IO::String;
require 't/test-lib.pm';
......@@ -17,7 +17,7 @@ ok(
);
ok( $res->[0] == 200, "Result code is 200" );
my $key;
ok( $key = decode_json( $res->[2]->[0] ), 'Response is JSON' );
ok( $key = from_json( $res->[2]->[0] ), 'Response is JSON' );
count(3);
ok(
......@@ -28,7 +28,7 @@ ok(
"Request succeed"
);
ok( $res->[0] == 200, "Result code is 200" );
ok( $key = decode_json( $res->[2]->[0] ), 'Response is JSON' );
ok( $key = from_json( $res->[2]->[0] ), 'Response is JSON' );
count(3);
#print STDERR Dumper($key);use Data::Dumper;
......
......@@ -3,14 +3,16 @@
# Test if an UTF-8 char is well returned
use Test::More;
use JSON::MaybeXS;
use strict;
use utf8;
my $str = "The LemonLDAP::NG team \x{c2}\x{a9}";
require 't/test-lib.pm';
my $href = &client->jsonResponse('/confs/1/cfgAuthor');
ok( $href->{value} eq "The LemonLDAP::NG team ©", 'Value is well encoded' );
#binmode STDERR;
ok( $href->{value} eq $str, 'Value is well encoded' ) or print STDERR "Expect '$href->{value}' eq '$str'";
count(1);
done_testing( count() );
......@@ -4,7 +4,7 @@
use Test::More;
use strict;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use Data::Dumper;
require 't/test-lib.pm';
......@@ -33,7 +33,7 @@ while ( my $body = &body() ) {
);
ok( $res->[0] == 200, "$desc: result code is 200" );
ok(
$resBody = decode_json( $res->[2]->[0] ),
$resBody = from_json( $res->[2]->[0] ),
"$desc: result body contains JSON text"
);
......
......@@ -5,7 +5,7 @@
use Test::More;
use strict;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use Data::Dumper;
require 't/test-lib.pm';
......@@ -23,7 +23,7 @@ my ( $res, $resBody );
ok( $res = &client->_post( '/confs/', 'cfgNum=1', &body, 'application/json' ),
"Request succeed" );
ok( $res->[0] == 200, "Result code is 200" );
ok( $resBody = decode_json( $res->[2]->[0] ),
ok( $resBody = from_json( $res->[2]->[0] ),
"Result body contains JSON text" );
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" )
or print STDERR Dumper($resBody);
......
......@@ -3,7 +3,7 @@
# Test sessions explorer API
use Test::More;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use strict;
use Lemonldap::NG::Common::Session;
......@@ -147,7 +147,7 @@ foreach (@ids) {
my $res;
ok( $res = &client->_del("/sessions/global/$_"), "Delete $_" );
ok( $res->[0] == 200, 'Result code is 200' );
ok( decode_json( $res->[2]->[0] )->{result} == 1,
ok( from_json( $res->[2]->[0] )->{result} == 1,
'Body is JSON and result==1' );
count(3);
}
......
......@@ -3,7 +3,7 @@
# Verify that languages translation cover all `trspan`
use Test::More;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use strict;
my $langDir = 'site/static/languages';
......@@ -23,7 +23,7 @@ ok( $en = join( '', <F> ), 'en.json is not empty' );
close F;
ok( @langs, 'Found other languages' );
my $keys;
ok( $keys = decode_json($en), 'en.json contains JSON' );
ok( $keys = from_json($en), 'en.json contains JSON' );
$en = undef;
$count += 5;
......@@ -32,7 +32,7 @@ foreach my $lang (@langs) {
my $j;
ok( $j = join( '', <F> ), "$lang is not empty" );
my $l;
ok( $l = decode_json($j), "$lang contains JSON" . ( $@ ? " ($@)" : "" ) );
ok( $l = from_json($j), "$lang contains JSON" . ( $@ ? " ($@)" : "" ) );
$lang =~ s/\.json$//;
my @l1 = sort keys %$keys;
my @l2 = sort keys %$l;
......
......@@ -3,7 +3,7 @@
# Simple OpenID Connect client
use strict;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use LWP::UserAgent;
use MIME::Base64
qw/encode_base64url encode_base64 decode_base64url decode_base64/;
......@@ -65,9 +65,9 @@ if ( $cgi->param("test") eq "request" ) {
aud => [$portal_url]
};
my $request_payload =
encode_base64( encode_json($request_paylod_hash), "" );
encode_base64( to_json($request_paylod_hash), "" );
my $request_header_hash = { typ => "JWT", alg => "HS256" };
my $request_header = encode_base64( encode_json($request_header_hash), "" );
my $request_header = encode_base64( to_json($request_header_hash), "" );
my $request_digest =
hmac_sha256_base64( $request_header . "." . $request_payload,
......@@ -283,7 +283,7 @@ if ($callback) {
my $content = $response->decoded_content;
my $json;
eval { $json = decode_json $content; };
eval { $json = from_json $content; };
if ($@) {
print "<div class=\"alert alert-danger\">";
......@@ -338,7 +338,7 @@ if ($callback) {
# TODO check signature
my $id_token_payload_hash = decode_json( decode_base64($id_token_payload) );
my $id_token_payload_hash = from_json( decode_base64($id_token_payload) );
print "<div class=\"panel panel-info\">\n";
print "<div class=\"panel-heading\">\n";
......@@ -359,12 +359,12 @@ if ($callback) {
my $content_type = $ui_response->header('Content-Type');
if ( $content_type =~ /json/ ) {
eval { $ui_json = decode_json($ui_content); };
eval { $ui_json = from_json($ui_content); };
}
elsif ( $content_type =~ /jwt/ ) {
my ( $ui_header, $ui_payload, $ui_signature ) =
split( /\./, $ui_content );
eval { $ui_json = decode_json( decode_base64($ui_payload) ); };
eval { $ui_json = from_json( decode_base64($ui_payload) ); };
}
print "<div class=\"panel panel-info\">\n";
......@@ -436,7 +436,7 @@ elsif ( $cgi->param("test") eq "configuration" ) {
my $content = $config_response->decoded_content;
my $json;
eval { $json = decode_json $content; };
eval { $json = from_json $content; };
if ($@) {
print "<div class=\"alert alert-danger\">";
......@@ -496,7 +496,7 @@ elsif ( $cgi->param("test") eq "configuration" ) {
my $jwks_content = $jwks_response->decoded_content;
my $jwks_json;
eval { $jwks_json = decode_json $jwks_content; };
eval { $jwks_json = from_json $jwks_content; };
if ($@) {
print "<div class=\"alert alert-danger\">";
......@@ -581,7 +581,7 @@ elsif ( $cgi->param("test") eq "registration" ) {
print "</div>\n";
# POST client metadata
my $fake_metadata_json = encode_json($fake_metadata);
my $fake_metadata_json = to_json($fake_metadata);
my $register_response = $ua->post(
$registration_uri,
"Content-Type" => 'application/json',
......@@ -591,7 +591,7 @@ elsif ( $cgi->param("test") eq "registration" ) {
my $register_content = $register_response->decoded_content;
my $register_json;
eval { $register_json = decode_json $register_content; };
eval { $register_json = from_json $register_content; };
if ($@) {
print "<div class=\"alert alert-danger\">";
......@@ -664,9 +664,9 @@ else {
aud => [$portal_url]
};
my $request_payload =
encode_base64( encode_json($request_paylod_hash), "" );
encode_base64( to_json($request_paylod_hash), "" );
my $request_header_hash = { typ => "JWT", alg => "HS256" };
my $request_header = encode_base64( encode_json($request_header_hash), "" );
my $request_header = encode_base64( to_json($request_header_hash), "" );
my $request_digest =
hmac_sha256_base64( $request_header . "." . $request_payload,
......
#!/usr/bin/perl
use Lemonldap::NG::Portal::SharedConf;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use strict;
my $portal = Lemonldap::NG::Portal::SharedConf->new();
......
......@@ -9,7 +9,7 @@ use strict;
use Lemonldap::NG::Portal::Simple;
use Lemonldap::NG::Portal::_Browser;
use HTTP::Request;
use JSON::MaybeXS;
use JSON::MaybeXS qw(JSON to_json from_json);
our @ISA = (qw(Lemonldap::NG::Portal::_Browser));
our $VERSION = '1.3.0';
......
......@@ -6,7 +6,7 @@
package Lemonldap::NG::Portal::_OpenIDConnect;
use strict;
use JSON::MaybeXS;
use JSON::MaybeXS qw(to_json from_json);
use MIME::Base64
qw/encode_base64url encode_base64 decode_base64url decode_base64/;
use URI::Escape;
......@@ -643,7 +643,7 @@ sub decodeJSON {
my ( $self, $json ) = @_;
my $json_hash;
eval { $json_hash = decode_json $json; };
eval { $json_hash = from_json $json; };
if ($@) {
$json_hash->{error} = "parse_error";
......@@ -1228,13 +1228,13 @@ sub createJWT {
my ( $self, $payload, $alg, $rp ) = @_;
# Payload encoding
my $jwt_payload = encode_base64( encode_json($payload), "" );
my $jwt_payload = encode_base64( to_json($payload), "" );
# JWT header
my $jwt_header_hash = { typ => "JWT", alg => $alg };
$jwt_header_hash->{kid} = $self->{oidcServiceKeyIdSig}
if $self->{oidcServiceKeyIdSig};
my $jwt_header = encode_base64( encode_json($jwt_header_hash), "" );
my $jwt_header = encode_base64( to_json($jwt_header_hash), "" );
if ( $alg eq "none" ) {
......@@ -1357,7 +1357,7 @@ sub getJWTJSONData {
my ( $self, $jwt ) = @_;
my $jwt_parts = $self->extractJWT($jwt);
return decode_json( decode_base64url( $jwt_parts->[1] ) );
return from_json( decode_base64url( $jwt_parts->[1] ) );
}
## @method HashRef key2jwks(String key)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment