| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Net::OAuth2::Scheme::Mixin::NextID; | ||
| 5 | BEGIN { | ||
| 6 | $Net::OAuth2::Scheme::Mixin::NextID::VERSION = '0.020002_099'; | ||
| 7 | } | ||
| 8 | # ABSTRACT: the 'v_id_next', 'counter', and 'random' option groups | ||
| 9 | |||
| 10 | use Net::OAuth2::Scheme::Option::Defines; | ||
| 11 | use Net::OAuth2::Scheme::Random; | ||
| 12 | use Net::OAuth2::Scheme::Counter; | ||
| 13 | |||
| 14 | # INTERFACE v_id | ||
| 15 | # DEFINES | ||
| 16 | # v_id_next | ||
| 17 | # v_id_is_random | ||
| 18 | # v_id_get_suffix | ||
| 19 | |||
| 20 | Define_Group v_id => 'default', | ||
| 21 | qw(v_id_next v_id_is_random v_id_get_suffix); | ||
| 22 | |||
| 23 | Default_Value v_id_random_length => 12; | ||
| 24 | Default_Value v_id_suffix => ''; | ||
| 25 | |||
| 26 | sub pkg_v_id_default { | ||
| 27 | my __PACKAGE__ $self = shift; | ||
| 28 | my $which = $self->uses('v_id_suggest', 'counter'); | ||
| 29 | my $pkg = "pkg_v_id_${which}"; | ||
| 30 | return $self->$pkg(@_); | ||
| 31 | } | ||
| 32 | |||
| 33 | # IMPLEMENTATION v_id_random FOR v_id | ||
| 34 | # REQUIRES | ||
| 35 | # random | ||
| 36 | # OPTIONS | ||
| 37 | # v_id_random_length | ||
| 38 | # v_id_suffix | ||
| 39 | |||
| 40 | sub pkg_v_id_random { | ||
| 41 | my __PACKAGE__ $self = shift; | ||
| 42 | $self->parameter_prefix(qw(v_id_ _default random_length) => @_); | ||
| 43 | $self->make_alias(qw(v_id_random_length v_id_length)); | ||
| 44 | if ($self->is_auth_server) { | ||
| 45 | my $random = $self->uses('random'); | ||
| 46 | my $length = $self->uses('v_id_random_length'); | ||
| 47 | my $suffix = $self->uses('v_id_suffix'); | ||
| 48 | |||
| 49 | $self->croak("v_id_length must be at least 8") | ||
| 50 | unless $length >= 8; | ||
| 51 | $self->install(v_id_is_random => 1); | ||
| 52 | my $l = pack 'w', $length+56; | ||
| 53 | $l = ((ord($l)&0xc0)==0xc0 ? chr(0x00).$l : | ||
| 54 | (ord($l)&0xc0)==0x80 ? chr(0x80)^$l : | ||
| 55 | $l); | ||
| 56 | $self->install(v_id_next => sub { | ||
| 57 | return $l . $random->($length) . $suffix; | ||
| 58 | }); | ||
| 59 | } | ||
| 60 | if ($self->is_resource_server) { | ||
| 61 | $self->install(v_id_get_suffix => sub { | ||
| 62 | my $v = shift; | ||
| 63 | my ($l,$rest) = unpack 'wa*', (ord($v)&0x40)==0 ? chr(0x80)^$v : $v; | ||
| 64 | return substr($rest,$l-56); | ||
| 65 | }); | ||
| 66 | } | ||
| 67 | return $self; | ||
| 68 | } | ||
| 69 | |||
| 70 | # IMPLEMENTATION v_id_counter FOR v_id | ||
| 71 | # REQUIRES | ||
| 72 | # counter | ||
| 73 | # OPTIONS | ||
| 74 | # v_id_counter_tag | ||
| 75 | # v_id_suffix | ||
| 76 | |||
| 77 | sub pkg_v_id_counter { | ||
| 78 | my __PACKAGE__ $self = shift; | ||
| 79 | $self->parameter_prefix(qw(v_id_ _default counter_tag) => @_); | ||
| 80 | $self->make_alias(v_id_counter_tag => 'counter_tag'); | ||
| 81 | if ($self->is_resource_server) { | ||
| 82 | $self->install(v_id_get_suffix => $self->uses('counter_get_suffix')); | ||
| 83 | } | ||
| 84 | if ($self->is_auth_server) { | ||
| 85 | my $counter = $self->uses('counter'); | ||
| 86 | my $suffix = $self->uses('v_id_suffix'); | ||
| 87 | $self->install(v_id_is_random => 0); | ||
| 88 | $self->install(v_id_next => sub { | ||
| 89 | return $counter->next() . $suffix; | ||
| 90 | }); | ||
| 91 | } | ||
| 92 | return $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | # INTERFACE counter | ||
| 97 | # SUMMARY | ||
| 98 | # generate a sequence of bytes different from every previous sequence produced | ||
| 99 | # and from every other possible sequence that can be produced from this same code | ||
| 100 | # running in any other process or thread. | ||
| 101 | # DEFINES | ||
| 102 | # counter object with a 'next' method () -> string of bytes | ||
| 103 | |||
| 104 | Define_Group counter_set => 'default', qw(counter counter_get_suffix); | ||
| 105 | |||
| 106 | Default_Value counter_tag => ''; | ||
| 107 | |||
| 108 | sub pkg_counter_set_default { | ||
| 109 | my __PACKAGE__ $self = shift; | ||
| 110 | if ($self->is_auth_server) { | ||
| 111 | my $tag = $self->uses('counter_tag'); | ||
| 112 | $self->install('counter', Net::OAuth2::Scheme::Counter->new($tag)); | ||
| 113 | } | ||
| 114 | if ($self->is_resource_server) { | ||
| 115 | $self->install('counter_get_suffix', \&Net::OAuth2::Scheme::Counter::suffix); | ||
| 116 | } | ||
| 117 | return $self; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | # INTERFACE random | ||
| 122 | # SUMMARY | ||
| 123 | # generate random bytes in a cryptographically secure mannter | ||
| 124 | # DEFINES | ||
| 125 | # random (n)-> string of n random octets | ||
| 126 | |||
| 127 | # default implementation | ||
| 128 | Default_Value random_class => 'Math::Random::MT::Auto'; | ||
| 129 | |||
| 130 | Define_Group random_set => 'default', qw(random); | ||
| 131 | |||
| 132 | sub pkg_random_set_default { | ||
| 133 | require Net::OAuth2::Scheme::Random; | ||
| 134 | my __PACKAGE__ $self = shift; | ||
| 135 | my $rng = Net::OAuth2::Scheme::Random->new($self->installed('random_class')); | ||
| 136 | $self->install( random => sub { $rng->bytes(@_) }); | ||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 1; | ||
| 142 | |||
| 143 | |||
| 144 | __END__ |