View | Details | Raw Unified | Return to bug 31378
Collapse All | Expand All

(-)a/Koha/Auth/Provider.pm (+122 lines)
Lines 21-27 use Modern::Perl; Link Here
21
21
22
use base qw(Koha::Object);
22
use base qw(Koha::Object);
23
23
24
use JSON qw( decode_json encode_json );
25
use Try::Tiny;
26
24
use Koha::Auth::Provider::Domains;
27
use Koha::Auth::Provider::Domains;
28
use Koha::Exceptions;
29
use Koha::Exceptions::Object;
25
30
26
=head1 NAME
31
=head1 NAME
27
32
Lines 45-50 sub domains { Link Here
45
    return Koha::Auth::Provider::Domains->_new_from_dbic( scalar $self->_result->domains );
50
    return Koha::Auth::Provider::Domains->_new_from_dbic( scalar $self->_result->domains );
46
}
51
}
47
52
53
=head3 get_config
54
55
    my $config = $provider->get_config;
56
57
Returns a I<hashref> containing the configuration parameters for the provider.
58
59
=cut
60
61
sub get_config {
62
    my ($self) = @_;
63
64
    return try {
65
        return decode_json( $self->config );
66
    }
67
    catch {
68
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
69
    };
70
}
71
72
=head3 set_config
73
74
    # OAuth
75
    $provider->set_config(
76
        {
77
            key           => 'APP_ID',
78
            secret        => 'SECRET_KEY',
79
            authorize_url => 'https://provider.example.com/auth',
80
            token_url     => 'https://provider.example.com/token',
81
        }
82
    );
83
84
    # OIDC
85
    $provider->set_config(
86
        {
87
            key           => 'APP_ID',
88
            secret        => 'SECRET_KEY',
89
            well_known_url => 'https://login.microsoftonline.com/tenant-id/v2.0/.well-known/openid-configuration',
90
        }
91
    );
92
93
This method stores the passed config in JSON format.
94
95
=cut
96
97
sub set_config {
98
    my ($self, $config) = @_;
99
100
    my @mandatory;
101
102
    if ( $self->protocol eq 'OIDC' ) {
103
        @mandatory = qw(key secret well_known_url);
104
    }
105
    elsif ( $self->protocol eq 'OAuth' ) {
106
        @mandatory = qw(key secret authorize_url token_url);
107
    }
108
    else {
109
        Koha::Exception->throw( 'Unsupported protocol ' . $self->protocol );
110
    }
111
112
    for my $param (@mandatory) {
113
        unless ( defined( $config->{$param} ) ) {
114
            Koha::Exceptions::MissingParameter->throw(
115
                error => "The $param parameter is mandatory" );
116
        }
117
    }
118
119
    try {
120
        my $encoded_config = encode_json($config);
121
        $self->config($encoded_config)->store;
122
    } catch {
123
        Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
124
    };
125
126
    return $self;
127
}
128
129
=head3 get_mapping
130
131
    my $mapping = $provider->get_mapping;
132
133
Returns a I<hashref> containing the attribute mapping for the provider.
134
135
=cut
136
137
sub get_mapping {
138
    my ($self) = @_;
139
140
    return try {
141
        return decode_json( $self->mapping );
142
    }
143
    catch {
144
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
145
    };
146
}
147
148
=head3 set_mapping
149
150
    $provider->mapping( $mapping );
151
152
This method stores the passed mappings in JSON format.
153
154
=cut
155
156
sub set_mapping {
157
    my ($self, $mapping) = @_;
158
159
    try {
160
        my $encoded_mapping = encode_json( $mapping );
161
        $self->mapping( $encoded_mapping )->store;
162
    }
163
    catch {
164
        Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
165
    };
166
167
    return $self;
168
}
169
48
=head2 Internal methods
170
=head2 Internal methods
49
171
50
=head3 _type
172
=head3 _type
(-)a/t/db_dependent/Koha/Auth/Provider.t (-7 / +135 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 5;
23
24
use Test::MockModule;
25
use Test::Exception;
26
27
use JSON qw(encode_json);
23
28
24
use Koha::Auth::Providers;
29
use Koha::Auth::Providers;
25
30
Lines 35-50 subtest 'domains() tests' => sub { Link Here
35
40
36
    $schema->storage->txn_begin;
41
    $schema->storage->txn_begin;
37
42
38
    my $provider = $builder->build_object({ class => 'Koha::Auth::Providers' });
43
    my $provider = $builder->build_object( { class => 'Koha::Auth::Providers' } );
39
    my $domains  = $provider->domains;
44
    my $domains  = $provider->domains;
40
45
41
    is( ref($domains), 'Koha::Auth::Provider::Domains', 'Type is correct' );
46
    is( ref($domains),   'Koha::Auth::Provider::Domains', 'Type is correct' );
42
    is( $domains->count, 0, 'No domains defined' );
47
    is( $domains->count, 0,                               'No domains defined' );
43
48
44
    $builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } });
49
    $builder->build_object( { class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } } );
45
    $builder->build_object({ class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } });
50
    $builder->build_object( { class => 'Koha::Auth::Provider::Domains', value => { auth_provider_id => $provider->id } } );
46
51
47
    is( $provider->domains->count, 2, 'The provider has 2 domains defined' );
52
    is( $provider->domains->count, 2, 'The provider has 2 domains defined' );
48
53
49
    $schema->storage->txn_rollback;
54
    $schema->storage->txn_rollback;
50
};
55
};
51
- 
56
57
subtest 'get_config() tests' => sub {
58
59
    plan tests => 2;
60
61
    $schema->storage->txn_begin;
62
63
    my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { config => '{' } } );
64
65
    throws_ok { $provider->get_config() }
66
    'Koha::Exceptions::Object::BadValue', 'Expected exception thrown on bad JSON';
67
68
    my $config = { some => 'value', and => 'another' };
69
    $provider->config( encode_json($config) )->store;
70
71
    is_deeply( $provider->get_config, $config, 'Config correctly retrieved' );
72
73
    $schema->storage->txn_rollback;
74
};
75
76
subtest 'set_config() tests' => sub {
77
78
    plan tests => 3;
79
80
    $schema->storage->txn_begin;
81
82
    subtest 'OIDC protocol tests' => sub {
83
84
        plan tests => 4;
85
86
        my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'OIDC' } } );
87
88
        my $config = {
89
            key    => 'key',
90
            secret => 'secret',
91
        };
92
93
        throws_ok { $provider->set_config($config) }
94
        'Koha::Exceptions::MissingParameter', 'Exception thrown on missing parameter';
95
96
        is( $@->error, 'The well_known_url parameter is mandatory', 'Message is correct' );
97
98
        $config->{well_known_url} = 'https://koha-community.org/auth';
99
100
        my $return = $provider->set_config($config);
101
        is( ref($return), 'Koha::Auth::Provider', 'Return type is correct' );
102
103
        is_deeply( $provider->get_config, $config, 'Configuration stored correctly' );
104
    };
105
106
    subtest 'OAuth protocol tests' => sub {
107
108
        plan tests => 4;
109
110
        my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'OAuth' } } );
111
112
        my $config = {
113
            key       => 'key',
114
            secret    => 'secret',
115
            token_url => 'https://koha-community.org/auth/token',
116
        };
117
118
        throws_ok { $provider->set_config($config) }
119
        'Koha::Exceptions::MissingParameter', 'Exception thrown on missing parameter';
120
121
        is( $@->error, 'The authorize_url parameter is mandatory', 'Message is correct' );
122
123
        $config->{authorize_url} = 'https://koha-community.org/auth/authorize';
124
125
        my $return = $provider->set_config($config);
126
        is( ref($return), 'Koha::Auth::Provider', 'Return type is correct' );
127
128
        is_deeply( $provider->get_config, $config, 'Configuration stored correctly' );
129
    };
130
131
    subtest 'Unsupported protocol tests' => sub {
132
133
        plan tests => 2;
134
135
        my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { protocol => 'CAS' } } );
136
137
        throws_ok { $provider->set_config() }
138
        'Koha::Exception', 'Exception thrown on unsupported protocol';
139
140
        like( "$@", qr/Unsupported protocol CAS/, 'Message is correct' );
141
    };
142
143
    $schema->storage->txn_rollback;
144
};
145
146
subtest 'get_mapping() tests' => sub {
147
148
    plan tests => 2;
149
150
    $schema->storage->txn_begin;
151
152
    my $provider = $builder->build_object( { class => 'Koha::Auth::Providers', value => { config => '{' } } );
153
154
    throws_ok { $provider->get_mapping() }
155
    'Koha::Exceptions::Object::BadValue', 'Expected exception thrown on bad JSON';
156
157
    my $mapping = { some => 'value', and => 'another' };
158
    $provider->mapping( encode_json($mapping) )->store;
159
160
    is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' );
161
162
    $schema->storage->txn_rollback;
163
};
164
165
subtest 'set_mapping() tests' => sub {
166
167
    plan tests => 1;
168
169
    $schema->storage->txn_begin;
170
171
    my $provider = $builder->build_object( { class => 'Koha::Auth::Providers' } );
172
173
    my $mapping = { some => 'value', and => 'another' };
174
    $provider->set_mapping($mapping)->store;
175
176
    is_deeply( $provider->get_mapping, $mapping, 'Mapping correctly retrieved' );
177
178
    $schema->storage->txn_rollback;
179
};

Return to bug 31378