RESTful Authentication

前端 未结 14 2022
死守一世寂寞
死守一世寂寞 2020-11-21 22:56

What does RESTful Authentication mean and how does it work? I can\'t find a good overview on Google. My only understanding is that you pass the session key (remeberal) in

相关标签:
14条回答
  • 2020-11-21 23:32

    Here is a truly and completely RESTful authentication solution:

    1. Create a public/private key pair on the authentication server.
    2. Distribute the public key to all servers.
    3. When a client authenticates:

      3.1. issue a token which contains the following:

      • Expiration time
      • users name (optional)
      • users IP (optional)
      • hash of a password (optional)

      3.2. Encrypt the token with the private key.

      3.3. Send the encrypted token back to the user.

    4. When the user accesses any API they must also pass in their auth token.

    5. Servers can verify that the token is valid by decrypting it using the auth server's public key.

    This is stateless/RESTful authentication.

    Note, that if a password hash were included the user would also send the unencrypted password along with the authentication token. The server could verify that the password matched the password that was used to create the authentication token by comparing hashes. A secure connection using something like HTTPS would be necessary. Javascript on the client side could handle getting the user's password and storing it client side, either in memory or in a cookie, possibly encrypted with the server's public key.

    0 讨论(0)
  • 2020-11-21 23:35

    It's certainly not about "session keys" as it is generally used to refer to sessionless authentication which is performed within all of the constraints of REST. Each request is self-describing, carrying enough information to authorize the request on its own without any server-side application state.

    The easiest way to approach this is by starting with HTTP's built-in authentication mechanisms in RFC 2617.

    0 讨论(0)
  • 2020-11-21 23:38

    The 'very insightful' article mentioned by @skrebel ( http://www.berenddeboer.net/rest/authentication.html ) discusses a convoluted but really broken method of authentication.

    You may try to visit the page (which is supposed to be viewable only to authenticated user) http://www.berenddeboer.net/rest/site/authenticated.html without any login credentials.

    (Sorry I can't comment on the answer.)

    I would say REST and authentication simply do not mix. REST means stateless but 'authenticated' is a state. You cannot have them both at the same layer. If you are a RESTful advocate and frown upon states, then you have to go with HTTPS (i.e. leave the security issue to another layer).

    0 讨论(0)
  • 2020-11-21 23:42

    I think restful authentication involves the passing of an authentication token as a parameter in the request. Examples are the use of apikeys by api's. I don't believe the use of cookies or http auth qualifies.

    0 讨论(0)
  • 2020-11-21 23:45

    That's the way to do that: Using OAuth 2.0 for Login.

    You may use other authentication methods other then Google's as long as it supports OAuth.

    0 讨论(0)
  • 2020-11-21 23:50

    Tips valid for securing any web application

    If you want to secure your application, then you should definitely start by using HTTPS instead of HTTP, this ensures a creating secure channel between you & the users that will prevent sniffing the data sent back & forth to the users & will help keep the data exchanged confidential.

    You can use JWTs (JSON Web Tokens) to secure RESTful APIs, this has many benefits when compared to the server-side sessions, the benefits are mainly:

    1- More scalable, as your API servers will not have to maintain sessions for each user (which can be a big burden when you have many sessions)

    2- JWTs are self contained & have the claims which define the user role for example & what he can access & issued at date & expiry date (after which JWT won't be valid)

    3- Easier to handle across load-balancers & if you have multiple API servers as you won't have to share session data nor configure server to route the session to same server, whenever a request with a JWT hit any server it can be authenticated & authorized

    4- Less pressure on your DB as well as you won't have to constantly store & retrieve session id & data for each request

    5- The JWTs can't be tampered with if you use a strong key to sign the JWT, so you can trust the claims in the JWT that is sent with the request without having to check the user session & whether he is authorized or not, you can just check the JWT & then you are all set to know who & what this user can do.

    Many libraries provide easy ways to create & validate JWTs in most programming languages, for example: in node.js one of the most popular is jsonwebtoken

    Since REST APIs generally aims to keep the server stateless, so JWTs are more compatible with that concept as each request is sent with Authorization token that is self contained (JWT) without the server having to keep track of user session compared to sessions which make the server stateful so that it remembers the user & his role, however, sessions are also widely used & have their pros, which you can search for if you want.

    One important thing to note is that you have to securely deliver the JWT to the client using HTTPS & save it in a secure place (for example in local storage).

    You can learn more about JWTs from this link

    0 讨论(0)
提交回复
热议问题