-
Notifications
You must be signed in to change notification settings - Fork 5
/
proxy-auth.cgi
executable file
·160 lines (118 loc) · 3.57 KB
/
proxy-auth.cgi
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env perl
use strict;
our $VERSION = "0.01";
use constant LDAP_SERVER => "ldap.db.scl3.mozilla.com";
use constant BIND_USER => "uid=bind-labs,ou=logins,dc=mozilla";
use constant BIND_PASSWORD => "XXX";
#XXX: This needs to be dynamic/renewed
use constant ADMIN_TOKEN => "XXX";
use constant ADMIN_USER => "vcap-proxy-auth";
use constant ADMIN_PASSWORD => "sekrit";
use CGI;
use CGI::Carp;
use URI::Escape::XS qw(uri_unescape);
use Data::Dumper;
use JSON;
use Net::LDAP;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(
'agent' => "CF-Proxy-Auth/v$VERSION",
);
my $q = CGI->new();
my $data = decode_json($q->param('POSTDATA'));
my $url = uri_unescape($q->url(-absolute=>1));
( my $email = $url ) =~ s[/users/(.*)/tokens/?][$1];
my $password = $data->{password};
if (not login_ldap($email, $password)) {
error(401, "Failed LDAP login for $email");
}
if (not exist_cf_user($email)) {
warn "Need to create cloud foundry user for $email";
create_cf_user($email);
}
else { warn "Already known account"; }
get_cf_token($email);
sub error {
my ($status, $msg) = @_;
carp $msg;
print $q->header(
-type => 'text/plain',
-status => $status,
);
print $msg;
exit;
}
sub login_ldap {
my ($email, $password) = @_;
warn "Login attempt from $email/XXXXX";
my $ldap = Net::LDAP->new(LDAP_SERVER);
$ldap->bind(BIND_USER,
password => BIND_PASSWORD,
);
my $res = $ldap->search(
base => "dc=mozilla",
filter => "mail=$email",
);
$res->code && warn $res->error;
my $n = $res->count();
if ($n == 0) {
error(404, "User $email not found");
}
elsif ($n > 1) {
error(500, "Found multiple LDAP accounts for $email ?!?!");
}
my $entry = $res->entry(0);
my $dn = $entry->dn();
warn "Found dn='$dn' for $email";
my $login = $ldap->bind($dn,
password => $password);
my $code = $login->code;
if ($login->is_error) {
error(401, "Failed LDAP login " . $login->error);
}
warn "User $email logged into LDAP ok";
return 1;
}
sub exist_cf_user {
my $email = shift;
my $req = HTTP::Request->new(GET => "https://api.vcap.mozillalabs.com/users/$email");
$req->header("Authorization" => ADMIN_TOKEN);
my $res = $ua->request($req);
return $res->is_success;
}
sub create_cf_user {
my $email = shift;
my $req = HTTP::Request->new(POST => "https://api.vcap.mozillalabs.com/users");
$req->header("Authorization" => ADMIN_TOKEN);
$req->header("Content-type" => "application/json");
my $content = encode_json({
'email' => $email,
'password' => 'sekrit'
});
$req->content($content);
my $res = $ua->request($req);
if (!$res->is_success) {
error(401, "Cloudfoundry account creation failed " . $res->status_line);
}
return 1;
}
sub get_cf_token {
my $email = shift;
my $req = HTTP::Request->new(POST => "https://api.vcap.mozillalabs.com/users/$email/tokens");
$req->header("Authorization" => ADMIN_TOKEN);
$req->header("Content-type" => "application/json");
$req->content(encode_json({
'ssh_privkey' => '1'
}));
my $res = $ua->request($req);
if (!$res->is_success) {
error(401, "Cloudfoundry login failed " . $res->status_line);
}
my $content = $res->decoded_content;
my $content_type = $res->header("Content-type");
print $q->header(
-type => $content_type,
);
print $content;
return 1;
}