TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Rest api authorization

31 pointsby vskrover 8 years ago
I am working on service which exposes REST API.<p>What is the best way to implement authorization for incoming requests.<p>* Identify who is making the request. Can we use api key for this.<p>* Make sure this REST api can be used just as easily using curl. I am not sure how to do this, as I dn&#x27;t think exposing API key is a good idea. I can ask clients to hash request params with api key, but that makes it non-trivial to use with curl.<p>What are the standard practices, and what are the trade offs.<p>Please be kind and helpful. If I said anything wrong, please correct me instead of flagging this.<p>Any help would be appreciated. Glad to provide any info that makes it useful to answer

13 comments

jaypaulyniceover 8 years ago
OAuth2 is the way to go for REST services especially if you plan to let users give other apps&#x2F;developers access without giving out their passwords. For better security, you want to decouple the authentication server from your web application (2 separate databases at least)<p>The authentication server stores email&#x2F;username, encrypted password, and roles. To access the web app, you first get a token from the authentication server by exchanging a client_id, secret, username, password and grant type. The token is used whenever you want to make a request to the web app. The authentication server has an endpoint that lets the web app check to see if the token is valid and what roles the client has.<p>The token is only valid for a short time and can be revoked. To know who is making the request, you associate the username&#x2F;email from the authentication server with a user object on the web application so you can look up based on username&#x2F;email.<p>It&#x27;s not worth doing this from scratch as there are plenty of open source implementations out there already like Spring Security Oauth2 and other libraries for Django&#x2F;python, but they all require some reading to get started.<p>I&#x27;ve used Spring Security Oauth2, but it&#x27;s not very well documented. I&#x27;ve thought about open sourcing my work, but not sure yet.
评论 #12716882 未加载
RandomOpinionover 8 years ago
I&#x27;ve seen people use JWT (<a href="https:&#x2F;&#x2F;jwt.io&#x2F;introduction&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jwt.io&#x2F;introduction&#x2F;</a>) for REST API auth. There are a variety of libraries that implement it and it&#x27;s fairly simple to use.<p>Since it&#x27;s just another HTTP header field, cURL can include it easily enough. Granted, you&#x27;ll have to generate the encoded token externally but you would have had to do that with any other auth mechanism anyway.
johnjuuljensenover 8 years ago
Use basic authentication and ssl. The users credentials will be encrypted and the API will be easily consumable from cli and from browsers.
评论 #12723781 未加载
atmosxover 8 years ago
HMAC for auth to avoid having the key exposed at every request.<p>For the rest: <a href="http:&#x2F;&#x2F;www.javabeat.net&#x2F;rest-api-best-practices&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.javabeat.net&#x2F;rest-api-best-practices&#x2F;</a><p>And documentation: <a href="https:&#x2F;&#x2F;github.com&#x2F;Rebilly&#x2F;ReDoc&#x2F;blob&#x2F;master&#x2F;README.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Rebilly&#x2F;ReDoc&#x2F;blob&#x2F;master&#x2F;README.md</a><p>There many more comprehensive resources about sane API design (use HATEOAS, pagination, etc.) but you don&#x27;t have to implement everything from v1<p>ps. SSL goes without saying even if it&#x27;s a public API
rashkovover 8 years ago
I really wish this topic got discussed more because there don&#x27;t seem to be a lot of great options.<p>I personally use an OAuth 2 library using the &quot;Resource Owner Password Credentials Grant&quot; which is where you POST a username and password, and you get back a session token. OAuth 2 has a few other types of grant flows but they don&#x27;t make as much sense for REST only APIs.<p>The downside of this password grant flow is that anyone can create a client to work against your API, and potentially they can steal passwords in a man-in-the-middle fashion. One way to prevent this is to give your &quot;trusted clients&quot; a secret token, and then verify that token before issuing a session.<p>However you can&#x27;t hide a secret in browser-side JavaScript and even mobile apps can be be decompiled, so this isn&#x27;t perfect either. Some devices provide a hardware enclave to store your secrets in, but most don&#x27;t.<p>Another weakness is that if your SSL breaks, then you&#x27;re essentially sending the passwords in clear text over the wire. Another commenter mentioned HMAC encryption of the password which might help. That this isn&#x27;t recommended by oauth is concerning. It&#x27;s not the best standard and password grant is its weakest form. [Edit: now that I think about it, HMAC requires having another shared secret between your API and your client. Storing secrets on the client is difficult, as discussed in the previous paragraph]<p>JWT seems new and not too widely used but worth looking into. It has its own downsides like some difficulty with revoking sessions from the server side, but there are workarounds for this.<p>I wish there was an industry standard answer that was secure and we could all be happy with but there doesn&#x27;t seem to be much interest in the topic, going by how rarely it gets discussed. Best of luck!
评论 #12716672 未加载
tom_bover 8 years ago
I have semi-copied what AWS did a couple of years ago. Each api consumer has an api key with a private key known to the consumer and server-side. Requests are SSL only and are signed with the private key by the consumer side (including any parameters and a client-side timestamp). For example, using curl:<p><pre><code> #!&#x2F;bin&#x2F;bash TS=$(date +&quot;%s000&quot;) SIG=$(echo -ne &#x27;GET\nhttps:&#x2F;&#x2F;servername:port&#x2F;api&#x2F;resources?api_id=1&amp;ts=&#x27;$TS | openssl dgst -sha256 -hmac &quot;secret-key&quot; -binary | xxd -p -c 32) curl -H &quot;Content-type: application&#x2F;vnd.collection+json&quot; &#x27;https:&#x2F;&#x2F;servername:port&#x2F;api&#x2F;resources?api_id=1&amp;ts=&#x27;$TS&#x27;&amp;signature=&#x27;$SIG </code></pre> I have consumers of the api using node.js and Java as app dev languages with various http client libraries successfully. The actual server-side api is written with Clojure using the Liberator library (highly enjoyed working with this combo).<p>Server-side uses the api_id to check the signature using the shared secretkey. (Edit to clarify). There are two timestamps flying around here. One is a timestamp from the client call to the api ($TS above). This timestamp has to be &quot;close&quot; - completely configurable on server-side - to the server-side time or the request is not valid. A little more subtle is that each resource has a timestamp that is the last time the resource was changed server-side. As part of the authorization for the api call to change a resource, it has to be make the call to the api with the resource&#x27;s most current value for that timestamp to succeed. This is a little goofy logically.<p>This approach works very well when you have a requirement to allow multiple different apps to call the api rather than say, allowing <i>users</i> to call the api from their browser. I don&#x27;t have a case where users are in a web browser app that directly calls the REST api.<p>By the way, if you are using SSL, you don&#x27;t have to worry about the api_id being exposed in either the query parameters or the request headers.
weitzjover 8 years ago
Be aware of JWT when it comes to Login&#x2F;Logout behavior. JWT is not a good fit for user facing login unless you have a session ID on the server. But then you could as well use Cookies and a sessionID.<p>So I would do a Login via Basic Auth or a Form post or a JSON post with username&#x2F;password and then get some kind of token&#x2F;sessionID&#x2F;JWT which expires on the server side. The token might be encrypted on the server side with a secret only known to the server, never the client. Use the sessionStore to implement a proper session expiration scheme.
nickmancolover 8 years ago
Maybe you can take a look of an api gateway like apiman which can leverage the auth &amp; authz to keycloak, or perhaps kong may be the one.
aprdmover 8 years ago
I&#x27;ve used mostly JWT, the tokens with a short TTL which makes it easier to control revoking&#x2F;banning in the backend.<p>It is really simple to set up. In the frontend it&#x27;s pretty straight forward to implement logout and other common behaviors even if the token is still valid because of the TTL.
amingilaniover 8 years ago
Jwts are very straightforward to use, and you can get up and running with Auth0 for free in a few minutes.<p>They do have a 700 DAU limit, but when you need to, you can always implement your own JWT server
quickbenover 8 years ago
Look into identity server 4. YouTube some videos about it and it should get you going. The rest is just picking what&#x27;s popular for your platform&#x2F;language.
emmelaichover 8 years ago
Regarding cURL, note that Firefox and Chrome allow you to copy the request as a Curl command with their Developer Tools. (F12&#x2F;inspect...)
diggernautover 8 years ago
you can (and should :)) use it (token auth) with ssl. To get SSL is not a problem today, and will cost you nothing if you use letsencrypt.org