Browser-OIDC

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


     my $oidc = Browser::OIDC->new($config);

    This creates a new Browser::OIDC object. This use a OIDC discovery
    configuration instead of fetching it.

 get_token

     $oidc->get_token(%options);

    This fetches OIDC tokens from the endpoint. Note though that this
    module only fetches the tokens, it will not perform any decoding or
    verification on them.

    It takes the following options:

    client_id

      The client identifier. Mandatory.

    client_secret

lib/Browser/OIDC.pm  view on Meta::CPAN

sub new_from_discovery($class, $target_base) {
	validate_url('target_base', $target_base);
	my $response = $ua->get("$target_base/.well-known/openid-configuration")->result;
	croak "Error: ". $response->code if not $response->is_success;
	my $config = $response->json;
	croak "Error: non-matching issuer" if $config->{issuer} ne $target_base;
	return $class->new_from_config($config);
}

sub new_from_config($class, $config) {
	validate_url($_, $config->{$_}) for qw/issuer authorization_endpoint token_endpoint jwks_uri/;
	defined $config->{$_} && validate_url($_, $config->{$_}) for qw/userinfo_endpoint/;
	my $auth_methods_supported = $config->{token_endpoint_auth_methods_supported} // ['client_secret_basic'];
	my %auth_methods_supported = map { $_ => 1 } $auth_methods_supported->@*;
	my $auth_method = first { $auth_methods_supported{$_} } qw/client_secret_post client_secret_basic none/;
	croak("Don't know how to authenticate token") unless $auth_method;
	my $grant_types_supported = $config->{grant_types_supported} // ['authorization_code'];
	croak "Only Authorization Code Flow is supported yet" if none { $_ eq 'authorization_code' } $grant_types_supported->@*;
	my $response_types_supported = $config->{response_types_supported} // ['code'];
	croak "code is required for authorization code flow"  if none { $_ eq 'code' } $response_types_supported->@*;

	return bless {
		$config->%{qw/issuer authorization_endpoint token_endpoint userinfo_endpoint claims_supported scopes_supported jwks_uri/},
		auth_method  => $auth_method,
		response_iss => !!$config->{authorization_response_iss_parameter_supported},
		ua           => $ua,
	}, $class;
}

my sub form_escape($input) {
	return url_escape(encode('UTF-8', $input))  =~ s/%20/+/gr;
}

lib/Browser/OIDC.pm  view on Meta::CPAN

			}
		} else {
			$tx->res->code(400);
			$tx->resume;
		}
	});
	$daemon->start;

	my ($port) = $daemon->ports->@*;
	my $redirect_uri = "http://localhost:$port/auth/callback";
	my $full_target = Mojo::URL->new($self->{authorization_endpoint});
	$full_target->query->append(response_type => 'code');
	$full_target->query->append(redirect_uri => $redirect_uri);
	$full_target->query->append(client_id => $client_id);
	my $code_verifier = encode_base64url(random_bytes(32));
	$full_target->query->append(code_challenge => encode_base64url(sha256($code_verifier)));
	$full_target->query->append(code_challenge_method => 'S256');
	my @scope = 'openid';
	push @scope, $options{scope}->@*  if $options{scope};
	$full_target->query->append(scope => join ' ', @scope);
	$full_target->query->append(state => $state);

lib/Browser/OIDC.pm  view on Meta::CPAN

	my $client_secret = $options{client_secret} // '';
	if ($self->{auth_method} eq 'client_secret_post') {
		$arguments{client_id} = $client_id;
		$arguments{client_secret} = $client_secret;
	} elsif ($self->{auth_method} eq 'client_secret_basic') {
		my $auth = join ':', form_escape($client_id), form_escape($client_secret);
		$headers{Authorization} = 'Basic ' . encode_base64($auth, '');
	} else {
		$arguments{client_id} = $client_id;
	}
	my $response = $ua->post($self->{token_endpoint}, \%headers, form => \%arguments)->result;
	if ($response->is_success) {
		return Browser::OIDC::TokenResponse->new(parent => $self, value => $response->json, nonce => $nonce);
	} else {
		croak sprintf "Could not get token (%s): %s", $response->code , term_escape($response->body);
	}
}

sub issuer($self) {
	return $self->{issuer};
}

lib/Browser/OIDC.pm  view on Meta::CPAN

=head2 new_from_config

 my $oidc = Browser::OIDC->new($config);

This creates a new C<Browser::OIDC> object. This use a OIDC discovery configuration instead of fetching it.

=head2 get_token

 $oidc->get_token(%options);

This fetches OIDC tokens from the endpoint. Note though that this module only fetches the tokens, it will not perform any decoding or verification on them.

It takes the following options:

=over 4

=item client_id

The client identifier. Mandatory.

=item client_secret

lib/Browser/OIDC/TokenResponse.pm  view on Meta::CPAN

	no strict 'refs';
	*$name = $sub;
}

sub nonce($self) {
	return $self->{nonce};
}

sub userinfo($self) {
	my $parent = $self->{parent};
	my $url = $parent->{userinfo_endpoint} // croak 'No userinfo URL';
	my $authorization = join ' ', $self->{value}{token_type}, $self->{value}{access_token};
	my $response = $parent->{ua}->get($parent->{userinfo_endpoint}, { Authorization => $authorization })->result;
	croak "Could not fetch userinfo " . term_escape($response->message) if not $response->is_success;
	return $response->json;
}

1;

=head1 SYNOPSIS

 my $oidc = Browser::OIDC->new($url);
 my $token_response = $oidc->get_token(%args);



( run in 3.360 seconds using v1.01-cache-2.11-cpan-99fa8243a42 )