Skip to content

feat: add CLI flag and environment variable to enable legacy API tokens #7388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/source/guide/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ The following command line arguments are optional and must be specified with `la
| `--initial-project-description` | `LABEL_STUDIO_PROJECT_DESC` | `''` | Specify a project description for a Label Studio project. See [Set up your labeling project](setup.html). |
| `--password` | `LABEL_STUDIO_PASSWORD` | `None` | Password to use for the default user. See [Set up user accounts](signup.html). |
| `--username` | `LABEL_STUDIO_USERNAME` | `default_user@localhost` | Username to use for the default user. See [Set up user accounts](signup.html). |
| `--user-token` | `LABEL_STUDIO_USER_TOKEN` | Automatically generated. | Authentication token for a user to use for the API. Must be set with a username, otherwise automatically generated. See [Set up user accounts](signup.html).
| `--user-token` | `LABEL_STUDIO_USER_TOKEN` | Automatically generated. | Authentication token for a user to use for the API. Must be set with a username, otherwise automatically generated. See [Set up user accounts](signup.html). |
| `--agree-fix-sqlite` | N/A | `False` | Automatically agree to let Label Studio fix SQLite issues when using Python 3.6–3.8 on Windows operating systems. |
| `--enable-legacy-api-token` | `LABEL_STUDIO_ENABLE_LEGACY_API_TOKEN` | `False` | Enable legacy API token authentication. Useful for running with a pre-existing token via `--user-token`. |
| N/A | `LABEL_STUDIO_LOCAL_FILES_SERVING_ENABLED` | `False` | Allow Label Studio to access local file directories to import storage. See [Run Label Studio on Docker and use local storage](start.html#Run_Label_Studio_on_Docker_and_use_local_storage). |
| N/A | `LABEL_STUDIO_LOCAL_FILES_DOCUMENT_ROOT` | `/` | Specify the root directory for Label Studio to use when accessing local file directories. See [Run Label Studio on Docker and use local storage](start.html#Run_Label_Studio_on_Docker_and_use_local_storage). |

Expand Down
6 changes: 6 additions & 0 deletions label_studio/core/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def project_name(raw_name):
action='store_true',
help='Agree to fix SQLite issues on python 3.6-3.8 on Windows automatically',
)
root_parser.add_argument(
'--enable-legacy-api-token',
dest='enable_legacy_api_token',
action='store_true',
help='Enable legacy API token authentication',
)

parser = argparse.ArgumentParser(description='Label studio', parents=[root_parser])

Expand Down
2 changes: 2 additions & 0 deletions label_studio/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,8 @@ def collect_versions_dummy(**kwargs):

LOGOUT_REDIRECT_URL = get_env('LOGOUT_REDIRECT_URL', None)

# Enable legacy tokens (useful for running with a pre-existing token via `LABEL_STUDIO_USER_TOKEN`)
LABEL_STUDIO_ENABLE_LEGACY_API_TOKEN = get_bool_env('LABEL_STUDIO_ENABLE_LEGACY_API_TOKEN', False)
RESOLVER_PROXY_BUFFER_SIZE = int(get_env('RESOLVER_PROXY_BUFFER_SIZE', 512 * 1024))
RESOLVER_PROXY_TIMEOUT = int(get_env('RESOLVER_PROXY_TIMEOUT', 20))
RESOLVER_PROXY_MAX_RANGE_SIZE = int(get_env('RESOLVER_PROXY_MAX_RANGE_SIZE', 8 * 1024 * 1024))
Expand Down
9 changes: 6 additions & 3 deletions label_studio/organizations/functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from core.utils.common import temporary_disconnect_all_signals
from django.conf import settings
from django.db import transaction
from organizations.models import Organization, OrganizationMember
from projects.models import Project


def create_organization(title, created_by, **kwargs):
def create_organization(title, created_by, legacy_api_tokens_enabled=False, **kwargs):
from core.feature_flags import flag_set

JWT_ACCESS_TOKEN_ENABLED = flag_set('fflag__feature_develop__prompts__dia_1829_jwt_token_auth')
Expand All @@ -13,9 +14,11 @@ def create_organization(title, created_by, **kwargs):
org = Organization.objects.create(title=title, created_by=created_by, **kwargs)
OrganizationMember.objects.create(user=created_by, organization=org)
if JWT_ACCESS_TOKEN_ENABLED:
# set auth tokens to new system for new users
# set auth tokens to new system for new users, unless specified otherwise
org.jwt.api_tokens_enabled = True
org.jwt.legacy_api_tokens_enabled = False
org.jwt.legacy_api_tokens_enabled = (
legacy_api_tokens_enabled or settings.LABEL_STUDIO_ENABLE_LEGACY_API_TOKEN
)
org.jwt.save()
return org

Expand Down
4 changes: 3 additions & 1 deletion label_studio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ def _create_user(input_args, config):
user = User.objects.get(email=username)
org = Organization.objects.first()
if not org:
org = Organization.create_organization(created_by=user, title='Label Studio')
org = Organization.create_organization(
created_by=user, title='Label Studio', legacy_api_tokens_enabled=input_args.enable_legacy_api_token
)
else:
org.add_user(user)
user.active_organization = org
Expand Down
Loading