How to authenticate against \OpenID and get roles from local DBIC

  1. Follow the Catalyst tutorial and enable the authentication and session layers.

  2. Install these two CPAN modules:

$ cpan -i Catalyst::Authentication::Credential::OpenID
$ cpan -i Catalyst::Authentication::Store::DBIx::Class
  1. Add the following to myapp.yml:
authentication:
  default_realm: openid
  realms:
    openid:
      credential:
        class: OpenID
    dbic:
      credential:
        class: Password
        password_type: none
      store:
        class: DBIx::Class
        user_class: MyAppDB::User
        id_field: id
        role_column: user_role
  1. Add the following code to lib/MyApp/Controller/Root.pm:
sub login : Local {
  my ( $self, $c ) = @_;

  # eval necessary because LWPx::ParanoidAgent
  # croaks if invalid URL is specified
  eval {
    # Authenticate against OpenID to get user URL
    if ( $c->authenticate({}, 'openid' ) ) {
      $c->flash->{'status_msg'}='OpenID login was successful.';

      # Create basic user entry unless already found
      # (or use auto_create_user: 1)
      unless ( $c->model('MyAppDB::User')->find($c->user->url) ) {
        $c->model('MyAppDB::User')->create(
         { id => $c->user->url }
        );
      }

      # Re-authenticate against local DBIC store
      if ( $c->authenticate({ id => $c->user->url }, 'dbic') ) {
        $c->flash->{'status_msg'}='Login was successful.';
        $c->response->redirect( $c->uri_for("/") );
      }
      else {
        $c->flash->{'error_msg'}='Local login failed.';
        $c->stash->{'template'}='login.tt';
      }
    }
    else {
      $c->stash->{'template'}='login.tt';
    }
  };

  if ($@) {
    $c->log->error("Failure during login: " . $@);
    $c->flash->{'error_msg'}='Failure during login: ' . $@;
    $c->stash->{'template'}='login.tt';
  }
}
  1. Create a form to use this method (root/login.tt):
<form action="[% c.uri_for('/login') %]" method="get">
<input type="text" name="openid_identifier" value="http://" />
<button type="submit">Sign in with OpenID</button>
</form>
My tags:
 
Popular tags:
 
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker