| | 5 | |
|---|
| | 6 | our $VERSION = '0.01'; |
|---|
| | 7 | |
|---|
| | 8 | use WebService::JugemKey::Auth; |
|---|
| | 9 | use UNIVERSAL::require; |
|---|
| | 10 | use NEXT; |
|---|
| | 11 | |
|---|
| | 12 | sub setup { |
|---|
| | 13 | my $c = shift; |
|---|
| | 14 | |
|---|
| | 15 | my $config = $c->config->{authentication}->{jugemkey} ||= {}; |
|---|
| | 16 | |
|---|
| | 17 | $config->{jugemkey_object} ||= do { |
|---|
| | 18 | ( $config->{user_class} |
|---|
| | 19 | ||= 'Catalyst::Plugin::Authentication::User::Hash' )->require; |
|---|
| | 20 | |
|---|
| | 21 | WebService::JugemKey::Auth->new( |
|---|
| | 22 | { api_key => $config->{api_key}, |
|---|
| | 23 | secret => $config->{secret}, |
|---|
| | 24 | } |
|---|
| | 25 | ); |
|---|
| | 26 | }; |
|---|
| | 27 | |
|---|
| | 28 | $c->NEXT::setup(@_); |
|---|
| | 29 | } |
|---|
| | 30 | |
|---|
| | 31 | sub authenticate_jugemkey_url { |
|---|
| | 32 | my ($c, $params) = @_; |
|---|
| | 33 | warn $params; |
|---|
| | 34 | $c->config->{authentication}->{jugemkey}->{jugemkey_object}->uri_to_login($params); |
|---|
| | 35 | } |
|---|
| | 36 | |
|---|
| | 37 | sub authenticate_jugemkey_get_token { |
|---|
| | 38 | my $c = shift; |
|---|
| | 39 | |
|---|
| | 40 | my $config = $c->config->{authentication}->{jugemkey}; |
|---|
| | 41 | my $jugemkey = $config->{jugemkery_object}; |
|---|
| | 42 | |
|---|
| | 43 | my $frob = $c->req->params->{frob} or return; |
|---|
| | 44 | |
|---|
| | 45 | if ( my $user = $jugemkey->get_token($frob) ) { |
|---|
| | 46 | $c->log->debug("Successfully get token of user '$user->name'.") |
|---|
| | 47 | if $c->debug; |
|---|
| | 48 | |
|---|
| | 49 | my $store = $config->{store} || $c->default_auth_store; |
|---|
| | 50 | if ( $store |
|---|
| | 51 | and my $store_user = $store->get_user( $user->name, $user ) ) |
|---|
| | 52 | { |
|---|
| | 53 | $c->set_authenticated($store_user); |
|---|
| | 54 | } |
|---|
| | 55 | else { |
|---|
| | 56 | $user = $config->{user_class}->new($user); |
|---|
| | 57 | $c->set_authenticated($user); |
|---|
| | 58 | } |
|---|
| | 59 | |
|---|
| | 60 | return 1; |
|---|
| | 61 | } |
|---|
| | 62 | else { |
|---|
| | 63 | $c->log->debug( |
|---|
| | 64 | sprintf "Failed to authenticate hatena. Reason: '%s'", |
|---|
| | 65 | $jugemkey->errstr, ) |
|---|
| | 66 | if $c->debug; |
|---|
| | 67 | |
|---|
| | 68 | return; |
|---|
| | 69 | } |
|---|
| | 70 | } |
|---|
| | 71 | |
|---|