OIDC/Controller/OpenIDConnect.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 83 CC 4 SCAR 13.1
CC
4
Cov
0%
make_app_url
6.0
login
6.0
session
6.0
CRAP 3.7
1 package Mojolicious::Plugin::Authentication::OIDC::Controller::OpenIDConnect 0.06;
2 use v5.26;
3  
4 # ABSTRACT: OpenID controller endpoints implementation
5  
6 =encoding UTF-8
7  
8 =head1 NAME
9  
10 Mojolicious::Plugin::Authentication::OIDC::Controller::OpenIDConnect - controller
11 endpoints for OpenID Connect authentication
12  
13 =head1 DESCRIPTION
14  
15 This is a simple mojolicious controller that implements the endpoints necessary
16 for OpenID Connect authentication. These endpoints must be registered as routes
17 to function, which can be done implicity by
18 L<Mojolicious::Plugin::Authentication::OIDC/make_routes>
19  
20 =head1 METHODS
21  
22 L<Mojolicious::Plugin::Authentication::OIDC::Controller::OpenIDConnect> inherits
23 all methods from L<Mojolicious::Controller> and implements the following new ones
24  
25 =head2 redirect
26  
27 Returns an HTTP 302, redirecting the client to the auhorization server's
28 C<authorization_endpoint> with the proper parameters populated for an openid
29 code response, and then to redirect to L</login>
30  
31 =head2 login
32  
33 Contacts the authorization server to receive an access token for the OpenID
34 authenticated user. Calls the
35 L<Mojolicious::Plugin::Authentication::OIDC/on_success> or
36 L<Mojolicious::Plugin::Authentication::OIDC/on_error> handles, as apprporiate.
37  
38 =cut
39  
40 use Mojo::Base 'Mojolicious::Controller';
41  
42 use Mojo::Parameters;
43 use Mojo::UserAgent;
44 use Syntax::Keyword::Try;
45  
46 use experimental qw(signatures);
47  
48 my sub make_app_url($self, $path = '/') {
49 my $oidc_params = $self->app->renderer->get_helper('__oidc_params')->($self);
50 my $url;
51 if ($oidc_params->{base_url}) {
52 $url = Mojo::URL->new($oidc_params->{base_url});
53 } else {
54 $url = $self->tx->req->url->to_abs->clone;
55 $url->fragment(undef);
56 $url->query(Mojo::Parameters->new);
57 }
58 $url->path($path);
59 }
60  
61 sub redirect($self) {
62 my $oidc_params = $self->app->renderer->get_helper('__oidc_params')->($self);
63 my $idp_url = Mojo::URL->new($oidc_params->{auth_endpoint});
64  
65 my $success_url = $self->param('ref');
66 $self->cookie(oidc_login_redirect => $success_url);
67  
68 $idp_url->query(
69 {
70 client_id => $oidc_params->{client_id},
71 scope => $oidc_params->{scope},
72 response_type => $oidc_params->{response_type},
73 redirect_uri => make_app_url($self, $oidc_params->{login_path}),
74 }
75 );
76 $self->redirect_to($idp_url);
77 }
78  
79 sub login($self) {
80 my $oidc_params = $self->app->renderer->get_helper('__oidc_params')->($self);
81 my $code = $self->param('code');
82 my $ua = Mojo::UserAgent->new();
83 my $url =
84 Mojo::URL->new($oidc_params->{token_endpoint})->userinfo(join(':', $oidc_params->{client_id}, $oidc_params->{client_secret}));
85  
86 my $success_url = $self->cookie('oidc_login_redirect');
87 $self->cookie(oidc_login_redirect => '', {expires => 1});
88  
89 my $resp = $ua->post(
90 $url => form => {
91 grant_type => $oidc_params->{grant_type},
92 code => $code,
93 redirect_uri => make_app_url($self, $oidc_params->{login_path}),
94 }
95 );
96  
97 if ($resp->res->json->{error}) {
98 return $oidc_params->{on_error}->($self, $resp->res->json);
99 } else {
100 try {
101 my $token = $resp->res->json->{access_token};
102 # Decode the token; if it fails, because the key is wrong, or the token
103 # is invalid or has been re-encrypted, then we throw it away and call
104 # error handler
105 $self->app->renderer->get_helper('__oidc_token')->($self, $token);
106 return $oidc_params->{on_success}->($self, $token, $success_url);
107 } catch ($e) {
108 $self->log->error($e);
109 $oidc_params->{on_error}->($self, $resp->res->json);
110 }
111 }
112 }
113  
114 sub session($self) {
115 my $return_token = $self->param('t');
116  
117 my ($token, $data) = ($self->app->renderer->get_helper('__oidc_token')->($self, undef, 0), {});
118 try {$data = $self->app->renderer->get_helper('__oidc_token')->($self)} catch ($e) {
119 }
120 $data->{token} = $token if ($return_token);
121 $self->render(json => $data);
122 }
123  
124 sub logout($self) {
125 my $oidc_params = $self->app->renderer->get_helper('__oidc_params')->($self);
126 my $idp_url = Mojo::URL->new($oidc_params->{logout_endpoint});
127  
128 my $success_url = $self->param('ref');
129  
130 $idp_url->query(
131 {
132 client_id => $oidc_params->{client_id},
133 post_logout_redirect_uri => $success_url,
134 }
135 );
136 $self->redirect_to($idp_url);
137 }
138  
139 =head1 AUTHOR
140  
141 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
142  
143 =head1 LICENSE
144  
145 Copyright (c) 2024 Mark Tyrrell
146  
147 Permission is hereby granted, free of charge, to any person obtaining a copy
148 of this software and associated documentation files (the "Software"), to deal
149 in the Software without restriction, including without limitation the rights
150 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
151 copies of the Software, and to permit persons to whom the Software is
152 furnished to do so, subject to the following conditions:
153  
154 The above copyright notice and this permission notice shall be included in all
155 copies or substantial portions of the Software.
156  
157 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
159 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
161 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
162 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
163 SOFTWARE.
164  
165 =cut
166  
167 1;
168  
169 __END__