-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] cli: add a command to generate a proxy token
This commit adds a `genproxytoken` command to generate and set an access token for the proxy mode in the config file. X-original-commit: 41a7224 Part-of: odoo#92195
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
from . import populate | ||
from . import tsconfig | ||
from . import neutralize | ||
from . import genproxytoken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import argparse | ||
import os | ||
import secrets | ||
import sys | ||
import textwrap | ||
|
||
from passlib.hash import pbkdf2_sha512 | ||
|
||
from . import Command | ||
from odoo.tools import config | ||
|
||
|
||
class GenProxyToken(Command): | ||
def __init__(self): | ||
self.command_name = "genproxytoken" | ||
|
||
def generate_token(self, length=16): | ||
token = secrets.token_hex(int(length / 2)) | ||
split_size = int(length / 4) | ||
return '-'.join(textwrap.wrap(token, split_size)) | ||
|
||
def run(self, cmdargs): | ||
parser = argparse.ArgumentParser( | ||
prog="%s %s" % (sys.argv[0].split(os.path.sep)[-1], self.command_name), | ||
description="Generate and (re)set proxy access token in config file" | ||
) | ||
parser.add_argument('-c', '--config', type=str, help="Specify an alternate config file") | ||
parser.add_argument('--token-length', type=int, help="Token Length", default=16) | ||
args, _ = parser.parse_known_args() | ||
if args.config: | ||
config.rcfile = args.config | ||
token = self.generate_token(length=args.token_length) | ||
config['proxy_access_token'] = pbkdf2_sha512.hash(token) | ||
config.save() | ||
sys.stdout.write(f'{token}\n') |