File: oauth2_client.pl

package info (click to toggle)
libnet-oauth2-authorizationserver-perl 0.28-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 3,087; sql: 111; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 1,750 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl

use strict;
use warnings;

use Mojolicious::Lite;
use Data::Dumper;
use IO::Socket::SSL;

# work around for example only (don't do this in PROD code)
IO::Socket::SSL::set_defaults(
  SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
);

my $host = $ENV{HOST} // '127.0.0.1';

plugin 'OAuth2', {
  overly_attached_social_network => {
     authorize_url => "https://$host:3000/oauth/authorize?response_type=code",
     token_url     => "https://$host:3000/oauth/access_token",
     key           => 'TrendyNewService',
     secret        => 'boo',
     scope         => 'post_images annoy_friends',
  },
};

app->helper(
  delay => sub {
    my $c  = shift;
    my $tx = $c->render_later->tx;
    Mojo::IOLoop->delay(@_)->catch(sub { $c->helpers->reply->exception(pop) and undef $tx })->wait;
  }
);

get '/auth' => sub {
  my $self = shift;
  if ( my $error = $self->param( 'error' ) ) {
    return $self->render(
      text => "Call to overly_attached_social_network returned: $error"
    );
  } else {
    $self->delay(
      sub {
        my $delay = shift;
        $self->oauth2->get_token( overly_attached_social_network => $delay->begin )
      },
      sub {
        my( $delay,$error,$data ) = @_;
        return $self->render( error => $error ) if ! $data->{access_token};
        return $self->render( json => $data );
      },
    );
  }
};

get '/' => sub {
  my ( $c ) = @_;
  $c->render( 'index' );
};

app->start;

# vim: ts=2:sw=2:et

__DATA__
@@ layouts/default.html.ep
<!doctype html><html>
  <head><title>TrendyNewService</title></head>
  <body><h3>Welcome to TrendyNewService</h3><%== content %></body>
</html>

@@ index.html.ep
% layout 'default';
<a href="/auth">Connect to Overly Attached Social Network</a>