Catalyst-Plugin-OpenIDConnect
view release on metacpan or search on metacpan
Session
Session::Store::File
Session::State::Cookie
/;
# Load the controller before setup
use MyApp::Controller::OpenIDConnect;
```
### 3. Configure in your catalyst.conf
```
<Plugin::OpenIDConnect>
<issuer>
url = http://localhost:5000
private_key_file = /path/to/private_key.pem
public_key_file = /path/to/public_key.pem
key_id = my-key-123
</issuer>
<clients>
<MyClient>
client_id = my-client-id
client_secret = my-client-secret
redirect_uris = http://localhost:3000/callback
post_logout_redirect_uris = http://localhost:3000/logged-out
response_types = code
grant_types = authorization_code refresh_token
scope = openid profile email
</MyClient>
</clients>
<user_claims>
sub = user.id
username = user.username
name = user.name
email = user.email
picture = user.avatar_url
</user_claims>
</Plugin::OpenIDConnect>
```
### 4. Implement a login action
Your app must have a login action that supports the `back` parameter. When a user is not authenticated, the plugin redirects to your login page with a `back` parameter indicating where to return:
```perl
package MyApp::Controller::Auth;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }
sub login : Local {
my ( $self, $c ) = @_;
if ( $c->request->method eq 'POST' ) {
my $username = $c->request->params->{username};
my $password = $c->request->params->{password};
# Validate credentials
if ( validate_credentials($username, $password) ) {
my $user = get_user($username);
$c->session->{user} = $user;
# IMPORTANT: Redirect to 'back' parameter to resume OIDC flow
my $back = $c->request->params->{back} || '/';
return $c->response->redirect($back);
}
$c->stash->{error} = 'Invalid credentials';
}
$c->stash->{template} = 'login.html';
}
1;
```
### 5. Use in your controllers
```perl
package MyApp::Controller::Protected;
use Moose;
use namespace::autoclean;
BEGIN { extends 'Catalyst::Controller'; }
sub profile : Local {
my ( $self, $c ) = @_;
# Check if user is authenticated via OIDC
unless ( $c->user ) {
$c->response->redirect( $c->uri_for('/openidconnect/authorize') );
return;
}
$c->stash->{user} = $c->user;
}
1;
```
## API Endpoints
### Authorization Endpoint
```
GET /openidconnect/authorize
```
Parameters:
- `response_type` (required): "code"
- `client_id` (required): Client ID
- `redirect_uri` (required): Registered redirect URI
- `scope` (optional): Space-separated list of scopes (default: "openid")
- `state` (recommended): CSRF protection token
- `nonce` (optional): String to bind to session
### Token Endpoint
```
POST /openidconnect/token
Content-Type: application/x-www-form-urlencoded
```
Parameters:
- `grant_type` (required): "authorization_code"
- `code` (required): Authorization code
- `client_id` (required): Client ID
- `client_secret` (required): Client secret
( run in 0.577 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )