Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #822 from joyent/ether/command-token
Browse files Browse the repository at this point in the history
command-line utility to create an api token
  • Loading branch information
karenetheridge authored Jul 2, 2019
2 parents 28af261 + 4516695 commit 23a84e6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/modules/Conch::Command::create_token.md
Original file line number Diff line number Diff line change
@@ -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/).
1 change: 1 addition & 0 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 77 additions & 0 deletions lib/Conch/Command/create_token.pm
Original file line number Diff line number Diff line change
@@ -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<http://mozilla.org/MPL/2.0/>.
=cut
# vim: set ts=4 sts=4 sw=4 et :

0 comments on commit 23a84e6

Please sign in to comment.