How about this system. I need some comments and maybe critical security part for this.
System which I use is maybe little bit complicated but 100% custom and should be good. This is a system for custom authentication in sending request to Asp.NET WebApi
System works with sending 2 request
Everything what you need is 2 pairs of data. 1st one is public and 2nd one is secret.
Second pair of data be must be known to both sides (sender and receiver)
public: ApiKey and RequstID where ApiKey is "normal" and requstID have to be unique always;
secret: UserName and Password (both side have to know these data)
Sender: Send 1st request with 3 parameters: 1st= ApiKey, 2nd=RequstID, 3rd=Hash(ApiKey+RequestID+USerName+Pass)
Server: Read RequstID Read ApiKey and get data about users UserName and Pass for this ApiKey From the own side: Hash(ApiKey+RequestID+USerName+Pass)
Check is Hash from Sender same us from Server
If is False: BadRequest - or whatever...
if is True
Before all - Create on database on table for collect data about request. This is table with columns (e.g.): ID(autoincrement), RequstID, Token, TokenValidateDateTime
Before create new row, check is there already this RequestID and if there is return BadRequest. If there is not - make new row.
- RequstID is RequstID from request;
- Token - Generate token (e.g. Guid.NewGuid().ToString());
- TokenValidateDateTime= DateTime.Now.AddMinutes(2) - or some other value ...
In response for the first request send back this this Token (from item 2)
In the second request, Sender have to use AGAIN same RequstID and Token (from response before)
Server will check Combination RequstID and Token Token validation (depend on current date time);
Is everything is OK, user is validated if is not - BadRequest, or whatever
Any suggestions or comments are welcome :)