| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Net::OAuth2::Scheme::Mixin::Accept; | ||
| 5 | BEGIN { | ||
| 6 | $Net::OAuth2::Scheme::Mixin::Accept::VERSION = '0.020002_099'; | ||
| 7 | } | ||
| 8 | # ABSTRACT: defines 'token_accept' | ||
| 9 | |||
| 10 | use Net::OAuth2::Scheme::Option::Defines; | ||
| 11 | |||
| 12 | # FUNCTION token_accept | ||
| 13 | # token[, issue_attributes] -> error, token[, save_attributes] | ||
| 14 | # SUMMARY | ||
| 15 | # issue_attributes and send_attributes are the same | ||
| 16 | # IMPLEMENTATION token_accept_default | ||
| 17 | # (accept_)token_type_re = '' | ||
| 18 | # (accept_)remove = [qw(expires_in scope refresh_token)] | ||
| 19 | # (accept_)keep = [...] or 'everything' | ||
| 20 | # REQUIRES | ||
| 21 | # accept_needs | ||
| 22 | # accept_hook | ||
| 23 | |||
| 24 | Define_Group token_accept => 'default'; | ||
| 25 | |||
| 26 | Default_Value accept_remove => [qw(expires_in scope refresh_token)]; | ||
| 27 | Default_Value accept_keep => 'everything'; | ||
| 28 | |||
| 29 | sub pkg_token_accept_default { | ||
| 30 | my __PACKAGE__ $self = shift; | ||
| 31 | $self->parameter_prefix(accept_ => @_); | ||
| 32 | |||
| 33 | # these two cases are probably not necessary. In fact, now that I | ||
| 34 | # think about it 'token_accept' for refresh tokens and authcodes | ||
| 35 | # should be completely inaccessible to clients, but maybe I'll | ||
| 36 | # change my mind about this... | ||
| 37 | |||
| 38 | if ($self->uses('usage') eq 'authcode') { | ||
| 39 | # authcode is the token string ONLY | ||
| 40 | $self->install( token_accept => sub { return (undef, $_[0]); } ); | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | # ditto... | ||
| 45 | if ($self->uses('usage') eq 'refresh') { | ||
| 46 | # refresh is the token string ONLY | ||
| 47 | $self->install( token_accept => sub { | ||
| 48 | my ($token, %params) = @_; | ||
| 49 | $token = $params{refresh_token} if $params{refresh_token}; | ||
| 50 | return (undef, $token); | ||
| 51 | }); | ||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | # now for the real stuff | ||
| 56 | my ($token_type, $remove, $keep, $needs, $hook) = $self->uses_all | ||
| 57 | (qw(token_type accept_remove accept_keep accept_needs accept_hook)); | ||
| 58 | |||
| 59 | $self->install( token_accept => sub { | ||
| 60 | my ($token, %params); | ||
| 61 | if (0 == @_ % 2) { | ||
| 62 | # token_accept( access_token => $token [, kwd => $value]* ) | ||
| 63 | %params = @_; | ||
| 64 | $token = delete $params{access_token} | ||
| 65 | or return ('no_access_token'); | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | # token_accept( $token [, kwd => $value]* ) | ||
| 69 | ($token, %params) = @_; | ||
| 70 | } | ||
| 71 | |||
| 72 | return ('wrong_token_type') | ||
| 73 | if (lc($params{token_type}) ne lc($token_type)); | ||
| 74 | |||
| 75 | my ($error) = $hook->(\%params); | ||
| 76 | return ($error) if $error; | ||
| 77 | |||
| 78 | # (1) everything in @$needs is passed through (as %save) | ||
| 79 | # and we die if any of them are missing | ||
| 80 | my @missing = (); | ||
| 81 | my %save = map { | ||
| 82 | my $v = $params{$_}; | ||
| 83 | push @missing,$_ if !defined($v) || !length($v); | ||
| 84 | ($_,$v) | ||
| 85 | } @$needs; | ||
| 86 | return ("missing_$missing[0]") if @missing; | ||
| 87 | |||
| 88 | # (2) if $keep is a listref, | ||
| 89 | # we keep JUST those additional fields (i.e., beyond @$needs) | ||
| 90 | # otherwise we keep everything that is not in @$remove | ||
| 91 | if (ref $keep) { | ||
| 92 | %params = map {$save{$_}? (): ($_,$params{$_})} @$keep; | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | delete @{params}{@$remove,@$needs}; | ||
| 96 | } | ||
| 97 | return (undef, $token, %params, %save); | ||
| 98 | }); | ||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | |||
| 106 | __END__ |