Slide 29
Slide 29 text
use Crypt::JWT qw(encode_jwt decode_jwt);
$router->add("/authorize", sub {
my ($req, $env) = @_;
my $id = $req->body_parameters->{login_id};
my $pass = $req->body_parameters->{password};
## DBଓͯ͠ೝূ
my $user = $db->query('SELECT * FROM users WHERE login_id = ? AND password = ?', $id, $pass)->hash;
unless ($user) {
die;
}
# JSON TokenԽ͢ΔσʔλߏΛ࡞
my $payload = {
iss => ‘υϝΠϯ’, # ൃߦݩURL
sub => $user->{app_id}, # Ϣʔβࣝผࢠ / ΞϓϦID
iat => time, # τʔΫϯੜ࣌ࠁ
exp => time + (60 * 60), # ༗ޮظݶ
};
# ൿີ伴ͰJSONΛॺ໊
my $jws_token = encode_jwt(payload => $payload, alg => 'HS256', key => 'ൿີʂ');
return ["200", ["Content-Type" => "application/json"], [encode_json({token => $jws_token})]];
});
+85ʹΑΔೝূαʔό
ηΩϡϦςΟೝূ