#!/usr/bin/env perl # # This plack demo shows the logic which your website needs to facilitate # third-party OAuth2 authorization. # This code only works when you plack server runs in a single process: # otherwise you will need to keep the session information in some # shared store, for instance a database. That makes life considerable # harder. use strict; use warnings; use Dancer; use Net::OAuth2::Client; use HTML::Entities; get '/get/:site_id' => sub { redirect client(params->{site_id})->authorize; }; get '/got/:site_id' => sub { defined params->{code} or return html_page("Error: Missing access code"); my $site_id = params->{site_id}; my $access_token = client($site_id)->get_access_token(params->{code}); return html_page("Error: " . $access_token->to_string) if $access_token->{error}; my $content = "
'.encode_entities($access_token->to_string)."
\n"; my $this_site = config->{sites}{$site_id}; my $response = $access_token->get($this_site->{protected_resource_url} || $this_site->{protected_resource_path}); if ($response->is_success) { $content .= "'.encode_entities($response->decoded_content).'
'; } else { $content .= 'Error: '. $response->status_line."
\n"; } $content =~ s[\n][$v->{name}: /get/$k
\n} if $v->{client_id} && $v->{client_secret}; } $content ||= "You haven't configured any sites yet.
\n"
. "Edit your config.yml file!