Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF, RESTful Web Services and custom authentication

I am beginning to believe I am a grossly incompetent programmer. After a lot of reading, I still cannot figure out how to cleanly implement an authentication mechanism for a RESTful WCF Service.

I have tried everything:

  1. Following this guide, only to find out that the proposed authentication mechanism is a ugly hack.

  2. Installing the WCF REST Starter Kit, only to find out it is obsolete and has been replaced with another project template.

  3. Install said project template, only to find out it does not provide any authentication mechanism of its own.

  4. Install an IIS module that provides custom authentication functionality at transport level, only to find out the author provides zero documentation. only to find out I need to implement a MembershipProvider. (I want to provide only an UserNamePasswordValidator.)

Is there any sane way to implement an authentication mechanism for a RESTful WCF Service?

like image 297
pyon Avatar asked May 16 '11 18:05

pyon


1 Answers

When I was investigating how to implement security for my own WCF RESTful service I spent some time looking at how other popular services like flickr and amazon implement their own security - assuming that they've probably spent far more time thinking about it than I have. Flickr's documentation in particular helped shaped how I formated my signatures and requests.

In the end I chose a HMAC (Hash-based Message Authentication Code) authentication scheme for my services.

I created a custom HMAC ServiceAuthorizationManager that validates the signature of each request as it comes in. Each request contains the following:

  • a user token
  • timestamp
  • nonce
  • signature

Using this information the manager can look up the user's secret from their token and can recreate the signature on the server using the provided information.

My signature consists of an MD5 hash of the following (values are concatenated together in a specific order and hashed so the value can be repeated on the server):

  • apikey
  • userToken
  • secret
  • timestamp
  • nonce

I store the nonce's in a memcache instance for a short period of time in order to quickly check against any replay attacks. After that time skew (about 10 minutes) the timestamp is used to reject any other old requests.

I can post some snippets of my code if it will help. In general I've found that the HMAC authentication is generally the safest way to go and is easily supported on any clients that will be using your service (not just .NET).

like image 125
efalconer Avatar answered Nov 15 '22 23:11

efalconer