-
Notifications
You must be signed in to change notification settings - Fork 455
Share user sessions through the database #1172
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
30 changes: 30 additions & 0 deletions
30
config_db_migrate/versions/150800b30447_share_sessions_through_the_database.py
This file contains hidden or 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,30 @@ | ||
| """Share sessions through the database | ||
|
|
||
| Revision ID: 150800b30447 | ||
| Revises: 8268fc7ca7f4 | ||
| Create Date: 2017-11-23 15:26:45.594141 | ||
|
|
||
| """ | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = '150800b30447' | ||
| down_revision = '8268fc7ca7f4' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| def upgrade(): | ||
| op.create_table('sessions', | ||
| sa.Column('auth_string', sa.CHAR(64), nullable=False), | ||
| sa.Column('token', sa.CHAR(32), nullable=False), | ||
| sa.Column('last_access', sa.DateTime(), nullable=False), | ||
| sa.PrimaryKeyConstraint('auth_string', | ||
| name=op.f('pk_sessions')) | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_table('sessions') |
Empty file.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,92 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # The CodeChecker Infrastructure | ||
| # This file is distributed under the University of Illinois Open Source | ||
| # License. See LICENSE.TXT for details. | ||
| # ------------------------------------------------------------------------- | ||
| """ | ||
| Handles the management of stored user credentials and currently known session | ||
| tokens. | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| import stat | ||
|
|
||
| import portalocker | ||
|
|
||
| from libcodechecker.logger import get_logger | ||
| from libcodechecker.util import check_file_owner_rw | ||
| from libcodechecker.util import load_json_or_empty | ||
| from libcodechecker.version import SESSION_COOKIE_NAME as _SCN | ||
|
|
||
| LOG = get_logger('system') | ||
| SESSION_COOKIE_NAME = _SCN | ||
|
|
||
|
|
||
| class UserCredentials: | ||
|
|
||
| def __init__(self): | ||
| LOG.debug("Loading clientside session config.") | ||
|
|
||
| # Check whether user's configuration exists. | ||
| user_home = os.path.expanduser("~") | ||
| session_cfg_file = os.path.join(user_home, | ||
| ".codechecker.passwords.json") | ||
| LOG.debug(session_cfg_file) | ||
|
|
||
| scfg_dict = load_json_or_empty(session_cfg_file, {}, | ||
| "user authentication") | ||
| if os.path.exists(session_cfg_file): | ||
| check_file_owner_rw(session_cfg_file) | ||
|
|
||
| if not scfg_dict.get('credentials'): | ||
| scfg_dict['credentials'] = {} | ||
|
|
||
| self.__save = scfg_dict | ||
| self.__autologin = scfg_dict.get('client_autologin', True) | ||
|
|
||
| # Check and load token storage for user. | ||
| self.token_file = os.path.join(user_home, ".codechecker.session.json") | ||
| LOG.debug(self.token_file) | ||
|
|
||
| if os.path.exists(self.token_file): | ||
| token_dict = load_json_or_empty(self.token_file, {}, | ||
| "user authentication") | ||
| check_file_owner_rw(self.token_file) | ||
|
|
||
| self.__tokens = token_dict.get('tokens') | ||
| else: | ||
| with open(self.token_file, 'w') as f: | ||
| json.dump({'tokens': {}}, f) | ||
| os.chmod(self.token_file, stat.S_IRUSR | stat.S_IWUSR) | ||
|
|
||
| self.__tokens = {} | ||
|
|
||
| def is_autologin_enabled(self): | ||
| return self.__autologin | ||
|
|
||
| def get_token(self, host, port): | ||
| return self.__tokens.get("{0}:{1}".format(host, port)) | ||
|
|
||
| def get_auth_string(self, host, port): | ||
| ret = self.__save['credentials'].get('{0}:{1}'.format(host, port)) | ||
| if not ret: | ||
| ret = self.__save['credentials'].get(host) | ||
| if not ret: | ||
| ret = self.__save['credentials'].get('*:{0}'.format(port)) | ||
| if not ret: | ||
| ret = self.__save['credentials'].get('*') | ||
|
|
||
| return ret | ||
|
|
||
| def save_token(self, host, port, token, destroy=False): | ||
| if destroy: | ||
| del self.__tokens['{0}:{1}'.format(host, port)] | ||
| else: | ||
| self.__tokens['{0}:{1}'.format(host, port)] = token | ||
|
|
||
| with open(self.token_file, 'w') as scfg: | ||
| portalocker.lock(scfg, portalocker.LOCK_EX) | ||
| json.dump({'tokens': self.__tokens}, scfg, | ||
| indent=2, sort_keys=True) | ||
| portalocker.unlock(scfg) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You use this line of code in multiple places. Can we create a helper function for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there exists a good place where this helper function could be put.