I developed a server that takes a csv file and write it in the cloud using python 3.10 (connextion, flask) and swagger v2. It worked just fine, all curl requests have returned 200 and the file have been saved correctly.
However, since I added the api-key authentication in the yml file, the curl requests always returned this error:
{
"detail": "No authorization token provided",
"status": 401,
"title": "Unauthorized",
"type": "about:blank"
}
For the yml file I used the swagger v2 documentation and it seems alright to me, so I don't understand where the problem is. I hope some of you can figure why this happens.
The request:
curl --location 'http://localhost:5000/v1/public/function' \
--header 'x-api-key: apikey' \
--form 'key1="value1"' \
--form 'key2="value2"' \
--form 'key3="value3"' \
--form 'file=@"/path/to/file/file.csv"'
The yml file:
swagger: "2.0"
info:
description: This is the swagger file that goes with our server code
version: "1.0.0"
title: Swagger REST Article
consumes:
- "application/json"
produces:
- "application/json"
securityDefinitions:
APIKeyHeader:
type: apiKey
in: header
name: x-api-key
basePath: "/v1"
paths:
/public/function:
post:
operationId: "file.function"
consumes:
- multipart/form-data
security:
- APIKeyHeader: []
parameters:
- in: formData
name: file
type: file
required: true
description: The file to upload.
- in: formData
name: key1
type: string
required: false
- in: formData
name: key2
type: string
required: false
- in: formData
name: key3
type: string
required: false
responses:
200:
description: "Successful request"
schema:
type: "string"
items:
properties:
response:
type: "string"
The server:
app = connexion.App(__name__, specification_dir='./')
CORS(app.app)
app.add_api('swagger.yml')
@app.route('/')
def home():
"""
This function just responds to the browser ULR
localhost:5000/
"""
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
File for authentication:
from connexion.exceptions import OAuthProblem
APIKEY_DB = {
"apikey": {
"uid": 1
}
}
def apikey_auth(apikey):
print(apikey)
info = APIKEY_DB.get(apikey)
print(info)
if not info:
print("Invalid token")
raise OAuthProblem('Invalid token')
print("Valid token")
return info