Addendum to TypeKey-Catalyst integration

LTjake on 2006-04-20T17:34:17

Since my initial tutorial on integrating TypeKey authentication with local a Catalyst database user, there have been a few significant changes that greatly simplify the process.

The new method requires the latest Catalyst::Plugin::Authentication::Store::DBIC from the svn repository. (Note: A new version of this plugin will soon to be released to CPAN. Watch this space.)

  1. Credential/Store config

    The configuration is much the same as before, except for two options: auto_create_user and auto_update_user

    authentication:
      typekey:
        server           : http://typekeyserver.com/
        key_url          : http://typekeyserver.com/regkeys.txt
        version          : 1
        auth_store       : default
      dbic:
        user_class      : Accounts::Account
        user_field      : username
        auto_create_user: 1
        auto_update_user: 1
    authorization:
      dbic:
        role_class          : Accounts::Role
        role_field          : name
        role_rel            : account_roles
        user_role_user_field: account
    

    Those two options will drive the functionality that we emulated in the old "AutoCreate" plugin.

  2. User Class Modification

    The plugin will call auto_create and auto_update on your account class when those events happen.

    For auto_create to work, it needs to be called on a ResultSet, so you should use the ResultSetManager in your user class. Each sub gets a number of parameters, the last one being the results of the typekey authentication (a hashref with name, nick, email and ts keys). Use that hashref to add or update the user.

    NB: it's possible for auto_update to get called in situations where it hasn't authenticated, so it's important to get out of the process if the data doesn't exist.

    package MyApp::Schema::Account;
    
    use strict;
    use warnings;
    
    use base qw( DBIx::Class );
    
    __PACKAGE__->load_components( qw( PK::Auto ResultSetManager Core ) );
    
    # ...
    
    sub auto_create : ResultSet {
        my( $self, $username, $req, $res ) = @_;
        $self->create( {
            # Deal with $res here
        } );
    }
    
    sub auto_update {
        my( $self, $username, $req, $res ) = @_;
        return unless $res;
    
        # Deal with $res here
    
        $self->update;
    }
    
    1;
    

That's it! A much tighter process, don't you think?