File Coverage

blib/lib/App/Dochazka/REST/Util.pm
Criterion Covered Total %
statement 20 44 45.4
branch 0 6 0.0
condition 0 3 0.0
subroutine 7 10 70.0
pod 3 3 100.0
total 30 66 45.4


line stmt bran cond sub pod time code
1             # *************************************************************************
2             # Copyright (c) 2014-2017, SUSE LLC
3             #
4             # All rights reserved.
5             #
6             # Redistribution and use in source and binary forms, with or without
7             # modification, are permitted provided that the following conditions are met:
8             #
9             # 1. Redistributions of source code must retain the above copyright notice,
10             # this list of conditions and the following disclaimer.
11             #
12             # 2. Redistributions in binary form must reproduce the above copyright
13             # notice, this list of conditions and the following disclaimer in the
14             # documentation and/or other materials provided with the distribution.
15             #
16             # 3. Neither the name of SUSE LLC nor the names of its contributors may be
17             # used to endorse or promote products derived from this software without
18             # specific prior written permission.
19             #
20             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30             # POSSIBILITY OF SUCH DAMAGE.
31             # *************************************************************************
32              
33              
34             use 5.012;
35 41     41   785 use strict;
  41         164  
36 41     41   233 use warnings;
  41         90  
  41         678  
37 41     41   187  
  41         90  
  41         902  
38             use App::CELL qw( $log );
39 41     41   201 use Authen::Passphrase::SaltedDigest;
  41         89  
  41         2856  
40 41     41   17453 use Pod::Simple::HTML;
  41         735248  
  41         2273  
41 41     41   21473  
  41         1517100  
  41         1857  
42              
43              
44             =head1 NAME
45              
46             App::Dochazka::REST::Util - miscellaneous utilities
47              
48              
49              
50              
51             =head1 SYNOPSIS
52              
53             Miscellaneous utilities
54              
55             use App::Dochazka::REST::Util;
56              
57             ...
58              
59              
60              
61              
62             =head1 EXPORTS
63              
64             This module provides the following exports:
65              
66             =over
67              
68             =item L<hash_the_password> (function)
69              
70             =item L<pod_to_html> (function)
71              
72             =item L<pre_update_comparison> (function)
73              
74             =back
75              
76             =cut
77              
78             use Exporter qw( import );
79 41     41   363 our @EXPORT_OK = qw(
  41         86  
  41         14588  
80             hash_the_password
81             pod_to_html
82             pre_update_comparison
83             );
84              
85              
86              
87              
88             =head1 FUNCTIONS
89              
90              
91             =head2 hash_the_password
92              
93             Takes a request entity (hashref) - looks for a 'password' property. If it
94             is present, adds a random salt to the request entity and hashes the
95             password with it. If there is no password property, the function does
96             nothing.
97              
98             =cut
99              
100             my $entity = shift;
101             if ( $entity->{'password'} ) {
102 0     0 1   my $ppr = Authen::Passphrase::SaltedDigest->new(
103 0 0         algorithm => "SHA-512", salt_random => 20,
104             passphrase => $entity->{'password'}
105             );
106 0           delete $entity->{'password'};
107             $entity->{'passhash'} = $ppr->hash_hex;
108 0           $entity->{'salt'} = $ppr->salt_hex;
109 0           }
110 0           }
111              
112              
113             =head2 pod_to_html
114              
115             Each L<App::Dochazka::REST> resource definition includes a 'documentation'
116             property containing a POD string. Our 'docu/html' resource converts this
117             POD string into HTML with a little help from this routine.
118              
119             =cut
120              
121             my ( $pod_str ) = @_;
122             $log->debug( "pod_to_html before: $pod_str" );
123             my $p = Pod::Simple::HTML->new;
124 0     0 1   $p->output_string(\my $html_str);
125 0           $p->parse_string_document($pod_str);
126 0            
127 0           # now $html contains a full-blown HTML file, of which only one part is of
128 0           # interest to us. That part starts with the line <!-- start doc -->
129             # and ends with <!-- end doc -->
130              
131             $html_str =~ s/.*<!-- start doc -->//s;
132             $html_str =~ s/<!-- end doc -->.*//s;
133              
134 0           $log->debug( "pod_to_html after: $html_str" );
135 0           return $html_str;
136             }
137 0            
138 0            
139             =head2 pre_update_comparison
140              
141             Given an original object and a hashref of possible changed properties,
142             compare the properties in the hashref with the corresponding properties
143             in the original object. If any properties really are changed, update
144             the object. Return the number of properties so changed.
145              
146             =cut
147              
148             my ( $obj, $over ) = @_;
149             my $c = 0;
150             foreach my $prop (keys %$over) {
151             if ( exists $obj->{$prop} ) {
152 0     0 1   next if not defined $obj->{$prop} and not defined $over->{$prop};
153 0           $log->debug( "pre_update_comparison: detected changed property $prop" );
154 0           # FIXME: how to test equality when we don't know the type?
155 0 0         $obj->{$prop} = $over->{$prop};
156 0 0 0       $c += 1;
157 0           }
158             }
159 0           return $c;
160 0           }
161              
162              
163 0           1;