Giter Site home page Giter Site logo

angular-node-hmac-example's Introduction

This software is licensed under the terms and conditions of the MIT license.

Authentication Handshake

As the user hits the login button, Angular will generate a temporary HMAC secret which looks like this:

temporarySecret = sha512(username:password)

An HTTP POST request will then be sent to /login. The body data of this request looks like this:

{
    'payload': randomData,
    'username': username
}

An Angular request transformer will then add X-MICRO-TIME and X-HMAC-HASH headers to the request. The X-MICRO-TIME header is basically a UTC timestamp. The final HMAC hash is the generated using the requested URL, HTTP data and micro time.

hash = hmacSHA512(requestURL:data:microTime, temporarySecret)

The server will analyze and validate the request (see server/hmac.js#performLogin, it's well documented). This login request validation uses the technique described above to generate a temporary secret. If the user entered his username and password correctly the secret generated on the server will match the one generated on the client. Note: We do NOT send the actual password nor a hash of it so there is no way for a hacker to get it using network sniffing. If the validation was successful the server will generate a session token which will be sent back to the client.

A new secret to sign any further requests is then generated on the client and on the server.

secret = sha512(temporarySecret:sessionToken)

Authenticated requests

The Angular request transformer will intercept any further requests and sign them. The transformer adds the following headers:

X-MICRO-TIME    -   current unix timestamp
X-SESSION-TOKEN -   the session token received by the server
X-HMAC-HASH     -   the actual signature

The message signature is generated like this (see above for information on secret generation):

hash = hmacSHA512(requestURL:data:microTime, secret)

The server validates all incoming requests as follows (see server/hmac.js#verify):

  • Check if request is too old (using the micro time header)
  • Check if the session token exists (using the session token header)
  • Hash the message the same way the client did

Since the secret used to generate the signatures is based on the user's name, password and a session token we can also bind session tokens to a specific user.

How to use this example

You will need to have NodeJS and NPM installed.

Get the code (you can also download a ZIP file):

git clone https://github.com/Monofraps/angular-node-hmac-example.git

Install dependencies

npm install

Build the files

grunt

Run the server

cd _build && node app.js

You can now connect to the server: http://localhost:3000

Social

If you have any questions, recommendations or requests simply contact me or create an issue

Twitter - @monofraps

angular-node-hmac-example's People

Contributors

nristock avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.