google account export, #11760
[freeside.git] / FS / FS / part_export / acct_google.pm
1 package FS::part_export::acct_google;
2
3 use strict;
4 use vars qw(%info %SIG $CACHE);
5 use Tie::IxHash;
6 use base 'FS::part_export';
7
8 tie my %options, 'Tie::IxHash',
9   'domain'    => { label => 'Domain name' },
10   'username'  => { label => 'Admin username' },
11   'password'  => { label => 'Admin password' },
12 ;
13 # To handle multiple domains, use separate instances of 
14 # the export.  We assume that they all have different 
15 # admin logins.
16
17 %info = (
18   'svc'       => 'svc_acct',
19   'desc'      => 'Google hosted mail',
20   'options'   => \%options,
21   'nodomain'  => 'Y',
22   'notes'    => <<'END'
23 Export accounts to the Google Provisioning API.  Requires 
24 REST::Google::Apps::Provisioning from CPAN.
25 END
26 );
27
28 sub rebless { shift; }
29
30 sub _export_insert {
31   my($self, $svc_acct) = (shift, shift);
32   $svc_acct->finger =~ /^(.*)\s+(\S+)$/;
33   my ($first, $last) = ($1, $2);
34   $self->google_request('createUser',
35     'username'      => $svc_acct->username,
36     'password'      => $svc_acct->_password,
37     'givenName'     => $first,
38     'familyName'    => $last,
39   );
40 }
41
42 sub _export_replace {
43   my( $self, $new, $old ) = (shift, shift, shift);
44   # We have to do this in two steps, so do the renameUser last so that 
45   # if it fails partway through the username is still coherent.
46   if ( $new->_password ne $old->_password
47     or $new->finger    ne $old->finger ) {
48     $new->finger =~ /^(.*)\s+(\S+)$/;
49     my ($first, $last) = ($1, $2);
50     my $error = $self->google_request('updateUser',
51       'username'    => $old->username,
52       'password'    => $new->_password,
53       'givenName'   => $first,
54       'familyName'  => $last,
55     );
56     return $error if $error;
57   }
58   if ( $new->username ne $old->username ) {
59     my $error = $self->google_request('renameUser',
60       'username'  => $old->username,
61       'newname'   => $new->username
62     );
63     return $error if $error;
64   }
65   return;
66 }
67
68 sub _export_delete {
69   my( $self, $svc_acct ) = (shift, shift);
70   $self->google_request('deleteUser',
71     'username'  => $svc_acct->username
72   );
73 }
74
75 sub _export_suspend {
76   my( $self, $svc_acct ) = (shift, shift);
77   $self->google_request('updateUser',
78     'username'  => $svc_acct->username,
79     'suspended' => 'true',
80   );
81 }
82
83 sub _export_unsuspend {
84   my( $self, $svc_acct ) = (shift, shift);
85   $self->google_request('updateUser',
86     'username'  => $svc_acct->username,
87     'suspended' => 'false',
88   );
89 }
90
91
92 my %google_error = (
93   1000 => 'unknown error',
94   1001 => 'server busy',
95   1100 => 'username belongs to a recently deleted account',
96   1101 => 'user suspended',
97   1200 => 'domain user limit exceeded',
98   1201 => 'domain alias limit exceeded',
99   1202 => 'domain suspended',
100   1203 => 'feature not available on this domain',
101   1300 => 'username in use',
102   1301 => 'user not found',
103   1302 => 'reserved username',
104   1400 => 'illegal character in first name',
105   1401 => 'illegal character in last name',
106   1402 => 'invalid password',
107   1403 => 'illegal character in username',
108   # should be everything we need
109 );
110
111 # Runs the request and returns nothing if it succeeds, or an 
112 # error message.
113
114 sub google_request {
115   my ($self, $method, %opt) = @_;
116   my $google = $self->google_handle;
117   return $google->{'error'} if $google->{'error'};
118
119   # Throw away the result from this; we don't use it yet.
120   eval { $google->$method(%opt) };
121   if ( $@ ) {
122     return $google_error{ $@->{'error'}->{'errorCode'} } || $@->{'error'};
123   }
124   return;
125 }
126
127 # Returns a REST::Google::Apps::Provisioning object which is hooked 
128 # to die {error => stuff} on API errors.  The cached auth token 
129 # will be used if possible.  If not, try to authenticate.  On 
130 # authentication error, the R:G:A:P object will still be returned 
131 # but with $google->{'error'} set to the error message.
132
133 sub google_handle {
134   my $self = shift;
135   my $class = 'REST::Google::Apps::Provisioning';
136   eval "use $class";
137
138   die "failed to load $class\n" if $@;
139   $CACHE ||= new Cache::FileCache( {
140       'namespace'   => __PACKAGE__,
141       'cache_root'  => "$FS::UID::cache_dir/cache.$FS::UID::datasrc",
142   } );
143   my $google = $class->new( 'domain'  => $self->option('domain') );
144
145   # REST::Google::Apps::Provisioning lacks error reporting.  We deal 
146   # with that by hooking HTTP::Response to throw a useful fatal error 
147   # on failure.
148   $google->{'lwp'}->add_handler( 'response_done' =>
149     sub {
150       my $response = shift;
151       return if $response->is_success;
152
153       my $error = '';
154       if ( $response->content =~ /^</ ) {
155         #presume xml
156         $error = $google->{'xml'}->parse_string($response->content);
157       }
158       elsif ( $response->content =~ /=/ ) {
159         $error = +{ map { if ( /^(\w+)=(.*)$/ ) { lc($1) => $2 } }
160           split("\n", $response->content)
161         };
162       }
163       else { # have something to say if there is no response...
164         $error = {'error' => $response->status_line};
165       }
166       die $error;
167     }
168   );
169
170   my $cache_id = $self->exportnum . '_token';
171   $google->{'token'} = $CACHE->get($cache_id);
172   if ( !$google->{'token'} ) {
173     eval { 
174       $google->authenticate(
175         'username'  => $self->option('username'),
176         'password'  => $self->option('password'),
177       ) 
178     };
179     if ( $@ ) {
180       # XXX CAPTCHA
181       $google->{'error'} = $@->{'error'};
182       $CACHE->remove($cache_id);
183       return $google;
184     }
185     $CACHE->set($cache_id, $google->{'token'}, '1 hour');
186   }
187   return $google;
188 }
189
190 1;