TreeGateway / tree-gateway

This is a full featured and free API Gateway
http://treegateway.com
MIT License
189 stars 42 forks source link

Admin Rest API AuthenticationError: #166

Closed thesoulkiller closed 5 years ago

thesoulkiller commented 5 years ago

Hello, I've installed tree-gateway from npm and it's working normally with redis in docker. I've started tree-gateway with default settings and added an user. Basically I've entered the following : $ sudo npm install tree-gateway -g $ docker run -p 6379:6379 -d --name redis redis $ tree-gateway $ treeGatewayConfig users add -l adminz -p adminz -n ADMINZ -r admin config

Then I post the login data from bash terminal $ curl -X POST "http://localhost:8001/users/authentication" -H "accept: text/html" -H "Content-Type: application/x-www-form-urlencoded" -d "login=adminz&password=adminz" And it returned the token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6ImFkbWlueiIsIm5hbWUiOiJBRE1JTloiLCJyb2xlcyI6WyJhZG1pbiIsImNvbmZpZyJdLCJpYXQiOjE1Mzg3NDgwNTAsImV4cCI6MTUzODc1NTI1MH0.eGJsW0jqpF4J4vQpNf71Li9WwP6zWvgnYS4I7AhaqI0

When I tried to send the token as: $ curl -X GET "http://127.0.0.1:8001/users" -H "accept:application/json" -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6ImFkbWlueiIsIm5hbWUiOiJBRE1JTloiLCJyb2xlcyI6WyJhZG1pbiIsImNvbmZpZyJdLCJpYXQiOjE1Mzg3NDgwNTAsImV4cCI6MTUzODc1NTI1MH0.eGJsW0jqpF4J4vQpNf71Li9WwP6zWvgnYS4I7AhaqI0"

It returned AuthenticationError as :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>AuthenticationError: Unauthorized<br> &nbsp; &nbsp;at allFailed (/usr/lib/node_modules/tree-gateway/node_modules/passport/lib/middleware/authenticate.js:159:21)<br> &nbsp; &nbsp;at attempt (/usr/lib/node_modules/tree-gateway/node_modules/passport/lib/middleware/authenticate.js:167:28)<br> &nbsp; &nbsp;at JwtStrategy.strategy.fail (/usr/lib/node_modules/tree-gateway/node_modules/passport/lib/middleware/authenticate.js:284:9)<br> &nbsp; &nbsp;at JwtStrategy.authenticate (/usr/lib/node_modules/tree-gateway/dist/pipeline/authentication/strategies/jwt.js:109:25)<br> &nbsp; &nbsp;at attempt (/usr/lib/node_modules/tree-gateway/node_modules/passport/lib/middleware/authenticate.js:348:16)<br> &nbsp; &nbsp;at authenticate (/usr/lib/node_modules/tree-gateway/node_modules/passport/lib/middleware/authenticate.js:349:7)<br> &nbsp; &nbsp;at app.use (/usr/lib/node_modules/tree-gateway/dist/admin/api/users.js:44:13)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at trim_prefix (/usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/index.js:317:13)<br> &nbsp; &nbsp;at /usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/index.js:284:7<br> &nbsp; &nbsp;at Function.process_params (/usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/index.js:335:12)<br> &nbsp; &nbsp;at next (/usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/index.js:275:10)<br> &nbsp; &nbsp;at /usr/lib/node_modules/tree-gateway/node_modules/express-winston/index.js:341:9<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at trim_prefix (/usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/index.js:317:13)<br> &nbsp; &nbsp;at /usr/lib/node_modules/tree-gateway/node_modules/express/lib/router/index.js:284:7</pre>
</body>
</html>

What might be the issue, I couldn't figure out. My system is Debian 9. Thank you in advance

thiagobustamante commented 5 years ago

The problem is in your Authorization header. The authorization header must follow the syntax:

Authorization: <type> <credentials>

Tree Gateway use a Bearer token authentication. So you need to send the header as:

Authorization: Bearer <credentials>

Or, in your example:

$ curl -X GET "http://127.0.0.1:8001/users" -H "accept:application/json" -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpbiI6ImFkbWlueiIsIm5hbWUiOiJBRE1JTloiLCJyb2xlcyI6WyJhZG1pbiIsImNvbmZpZyJdLCJpYXQiOjE1Mzg3NDgwNTAsImV4cCI6MTUzODc1NTI1MH0.eGJsW0jqpF4J4vQpNf71Li9WwP6zWvgnYS4I7AhaqI0"