[Jifty-commit] r4252 - in jifty/trunk: lib/Jifty/Plugin lib/Jifty/Plugin/OAuth lib/Jifty/Plugin/OAuth/Model

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Oct 17 15:43:23 EDT 2007


Author: sartak
Date: Wed Oct 17 15:43:22 2007
New Revision: 4252

Added:
   jifty/trunk/lib/Jifty/Plugin/OAuth/
   jifty/trunk/lib/Jifty/Plugin/OAuth.pm
   jifty/trunk/lib/Jifty/Plugin/OAuth/Dispatcher.pm
   jifty/trunk/lib/Jifty/Plugin/OAuth/Model/
   jifty/trunk/lib/Jifty/Plugin/OAuth/Model/AccessToken.pm
   jifty/trunk/lib/Jifty/Plugin/OAuth/Model/RequestToken.pm
   jifty/trunk/lib/Jifty/Plugin/OAuth/View.pm
Modified:
   jifty/trunk/   (props changed)

Log:
 r43816 at onn:  sartak | 2007-10-17 15:43:11 -0400
 Start adding an OAuth plugin


Added: jifty/trunk/lib/Jifty/Plugin/OAuth.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth.pm	Wed Oct 17 15:43:22 2007
@@ -0,0 +1,31 @@
+package Jifty::Plugin::OAuth;
+use strict;
+use warnings;
+
+use base qw/Jifty::Plugin/;
+
+our $VERSION = 0.01;
+
+=head1 NAME
+
+Jifty::Plugin::OAuth
+
+=head1 DESCRIPTION
+
+A OAuth web services API for your Jifty app.
+
+=head1 WARNING
+
+This plugin is not yet complete. DO NOT USE IT.
+
+=head1 USAGE
+
+Add the following to your site_config.yml
+
+ framework:
+   Plugins:
+     - OAuth: {}
+
+=cut
+
+1;

Added: jifty/trunk/lib/Jifty/Plugin/OAuth/Dispatcher.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth/Dispatcher.pm	Wed Oct 17 15:43:22 2007
@@ -0,0 +1,74 @@
+package Jifty::Plugin::OAuth::Dispatcher;
+use warnings;
+use strict;
+
+use Jifty::Dispatcher -base;
+
+my $request_token_url = '/oauth/request_token';
+my $authorize_url     = '/oauth/authorize';
+my $access_token_url  = '/oauth/access_token';
+
+before POST $request_token_url => \&request_token;
+before GET  $authorize_url     => \&authorize;
+before POST $access_token_url  => \&access_token;
+
+# a consumer wants a request token
+sub request_token {
+    set oauth_url => $request_token_url;
+    my $headers = Jifty->web->handler->apache->headers_in();
+
+    for my $necessary_header (map {"oauth_$_"}
+                                  qw/consumer_key signature_method signature
+                                     timestamp nonce/) {
+        abort(400) if !defined $headers->{$necessary_header};
+    }
+
+}
+
+# the user is authorizing (or denying) a consumer's request token
+sub authorize {
+    set oauth_url => $authorize_url;
+
+}
+
+# the consumer is trying to trade a request token for an access token
+sub access_token {
+    set oauth_url => $access_token_url;
+
+}
+
+# 9.1.1
+sub get_normalized_parameters {
+    my $parameters = Jifty->handler->apache->headers_in();
+    my @parameters;
+
+    # we can't just use a hash because parameters may be repeated
+    $parameters->do(sub {
+        my ($key, $value) = @_;
+        push @parameters, [$key, defined($value) ? $value : ''];
+        return 1;
+    });
+
+    # XXX: include query parameters (http://x.com/path?THIS=THAT)
+
+    for (@parameters) {
+        @$_ = map { Jifty->web->escape_uri($_) } @$_;
+    }
+
+    return join '&',
+           map  { "$_->[0]=$_->[1]" }
+           grep { $_->[0] ne 'oauth_signature' }
+           sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @parameters;
+}
+
+# 9.1.2
+sub get_request_elements {
+    my $method          = uc Jifty->handler->apache->method();
+    my $url             = Jifty->web->url(get 'oauth_url');
+    my $parameters      = get_normalized_parameters();
+    my $consumer_secret = 'todo';
+    my $token_secret    = 'todo' || '';
+}
+
+1;
+

Added: jifty/trunk/lib/Jifty/Plugin/OAuth/Model/AccessToken.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth/Model/AccessToken.pm	Wed Oct 17 15:43:22 2007
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+package Jifty::Plugin::OAuth::Model::AccessToken;
+use strict;
+use warnings;
+
+use base qw( Jifty::Record );
+
+# kludge 1: you cannot call Jifty->app_class within schema {}
+my $app_user;
+BEGIN { $app_user = Jifty->app_class('Model', 'User') }
+
+use Jifty::DBI::Schema;
+use Jifty::Record schema {
+
+    # kludge 2: this kind of plugin cannot yet casually refer_to app models
+    column user =>
+        type is 'integer';
+        #refers_to $app_user;
+
+    column valid_until =>
+        type is 'timestamp',
+        filters are 'Jifty::DBI::Filter::DateTime';
+
+};
+
+sub before_create {
+    my ($self, $attr) = @_;
+    $attr{valid_until} ||= DateTime->now->add(hours => 1);
+}
+
+1;
+

Added: jifty/trunk/lib/Jifty/Plugin/OAuth/Model/RequestToken.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth/Model/RequestToken.pm	Wed Oct 17 15:43:22 2007
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+package Jifty::Plugin::OAuth::Model::RequestToken;
+use strict;
+use warnings;
+
+use base qw( Jifty::Record );
+
+# kludge 1: you cannot call Jifty->app_class within schema {}
+my $app_user;
+BEGIN { $app_user = Jifty->app_class('Model', 'User') }
+
+use Jifty::DBI::Schema;
+use Jifty::Record schema {
+
+    column valid_until =>
+        type is 'timestamp',
+        filters are 'Jifty::DBI::Filter::DateTime';
+
+    column authorized =>
+        type is 'boolean',
+        default is 'f';
+
+    # kludge 2: this kind of plugin cannot yet casually refer_to app models
+    column authorized_by =>
+        type is 'integer';
+        #refers_to $app_user;
+
+    column used =>
+        type is 'boolean',
+        default is 'f';
+
+};
+
+sub before_create {
+    my ($self, $attr) = @_;
+    $attr{valid_until} ||= DateTime->now->add(hours => 1);
+}
+
+sub set_authorized {
+    my $self = shift;
+    $self->set_authorized_by(Jifty->web->current_user->id);
+}
+
+sub trade_for_access_token {
+    my $self = shift;
+    return undef if !$self->authorized;
+    return undef if !$self->authorized_by;
+    return undef if $self->used;
+    return undef if $self->valid_until < DateTime->now;
+
+    my $access_token = Jifty::Plugin::OAuth::Model::AccessToken->new(current_user => Jifty::CurrentUser->superuser);
+    my ($ok, $msg) = $access_token->create(user => $self->authorized_by);
+
+    return undef if !$ok;
+    return $access_token;
+}
+
+1;
+

Added: jifty/trunk/lib/Jifty/Plugin/OAuth/View.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/OAuth/View.pm	Wed Oct 17 15:43:22 2007
@@ -0,0 +1,8 @@
+package Jifty::Plugin::OAuth::View;
+use strict;
+use warnings;
+
+use Jifty::View::Declare -base;
+
+1;
+


More information about the Jifty-commit mailing list