From 4516695491c7c4e53b8b2ff664a8f1298bcb8d8d Mon Sep 17 00:00:00 2001 From: Karen Etheridge Date: Mon, 24 Jun 2019 09:59:53 -0700 Subject: [PATCH] command-line utility to create an api token closes #821. --- docs/modules/Conch::Command::create_token.md | 21 ++++++ docs/modules/index.md | 1 + lib/Conch/Command/create_token.pm | 77 ++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 docs/modules/Conch::Command::create_token.md create mode 100644 lib/Conch/Command/create_token.pm diff --git a/docs/modules/Conch::Command::create_token.md b/docs/modules/Conch::Command::create_token.md new file mode 100644 index 000000000..7347de5f8 --- /dev/null +++ b/docs/modules/Conch::Command::create_token.md @@ -0,0 +1,21 @@ +# NAME + +create\_token - create a new api token + +# SYNOPSIS + +```perl +bin/conch create_token [long options...] + + --name required; the name to give the token + --email required; the user account for which to create the token + --help print usage message and exit +``` + +# LICENSING + +Copyright Joyent, Inc. + +This Source Code Form is subject to the terms of the Mozilla Public License, +v.2.0. If a copy of the MPL was not distributed with this file, You can obtain +one at [http://mozilla.org/MPL/2.0/](http://mozilla.org/MPL/2.0/). diff --git a/docs/modules/index.md b/docs/modules/index.md index 312a49824..9ac3c6939 100644 --- a/docs/modules/index.md +++ b/docs/modules/index.md @@ -4,6 +4,7 @@ * [Conch::Command::check_layouts](../modules/Conch::Command::check_layouts) * [Conch::Command::check_validation_plans](../modules/Conch::Command::check_validation_plans) * [Conch::Command::clean_permissions](../modules/Conch::Command::clean_permissions) +* [Conch::Command::create_token](../modules/Conch::Command::create_token) * [Conch::Command::create_user](../modules/Conch::Command::create_user) * [Conch::Command::thin_device_reports](../modules/Conch::Command::thin_device_reports) * [Conch::Command::update_validation_plans](../modules/Conch::Command::update_validation_plans) diff --git a/lib/Conch/Command/create_token.pm b/lib/Conch/Command/create_token.pm new file mode 100644 index 000000000..c58e18a8e --- /dev/null +++ b/lib/Conch/Command/create_token.pm @@ -0,0 +1,77 @@ +package Conch::Command::create_token; + +=pod + +=head1 NAME + +create_token - create a new api token + +=head1 SYNOPSIS + + bin/conch create_token [long options...] + + --name required; the name to give the token + --email required; the user account for which to create the token + --help print usage message and exit + +=cut + +use Mojo::Base 'Mojolicious::Command', -signatures; +use Getopt::Long::Descriptive; + +has description => 'Create a new application token'; + +has usage => sub { shift->extract_usage }; # extracts from SYNOPSIS + +use Session::Token; +use Mojo::JWT; + +sub run ($self, @opts) { + local @ARGV = @opts; + my ($opt, $usage) = describe_options( + # the descriptions aren't actually used anymore (mojo uses the synopsis instead)... but + # the 'usage' text block can be accessed with $usage->text + 'create_token %o', + [ 'name|n=s', 'the name to give the token', { required => 1 } ], + [ 'email|e=s', 'the user account for which to create the token', { required => 1 } ], + [], + [ 'help', 'print usage message and exit', { shortcircuit => 1 } ], + ); + + my $user = $self->app->db_user_accounts->active->lookup_by_email($opt->email); + die 'cannot find user with email ', $opt->email if not $user; + + # NOTE: all this code will change very soon with the user_session_token refactor + my $token = Session::Token->new->get; + my $expires_abs = time + (($self->app->config('jwt') || {})->{custom_token_expiry} // 86400*365*5); + my $row = $self->app->db_user_session_tokens->create({ + user_id => $user->id, + name => $opt->name, + token_hash => \[ q{digest(?, 'sha256')}, $token ], + expires => \[ q{to_timestamp(?)::timestamptz}, $expires_abs ], + }); + + my $jwt = Mojo::JWT->new( + claims => { uid => $user->id, jti => $token }, + secret => $self->app->config('secrets')->[0], + expires => $expires_abs, + )->encode; + + say $jwt; +} + +1; +__END__ + +=pod + +=head1 LICENSING + +Copyright Joyent, Inc. + +This Source Code Form is subject to the terms of the Mozilla Public License, +v.2.0. If a copy of the MPL was not distributed with this file, You can obtain +one at L. + +=cut +# vim: set ts=4 sts=4 sw=4 et :