Le mirroring des dépôts vers Github est maintenant natif dans Gitlab (et donc Framagit). Merci de passer à la fonction native pour que nous puissions couper notre solution maison. Détails sur https://docs.gitlab.com/ee/workflow/repository_mirroring.html#pushing-to-a-remote-repository (pensez à supprimer le webhook de notre solution maison).

Commit 70cf6b26 by Luc Didry

Update API documentation. Fix #20

+ add JSON response to some point of API
parent 57c2175d
Pipeline #11216 passed with stages
in 5 minutes 37 seconds
......@@ -19,6 +19,10 @@ podcheck:
test: podcheck
$(CARTON) $(REAL_LSTU) test
test-sqlite:
MOJO_CONFIG=t/sqlite1.conf $(CARTON) $(REAL_LSTU) test
MOJO_CONFIG=t/sqlite2.conf $(CARTON) $(REAL_LSTU) test
dev:
$(CARTON) morbo $(LSTU) --listen http://0.0.0.0:3000 --watch lib/ --watch script/ --watch themes/ --watch lstu.conf
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lstu;
use Mojo::Base 'Mojolicious';
use Mojo::JSON;
use Net::LDAP;
use Apache::Htpasswd;
use Lstu::DB::URL;
......@@ -227,10 +228,37 @@ sub startup {
my $pwd = $c->param('password');
if($c->authenticate($login, $pwd)) {
$c->redirect_to('index');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged in.')
}
);
},
any => sub {
$c->redirect_to('index');
}
);
} else {
$c->stash(msg => $c->l('Please, check your credentials: unable to authenticate.'));
$c->render(template => 'login');
my $msg = $c->l('Please, check your credentials: unable to authenticate.');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->false,
msg => $msg
}
);
},
any => sub {
$c->stash(msg => $msg);
$c->render(template => 'login')
}
);
}
});
# Logout page
......@@ -239,7 +267,20 @@ sub startup {
if ($c->is_user_authenticated) {
$c->logout;
}
$c->render(template => 'logout');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged out.')
}
);
},
any => sub {
$c->render(template => 'logout');
}
);
})->name('logout');
}
......
......@@ -43,22 +43,64 @@ sub login {
)->write;
$c->session('token' => $token);
$c->redirect_to('stats');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged in.')
}
);
},
any => sub {
my $c = shift;
$c->redirect_to('stats');
}
);
} elsif (defined($act) && $act eq 'logout') {
Lstu::DB::Session->new(
app => $c,
token => $c->session('token')
)->delete;
delete $c->session->{token};
$c->redirect_to('stats');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged out.')
}
);
},
any => sub {
shift->redirect_to('stats');
}
);
} else {
Lstu::DB::Ban->new(
app => $c,
ip => $ip
)->increment_ban_delay(3600);
$c->flash('msg' => $c->l('Bad password'));
$c->redirect_to('stats');
my $msg = $c->l('Bad password');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->false,
msg => $msg
}
);
},
any => sub {
my $c = shift;
$c->flash('msg' => $msg);
$c->redirect_to('stats');
}
);
}
}
}
......@@ -97,7 +139,7 @@ sub delete {
);
}
} else {
$c->flash('msg' => $c->l('Bad password'));
$c->flash('msg' => $c->l('You\'re not authenticated as the admin'));
$c->redirect_to('stats');
}
}
......
......@@ -79,7 +79,7 @@ sub _provisioning {
for (my $i = 0; $i < $c->config('provis_step'); $i++) {
my $short;
do {
$short= $c->shortener($c->config('length'));
$short = $c->shortener($c->config('length'));
} while ($db_url->exist($short));
$db_url->short($short)->write;
......
......@@ -86,7 +86,7 @@ $b =~ s#http://127\.0\.0\.1:\d+/##;
$t->ua->max_redirects(1);
$t->get_ok('/d/'.$b)
->status_is(200)
->content_like(qr/Bad password/);
->content_like(qr/You&#39;re not authenticated as the admin/);
$t->post_ok('/stats' => form => { adminpwd => 'toto', page => 0 })
->status_is(200)
......
% # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab:
% title 'Lstu API';
% use Mojo::JSON qw(to_json);
%= stylesheet begin
dd > strong {
padding-left: 15px;
......@@ -7,22 +8,58 @@
% end
<h2>Lstu API</h2>
<dl>
<dt>POST /a</dt>
% if (defined $self->config('ldap') || defined $self->config('htpasswd')) {
<dt>POST <%= url_for('login') %></dt>
<dd>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>login</strong> <em><%= l('mandatory') %></em><br><%= l('Your login') %></li>
<li><strong>password</strong> <em><%= l('mandatory') %></em><br><%= l('Your password') %></li>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged in.')%>","success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Please, check your credentials: unable to authenticate.') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to the Lstu classic interface where you will be able to shorten URLs') %></li>
</ul>
</dd>
<dt>GET <%= url_for('logout') %></dt>
<dd>
<p><%= l('Should always be successful') %></p>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged out.')%>","success":true}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to Lstu successfully logged out interface') %></li>
</ul>
</dd>
% }
<dt>POST <%= url_for('add') %></dt>
<dd>
% if (defined $self->config('ldap') || defined $self->config('htpasswd')) {
<p><%= l('You must be logged in to use it.') %></p>
% }
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>lsturl</strong> <em><%= l('mandatory') %></em><br><%= l('URL to shorten') %></li>
<li><strong>lsturl-custom</strong> <em><%= l('optional') %></em><br><%= l('Custom shortened text')%></li>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"url":"https:\/\/fiat-tux.fr","short":"https:\/\/lstu.fr\/XzQBd6eF","success":true}</code></li>
% my $u = prefix().url_for('short', short => 'XzQBd6eFa');
% $u =~ s#//#/#g;
% $u = to_json $u;
<li><strong><%= l('JSON: success') %></strong><br><code>{"url":"https:\/\/fiat-tux.fr","short":<%= $u %>,"success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Failure reason') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to the Lstu interface with a message giving the shortened URL') %></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to the Lstu classic interface with a message giving the shortened URL') %></li>
</ul>
</dd>
<dt>GET /<%= l('shortened_url') %></dt>
<dt>GET <%= url_for('short', short => l('shortened_url')) %></dt>
<dd>
<strong><%= l('Response:') %></strong>
<ul>
......@@ -31,7 +68,7 @@
</li>
</ul>
</dd>
<dt>GET /<%= l('shortened_url') %>.json</dt>
<dt>GET <%= url_for('short', short => l('shortened_url')) %>.json</dt>
<dd>
<strong><%= l('Response:') %></strong>
<ul>
......@@ -39,4 +76,75 @@
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Failure reason') %>","success":false}</code></li>
</li>
</dd>
<dt>GET <%= url_for('stats') %></dt>
<dd>
% if (defined $self->config('ldap') || defined $self->config('htpasswd')) {
<p><%= l('You must be logged in to use it.') %></p>
% }
<p><%= l('It will use Lstu\'s cookies to know which shortened URL it will return statistics for.') %></p>
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
<p><%= l('If you are logged in as admin ("adminpwd" or "hashed_adminpwd" setting), it will give you statistics for all URLs, sorted by the most visited first, paginated with pages containing %1 records.', config('page_offset')) %></p>
% }
<strong><%= l('Parameters:') %></strong>
<ul>
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
<li><strong>page</strong> <em><%= l('optional') %></em><br><%= l('If you are logged in as admin, you can provide a "page" parameter') %>
% }
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong><br>
<ul>
<li>
<ul>
% $u = prefix().url_for('short', short => l('shortened_url'));
% $u =~ s#//#/#g;
% $u = to_json $u;
<li><strong><%= l('Response for JSON format') %></strong><br><code>[{"short":<%= $u %>,"url":"<%= l('URL') %>","counter":<%= l('integer, how many unique visits of the shortened URL') %>}]</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('A page with a table containing the same informations that the JSON response') %></li>
</ul>
</li>
</ul>
</dd>
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
<dt>POST <%= url_for('stats') %></dt>
<dd>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>adminpwd</strong></li> <em><%= l('optional') %></em><br><%= l('To do an admin login, set it to the password defined in the settings ("adminpwd" or "hashed_adminpwd")') %></li>
<li><strong>action</strong></li> <em><%= l('optional') %></em><br><%= l('To do an admin logout, set it to "logout".') %></li>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><%= l('If "adminpwd" is defined:') %>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged in.')%>","success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Bad password') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will have the statistics page with the admin stats if you\'re succesfully logged in, or your stats and a failure message otherwise') %></li>
</ul>
</li>
<li><%= l('If "action" is defined to "logout":') %><br>
<%= l('Should always be successful') %></p>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged out.')%>","success":true}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to Lstu statistics page') %></li>
</ul>
</li>
</ul>
</dd>
<dt>GET <%= url_for('delete', short => l('shortened_url')) %></dt>
<dd>
<p><%= l('You must be logged in as admin ("adminpwd" or "hashed_adminpwd" setting) to use it.') %></p>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"url":"https:\/\/fiat-tux.fr","short":<%= $u %>,"success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Failure reason') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to Lstu statistics page with a message in case of failure') %></li>
</ul>
</dd>
% }
</dl>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment