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             package App::Dochazka::REST::Util;
34              
35 41     41   884 use 5.012;
  41         187  
36 41     41   300 use strict;
  41         100  
  41         797  
37 41     41   218 use warnings;
  41         132  
  41         1062  
38              
39 41     41   238 use App::CELL qw( $log );
  41         87  
  41         3136  
40 41     41   20761 use Authen::Passphrase::SaltedDigest;
  41         861693  
  41         2256  
41 41     41   25162 use Pod::Simple::HTML;
  41         1791312  
  41         2178  
42              
43              
44              
45             =head1 NAME
46              
47             App::Dochazka::REST::Util - miscellaneous utilities
48              
49              
50              
51              
52             =head1 SYNOPSIS
53              
54             Miscellaneous utilities
55              
56             use App::Dochazka::REST::Util;
57              
58             ...
59              
60              
61              
62              
63             =head1 EXPORTS
64              
65             This module provides the following exports:
66              
67             =over
68              
69             =item L<hash_the_password> (function)
70              
71             =item L<pod_to_html> (function)
72              
73             =item L<pre_update_comparison> (function)
74              
75             =back
76              
77             =cut
78              
79 41     41   418 use Exporter qw( import );
  41         100  
  41         16934  
80             our @EXPORT_OK = qw(
81             hash_the_password
82             pod_to_html
83             pre_update_comparison
84             );
85              
86              
87              
88              
89             =head1 FUNCTIONS
90              
91              
92             =head2 hash_the_password
93              
94             Takes a request entity (hashref) - looks for a 'password' property. If it
95             is present, adds a random salt to the request entity and hashes the
96             password with it. If there is no password property, the function does
97             nothing.
98              
99             =cut
100              
101             sub hash_the_password {
102 0     0 1   my $entity = shift;
103 0 0         if ( $entity->{'password'} ) {
104             my $ppr = Authen::Passphrase::SaltedDigest->new(
105             algorithm => "SHA-512", salt_random => 20,
106 0           passphrase => $entity->{'password'}
107             );
108 0           delete $entity->{'password'};
109 0           $entity->{'passhash'} = $ppr->hash_hex;
110 0           $entity->{'salt'} = $ppr->salt_hex;
111             }
112             }
113              
114              
115             =head2 pod_to_html
116              
117             Each L<App::Dochazka::REST> resource definition includes a 'documentation'
118             property containing a POD string. Our 'docu/html' resource converts this
119             POD string into HTML with a little help from this routine.
120              
121             =cut
122              
123             sub pod_to_html {
124 0     0 1   my ( $pod_str ) = @_;
125 0           $log->debug( "pod_to_html before: $pod_str" );
126 0           my $p = Pod::Simple::HTML->new;
127 0           $p->output_string(\my $html_str);
128 0           $p->parse_string_document($pod_str);
129              
130             # now $html contains a full-blown HTML file, of which only one part is of
131             # interest to us. That part starts with the line <!-- start doc -->
132             # and ends with <!-- end doc -->
133              
134 0           $html_str =~ s/.*<!-- start doc -->//s;
135 0           $html_str =~ s/<!-- end doc -->.*//s;
136              
137 0           $log->debug( "pod_to_html after: $html_str" );
138 0           return $html_str;
139             }
140              
141              
142             =head2 pre_update_comparison
143              
144             Given an original object and a hashref of possible changed properties,
145             compare the properties in the hashref with the corresponding properties
146             in the original object. If any properties really are changed, update
147             the object. Return the number of properties so changed.
148              
149             =cut
150              
151             sub pre_update_comparison {
152 0     0 1   my ( $obj, $over ) = @_;
153 0           my $c = 0;
154 0           foreach my $prop (keys %$over) {
155 0 0         if ( exists $obj->{$prop} ) {
156 0 0 0       next if not defined $obj->{$prop} and not defined $over->{$prop};
157 0           $log->debug( "pre_update_comparison: detected changed property $prop" );
158             # FIXME: how to test equality when we don't know the type?
159 0           $obj->{$prop} = $over->{$prop};
160 0           $c += 1;
161             }
162             }
163 0           return $c;
164             }
165              
166              
167             1;