Skip to content

Commit

Permalink
auth: use invenio session cookie to retrieve user
Browse files Browse the repository at this point in the history
Addresses reanahub#153

Signed-off-by: Leticia Farias Wanderley <leticia.farias.wanderley@cern.ch>
  • Loading branch information
Leticia Farias Wanderley authored and leticiawanderley committed Jul 26, 2019
1 parent 683c4f7 commit 720ee5b
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 401 deletions.
2 changes: 1 addition & 1 deletion reana_server/rest/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from flask import Blueprint, jsonify

blueprint = Blueprint('ping', __name__)
blueprint = Blueprint('ping', __name__, url_prefix='/reana-api')


@blueprint.route('/ping', methods=['GET'])
Expand Down
36 changes: 25 additions & 11 deletions reana_server/rest/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@

from bravado.exception import HTTPError
from flask import Blueprint, jsonify, request
from flask_login import current_user, login_required

from reana_commons.errors import (REANASecretAlreadyExists,
REANASecretDoesNotExist)
from reana_commons.k8s.secrets import REANAUserSecretsStore
from reana_server.utils import get_user_from_token
from reana_server.utils import get_user_from_token, \
_get_user_from_invenio_user

blueprint = Blueprint('secrets', __name__)
blueprint = Blueprint('secrets', __name__, url_prefix='/reana-api')


@blueprint.route('/secrets/', methods=['POST'])
@login_required
def add_secrets(): # noqa
r"""Endpoint to create user secrets.
Expand All @@ -38,8 +41,8 @@ def add_secrets(): # noqa
parameters:
- name: access_token
in: query
description: Required. Secrets owner access token.
required: true
description: Secrets owner access token.
required: false
type: string
- name: overwrite
in: query
Expand Down Expand Up @@ -112,7 +115,10 @@ def add_secrets(): # noqa
}
"""
try:
user = get_user_from_token(request.args.get("access_token"))
if current_user.is_authenticated:
user = _get_user_from_invenio_user(current_user.email)
else:
user = get_user_from_token(request.args.get('access_token'))
secrets_store = REANAUserSecretsStore(str(user.id_))
overwrite = json.loads(request.args.get('overwrite'))
secrets_store.add_secrets(request.json, overwrite=overwrite)
Expand All @@ -127,6 +133,7 @@ def add_secrets(): # noqa


@blueprint.route('/secrets', methods=['GET'])
@login_required
def get_secrets(): # noqa
r"""Endpoint to retrieve user secrets.
Expand All @@ -141,8 +148,8 @@ def get_secrets(): # noqa
parameters:
- name: access_token
in: query
description: Required. Secrets owner access token.
required: true
description: Secrets owner access token.
required: false
type: string
responses:
200:
Expand Down Expand Up @@ -194,7 +201,10 @@ def get_secrets(): # noqa
}
"""
try:
user = get_user_from_token(request.args.get("access_token"))
if current_user.is_authenticated:
user = _get_user_from_invenio_user(current_user.email)
else:
user = get_user_from_token(request.args.get('access_token'))
secrets_store = REANAUserSecretsStore(str(user.id_))
user_secrets = secrets_store.get_secrets()
return jsonify(user_secrets), 200
Expand All @@ -206,6 +216,7 @@ def get_secrets(): # noqa


@blueprint.route('/secrets/', methods=['DELETE'])
@login_required
def delete_secrets(): # noqa
r"""Endpoint to delete user secrets.
Expand All @@ -220,8 +231,8 @@ def delete_secrets(): # noqa
parameters:
- name: access_token
in: query
description: Required. API key of the admin.
required: true
description: API key of the admin.
required: false
type: string
- name: secrets
in: body
Expand Down Expand Up @@ -283,7 +294,10 @@ def delete_secrets(): # noqa
}
"""
try:
user = get_user_from_token(request.args.get("access_token"))
if current_user.is_authenticated:
user = _get_user_from_invenio_user(current_user.email)
else:
user = get_user_from_token(request.args.get('access_token'))
secrets_store = REANAUserSecretsStore(str(user.id_))
deleted_secrets_list = secrets_store.delete_secrets(request.json)
return jsonify(deleted_secrets_list), 200
Expand Down
2 changes: 1 addition & 1 deletion reana_server/rest/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from reana_server.utils import _create_user, _get_users

blueprint = Blueprint('users', __name__)
blueprint = Blueprint('users', __name__, url_prefix='/reana-api')


@blueprint.route('/users', methods=['GET'])
Expand Down
Loading

0 comments on commit 720ee5b

Please sign in to comment.