« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/PHP/Session.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module/SocialMedia/Twitter.pm »
| 1 | package Dwarf::Module::SocialMedia::Rakuten; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module'; | ||
| 4 | use Dwarf::HTTP::Async; | ||
| 5 | use HTTP::Request::Common (); | ||
| 6 | use JSON; | ||
| 7 | use LWP::UserAgent; | ||
| 8 | use MIME::Base64 qw/decode_base64 encode_base64url/; | ||
| 9 | use Dwarf::Util qw/safe_decode_json encode_utf8/; | ||
| 10 | use Digest::SHA qw/hmac_sha256_base64/; | ||
| 11 | use URI::Escape; | ||
| 12 | |||
| 13 | use Dwarf::Accessor qw/ | ||
| 14 | ua urls | ||
| 15 | key secret | ||
| 16 | access_token | ||
| 17 | user_id name profile_image | ||
| 18 | on_error | ||
| 19 | /; | ||
| 20 | |||
| 21 | sub init { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | $self->{ua} ||= LWP::UserAgent->new; | ||
| 25 | |||
| 26 | $self->{urls} ||= { | ||
| 27 | api => 'https://app.rakuten.co.jp/engine/api/MemberInformation/GetOpenIdUserInfo/20160715', | ||
| 28 | access_token => 'https://app.rakuten.co.jp/engine/idtoken', | ||
| 29 | }; | ||
| 30 | |||
| 31 | $self->{on_error} ||= sub { die @_ }; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub show_user { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | die 'access token must be specified.' unless defined $self->access_token; | ||
| 38 | |||
| 39 | my $params = {}; | ||
| 40 | $params->{access_token} = $self->access_token; | ||
| 41 | |||
| 42 | my $uri = URI->new($self->urls->{api}); | ||
| 43 | $uri->query_form(%$params); | ||
| 44 | |||
| 45 | my $res = $self->ua->post($uri); | ||
| 46 | |||
| 47 | my $content = eval { safe_decode_json(encode_utf8 $res->decoded_content) }; | ||
| 48 | if ($@) { | ||
| 49 | warn "Couldn't decode JSON: $@"; | ||
| 50 | $content = $res->decoded_content; | ||
| 51 | } | ||
| 52 | |||
| 53 | return $content; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub request_access_token { | ||
| 57 | my ($self, %params) = @_; | ||
| 58 | |||
| 59 | die 'key must be specified.' unless defined $self->key; | ||
| 60 | die 'secret must be specified.' unless defined $self->secret; | ||
| 61 | die "redirect_uri must be specified." unless defined $params{redirect_uri}; | ||
| 62 | die "code must be specified." unless defined $params{code}; | ||
| 63 | |||
| 64 | $params{grant_type} = "authorization_code"; | ||
| 65 | $params{client_id} ||= $self->key; | ||
| 66 | $params{client_secret} ||= $self->secret; | ||
| 67 | |||
| 68 | my $uri = URI->new($self->urls->{access_token}); | ||
| 69 | $uri->query_form(%params); | ||
| 70 | |||
| 71 | my $res = $self->ua->post($uri); | ||
| 72 | |||
| 73 | if ($res->code !~ /^2/) { | ||
| 74 | $self->on_error->('Rakuten OAuth Error: Could not get access token.'); | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | my $content = eval { decode_json $res->decoded_content }; | ||
| 79 | if ($@) { | ||
| 80 | warn "Couldn't decode JSON: $@"; | ||
| 81 | $content = $res->decoded_content; | ||
| 82 | } | ||
| 83 | |||
| 84 | $self->_validate_id_token($content->{id_token}); | ||
| 85 | |||
| 86 | my $access_token = $content->{access_token}; | ||
| 87 | |||
| 88 | $self->access_token($access_token); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _validate_id_token { | ||
| 92 | my ($self, $id_token) = @_; | ||
| 93 | |||
| 94 | my ($header, $payload, $signature) = split /\./, $id_token; | ||
| 95 | |||
| 96 | my $alg = eval { decode_json(decode_base64 $header)->{alg} }; | ||
| 97 | die "Something wrong with JWT header. " if $@; | ||
| 98 | |||
| 99 | my $data = eval { decode_json(decode_base64 $payload) }; | ||
| 100 | die "Something wrong with JWT payload." if $@; | ||
| 101 | |||
| 102 | my $valid_signature; | ||
| 103 | if ($alg eq "HS256") { | ||
| 104 | $valid_signature = hmac_sha256_base64($header . "." . $payload, $self->secret); | ||
| 105 | while (length($valid_signature) % 4) { | ||
| 106 | $valid_signature .= "="; | ||
| 107 | } | ||
| 108 | |||
| 109 | # hmac_sha256 -> base64url encode のやり方が分からないので一旦これで... | ||
| 110 | $valid_signature = encode_base64url decode_base64 $valid_signature; | ||
| 111 | } | ||
| 112 | |||
| 113 | die "Something wrong with JWT header. Couldn't specify hash algorythm." unless $valid_signature; | ||
| 114 | die "Invalid signature." unless $signature eq $valid_signature; | ||
| 115 | |||
| 116 | die "Wrong value: payload.iss" unless $data->{iss} && $data->{iss} eq "https://app.rakuten.co.jp/"; | ||
| 117 | die "Wrong value: payload.aud" unless $data->{aud} && $data->{aud} eq $self->key; | ||
| 118 | |||
| 119 | die "Wrong value: payload.exp" unless $data->{exp} && $data->{exp} > time(); | ||
| 120 | die "Wrong value: payload.iat" unless $data->{iat} && $data->{iat} <= time() && time() <= ($data->{iat} + 300); | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; |