Skip to content

Commit 7293c37

Browse files
committed
Add Rake task for inserting pre-generated access token into database
This task allows inserting a token into the access tokens table without already having an API token. To use this, generate a UUID or other random string, and then pass the hex encoded sha256 digest of that to the `insert` task: ```bash TOKEN="$(ruby -e "require 'securerandom'; puts SecureRandom.uuid")" TOKEN_DIGEST="$(ruby -e "require 'digest'; puts Digest::SHA256.hexdigest('$TOKEN')")" bin/rails access_tokens:insert[$TOKEN_DIGEST] ``` Then don't forget to save the token somewhere safe!
1 parent 26cdfee commit 7293c37

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

lib/tasks/access_tokens.rake

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace :access_tokens do
2+
desc "Add a pre-generated token, using the SHA256 hex digest"
3+
task :insert, %i[token_digest] => :environment do |_, args|
4+
usage_message = "usage: rails access_tokens:insert[<token_digest>]".freeze
5+
abort usage_message unless args[:token_digest]&.match?(/^[a-f0-9]+$/)
6+
7+
access_token = AccessToken.create!(
8+
description: "inserted with `rails access_tokens:insert`",
9+
owner: ENV["USER"],
10+
token_digest: args[:token_digest],
11+
)
12+
pp(access_token)
13+
end
14+
end

0 commit comments

Comments
 (0)