Catalyst-Plugin-OpenIDConnect

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/OpenIDConnect/Context.pm  view on Meta::CPAN

package Catalyst::Plugin::OpenIDConnect::Context;

use Moose;
use namespace::autoclean;
use Catalyst::Plugin::OpenIDConnect::Utils::Store;

# Per-application-class storage for extension-point callbacks.
# Keyed by the consuming application class name so that multiple Catalyst apps
# loaded in the same interpreter each hold their own handlers.
my %_scope_handler_by_class;
my %_claims_provider_by_class;

=head1 NAME

Catalyst::Plugin::OpenIDConnect::Context - OIDC provider context object

=head1 DESCRIPTION

Context object passed to controllers for accessing OIDC functionality.

=head1 ATTRIBUTES

=head2 catalyst

The Catalyst application instance.

=cut

has catalyst => (
    is  => 'ro',
    required => 1,
);

=head1 METHODS

=head2 jwt()

Returns the JWT handler instance.

=cut

sub jwt {
    my ($self) = @_;
    $self->catalyst->log->debug('Retrieving JWT handler') if $self->config->{debug};
    my $jwt = $self->catalyst->_oidc_jwt();
    unless ($jwt) {
        $self->catalyst->log->error('OpenID Connect JWT handler not initialized');
        die 'OpenID Connect JWT handler not initialized. Check your Plugin::OpenIDConnect configuration (issuer.private_key_file and issuer.public_key_file required).';
    }
    return $jwt;
}

=head2 store()

Returns the state store instance.

=cut

sub store {
    my ($self) = @_;
    $self->catalyst->log->debug('Retrieving state store') if $self->config->{debug};
    my $store = $self->catalyst->_oidc_store();
    return $store if $store;

    $self->catalyst->log->debug('Creating new state store instance') if $self->config->{debug};

    my $store_class = $self->config->{store_class}
        || 'Catalyst::Plugin::OpenIDConnect::Utils::Store';
    my $store_args  = { %{ $self->config->{store_args} || {} } };

    # Allow the Redis password to be supplied via the environment so
    # that secrets are not embedded in application config files.
    if ( !exists $store_args->{password} && defined $ENV{REDIS_PASSWORD} && $ENV{REDIS_PASSWORD} ne '' ) {
        $store_args->{password} = $ENV{REDIS_PASSWORD};
    }

    require Module::Runtime;
    Module::Runtime::require_module($store_class);

    my $new_store = $store_class->new(
        logger => $self->catalyst->log,
        %$store_args,
    );
    $self->catalyst->_oidc_store($new_store) if $self->catalyst->can('_oidc_store');
    return $new_store;
}

=head2 config()

Returns the OIDC configuration.

=cut

sub config {
    my ($self) = @_;
    return $self->catalyst->config->{'Plugin::OpenIDConnect'} || {};
}

=head2 get_client($client_id)

Retrieves a client configuration by client ID.

=cut

sub get_client {
    my ( $self, $client_id ) = @_;
    $self->catalyst->log->debug("Looking up client: $client_id") if $self->config->{debug};
    my $clients = $self->config->{clients} || {};



( run in 0.371 second using v1.01-cache-2.11-cpan-13bb782fe5a )