Refer this https://github.com/kabbo06/radius_auth_using_api
I want to Authentication using Rest API Call and I need to set Session-Timeout for each user from API.
How to do it?
I try to ask solution from ChatGPT, but it is not work. It look like some thing went wrong on condition reply:Profile == “admin”
This is what I try.
API service
USER_DATA = {
'user1': {
'password': 'pass1',
'profile': 'admin'
},
'user2': {
'password': 'pass2',
'profile': 'guest'
}
}
@app.route('/authenticate', methods=['POST'])
def authenticate_user():
data = request.get_json()
username = data['username']
password = data['password']
# Verify the Authorization token
auth_header = request.headers.get('Authorization')
if auth_header != f"Bearer {SECRET_TOKEN}":
return Response(
"Invalid token!",
status=403,
)
# Authentication logic with profile
user = USER_DATA.get(username)
if user and user['password'] == password:
response = {
'authenticated': 'ok',
'profile': user['profile']
}
return jsonify(response)
else:
return Response(
"Authentication Not Allowed!",
status=400,
)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
mods-enabled/rest
connect_uri = "http://192.168.56.106:5000"
authenticate {
uri = "${..connect_uri}/authenticate"
method = "post"
request_headers {
Content-Type = "application/json"
Authorization = "Bearer my-hardcoded-token"
}
body = json
data = '{ "username": "%{User-Name}", "password": "%{User-Password}" }'
force_to = json
return {
ok = {
update {
&reply: += "Profile := %{json:profile}"
}
}
}
}
sites-available/default
authorize {
...
if (reply:Profile == "admin") {
update reply {
Filter-Id := "admin-filter"
Session-Timeout := 3600
}
}
elsif (reply:Profile == "guest") {
update reply {
Filter-Id := "guest-filter"
Session-Timeout := 1800
}
}
...
}
New contributor
Wicha Meesuksabai is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.