diff --git a/Dockerfile b/Dockerfile index f8045763..3c725cad 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM python:3.11-alpine as builder RUN apk --update add bash nano g++ -COPY . /vampi +COPY ./requirements.txt /vampi/requirements.txt WORKDIR /vampi RUN pip install -r requirements.txt diff --git a/README.md b/README.md index 2b05f508..45ae0473 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ A quick rundown of the actions included can be seen in the following table: |:----------:|:-----------------------------:|:--------------------------------------------------:| | GET | /createdb | Creates and populates the database with dummy data | | GET | / | VAmPI home | +| GET | /me | Displays the user that is logged in | | GET | /users/v1 | Displays all users with basic information | | GET | /users/v1/_debug | Displays all details for all users | | POST | /users/v1/register | Register new user | diff --git a/api_views/users.py b/api_views/users.py index f788701b..172540ac 100644 --- a/api_views/users.py +++ b/api_views/users.py @@ -25,6 +25,22 @@ def debug(): return_value = jsonify({'users': User.get_all_users_debug()}) return return_value +def me(): + resp = token_validator(request.headers.get('Authorization')) + if "error" in resp: + return Response(error_message_helper(resp), 401, mimetype="application/json") + else: + user = User.query.filter_by(username=resp['sub']).first() + responseObject = { + 'status': 'success', + 'data': { + 'username': user.username, + 'email': user.email, + 'admin': user.admin + } + } + return Response(json.dumps(responseObject), 200, mimetype="application/json") + def get_by_username(username): if User.get_user(username): diff --git a/openapi_specs/VAmPI.postman_collection.json b/openapi_specs/VAmPI.postman_collection.json index e9e79c8a..e4a2a1a3 100644 --- a/openapi_specs/VAmPI.postman_collection.json +++ b/openapi_specs/VAmPI.postman_collection.json @@ -1,9 +1,10 @@ { "info": { - "_postman_id": "07b56784-02d4-47a0-9bcd-03b5bc52f8dd", + "_postman_id": "2b4774dd-b3fb-4a63-81f4-353643bbb641", "name": "VAmPI", "description": "OpenAPI v3 specs for VAmPI", - "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "10538030" }, "item": [ { @@ -151,10 +152,10 @@ ] } }, - "_postman_previewlanguage": null, - "header": null, + "_postman_previewlanguage": "Text", + "header": [], "cookie": [], - "body": null + "body": "" } ] }, @@ -359,8 +360,8 @@ ] } }, - "_postman_previewlanguage": null, - "header": null, + "_postman_previewlanguage": "Text", + "header": [], "cookie": [], "body": "{\n \"message\": \"Successfully registered. Login to receive an auth token.\",\n \"status\": \"success\"\n}" } @@ -459,8 +460,8 @@ }, "status": "OK", "code": 200, - "_postman_previewlanguage": null, - "header": null, + "_postman_previewlanguage": "Text", + "header": [], "cookie": [], "body": "{\n \"status\": \"fail\",\n \"message\": \"Password is not correct for the given username.\"\n}" }, @@ -501,13 +502,60 @@ }, "status": "OK", "code": 200, - "_postman_previewlanguage": null, - "header": null, + "_postman_previewlanguage": "Text", + "header": [], "cookie": [], "body": "{\n \"status\": \"fail\",\n \"message\": \"Username does not exist\"\n}" } ] }, + { + "name": "Retrieves currently logged in user", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});" + ], + "type": "text/javascript", + "packages": {} + } + } + ], + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{auth_token}}", + "type": "string" + } + ] + }, + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json" + } + ], + "url": { + "raw": "{{baseUrl}}/me", + "host": [ + "{{baseUrl}}" + ], + "path": [ + "me" + ] + }, + "description": "Displays user by username" + }, + "response": [] + }, { "name": "Add new book", "event": [ @@ -744,10 +792,10 @@ ] } }, - "_postman_previewlanguage": null, - "header": null, + "_postman_previewlanguage": "Text", + "header": [], "cookie": [], - "body": null + "body": "" } ] }, @@ -862,10 +910,10 @@ ] } }, - "_postman_previewlanguage": null, - "header": null, + "_postman_previewlanguage": "Text", + "header": [], "cookie": [], - "body": null + "body": "" } ] }, diff --git a/openapi_specs/openapi3.yml b/openapi_specs/openapi3.yml index 34737404..744de6dc 100644 --- a/openapi_specs/openapi3.yml +++ b/openapi_specs/openapi3.yml @@ -212,6 +212,54 @@ paths: message: type: string example: 'Password is not correct for the given username.' + /me: + get: + security: + - bearerAuth: [] + tags: + - users + summary: Retrieves currently logged in user + description: Displays information about the currently authenticated user + operationId: api_views.users.me + responses: + '200': + description: Display current user info + content: + application/json: + schema: + type: object + properties: + data: + type: object + properties: + admin: + type: boolean + example: false + email: + type: string + example: 'mail1@mail.com' + username: + type: string + example: 'name1' + status: + type: string + example: 'success' + '401': + description: Unauthorized access due to expired, invalid, or missing token + content: + application/json: + schema: + type: object + properties: + status: + type: string + example: 'fail' + message: + type: string + enum: + - 'Signature expired. Please log in again.' + - 'Invalid token. Please log in again.' + /users/v1/{username}: get: tags: