A web API exposing a neural network to detect duplicate entities in knowledge graphs. It uses API key authentication and rate limits requests based on client tiers (FREEMIUM, PREMIUM)
Develop register and login endpoints within the FastAPI library. The register endpoint will allow new users to create an account, and the login endpoint will validate credentials. Upon successful login, the endpoint should return an API key, which the user will use for authenticating future requests to protected endpoints (e.g., /service). The task includes secure storage of user credentials and token generation.
User Stories
As a User, I want to register with my credentials so I can create an account to access the service.
As a User, I want to log in and receive a token so I can authenticate myself for using the service.
As a Developer, I want secure token-based authentication to manage user access to protected endpoints.
Details
Objective: Implement two endpoints (/register and /login) with authentication token generation. The token should be used for authorizing access to the predict-similarity endpoint.
Requirements:
Endpoint 1: POST /register
Accepts a JSON payload with username and password.
Hashes and stores the password securely (e.g., using bcrypt).
Returns a success message on successful registration.
Endpoint 2: POST /login
Accepts a JSON payload with username and password.
Verifies credentials against the stored hash.
If valid, returns a token (either API key or JWT) to be used for authenticating future requests.
Description
Develop
register
andlogin
endpoints within theFastAPI
library. Theregister
endpoint will allow new users to create an account, and thelogin
endpoint will validate credentials. Upon successful login, the endpoint should return an API key, which the user will use for authenticating future requests to protected endpoints (e.g.,/service
). The task includes secure storage of user credentials and token generation.User Stories
Details
/register
and/login
) with authentication token generation. The token should be used for authorizing access to thepredict-similarity
endpoint.POST /register
username
andpassword
.bcrypt
).POST /login
username
andpassword
.Example Usage and Responses
Register Request:
Register Success Response:
Login Request:
Login Success Response (API Key):
Error Responses:
Implementation Steps
Create User Database Model:
username
,hashed_password
, andtoken
(if using API keys).bcrypt
.Develop Registration Endpoint (
/register
):username
andpassword
).Develop Login Endpoint (
/login
):username
andpassword
).username
.Secure Endpoint Integration:
/service
and any future protected endpoints to require authentication by checking the provided token.Code Mockup
Here’s an example using JWT for token generation.
Edge Cases
401 Unauthorized
if login credentials are incorrect.