Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Please review my code for 'JWT' authentication.

  1. Are there any security issues?
  2. Where should I store the secret's key, DB or InMemory?
  3. What's a good 'JWT' Lifetime?
  4. Should I send the 'JWT' in Header for every request?
  5. What should be done when a 'JWT' Expires?

Token Manager

using System;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
using Procoor_V4_Model.SecurityModel;

namespace Procoor_V4_Security
{
    public class TokenManager
    {
        public static string EncodeToken(JwtPayload jwtPayload, string secret)
        {
            const string algorithm = "HS256";

            var header = new JwtHeader
            {
                Typ = "JWT", 
                Alg = algorithm
            };

            var jwt = Base64Encode(JsonConvert.SerializeObject(header)) + "." + Base64Encode(JsonConvert.SerializeObject(jwtPayload));

            jwt += "." + Sign(jwt, secret);

            return jwt;
        }

        public static JwtPayload DecodeToken(string token, string secret)
        {
            var segments = token.Split('.');

            if(segments.Length != 3)
                throw new Exception("Token structure is incorrect!");

            JwtHeader header = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(Base64Decode(segments[0])), typeof(JwtHeader));
            JwtPayload jwtPayload = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(Base64Decode(segments[1])), typeof(JwtPayload));

            var rawSignature = segments[0] + '.' + segments[1];

            if(!Verify(rawSignature, secret, segments[2]))
                throw new Exception("Verification Failed");

            return jwtPayload;
        }

        private static bool Verify(string rawSignature, string secret, string signature)
        {
            return signature == Sign(rawSignature, secret);
        }

        private static string Sign(string str, string key)
        {
            var encoding = new ASCIIEncoding();

            byte[] signature;

            using (var crypto = new HMACSHA256(encoding.GetBytes(key)))
            {
                signature = crypto.ComputeHash(encoding.GetBytes(str));
            }

            return Base64Encode(signature);
        }

        public static string Base64Encode(dynamic obj)
        {
            Type strType = obj.GetType();

            var base64EncodedValue = Convert.ToBase64String(strType.Name.ToLower() == "string" ? Encoding.UTF8.GetBytes(obj) : obj);

            return base64EncodedValue;
        }

        public static dynamic Base64Decode(string str)
        {
            var base64DecodedValue = Convert.FromBase64String(str);

            return base64DecodedValue;
        }
    }
}

How I currently use it:

var secret = TokenManager.Base64Encode(SecurityConstants.KeyForHmacSha256);
var currentTime = (long)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime()).TotalSeconds;
var payload = new JwtPayload
{
    iss = SecurityConstants.TokenIssuer,
    sub = "46897484",
    iat = currentTime,
    exp = currentTime + 1800
};

var jwt = TokenManager.CreateJwtToken(payload, secret);
share|improve this question
up vote 3 down vote accepted

Are there any security issues? Where should I store the secret's key, DB or InMemory? What's a good 'JWT' Lifetime? Should I send the 'JWT' in Header for every request? What should be done when a 'JWT' Expires?

  1. The contents are merely base64 encoded and thus are simple to decode -- so don't encode your valued customers personal data.

cut and paste your token in here as evidence https://developers.google.com/wallet/digital/docs/jwtdecoder

  1. the secret key is not a session one so in memory makes no sense. buy an oracle license and then stick it in there. or theres nothing wrong with something nice and simple like this.

        const string algorithm = "HS256";
    
  2. if you're logging a user onto a site. 1 hour? its possible to copy/paste the token and gain access to data. so it depends on the nature of the data.

  3. yes. its an awesome method to send to a REST server instead of some internal userid that never expires

  4. kick them out of your site. make them relog in and get a new token (with another hour long token)

share|improve this answer
    
Thanks for your answer, I just wanted to make sure of something, The key should be a constant and not changed with every time a 'JWT' expires? – Beyond Programming Nov 17 '14 at 7:39
1  
correct. the key is constant. – Gabe Rainbow Nov 17 '14 at 8:07
    
One more question if you may be patient, I am very grateful for your time, The Secret Key if same for all users not just one, and now that i know it's a constant do i have to change it over a period of time. – Beyond Programming Nov 17 '14 at 8:21
    
Sorry friend. Back to work. The secret key you receive from Facebook, Live or other OAuth provider is the same one used for all users. Its not a classic password situation. Its used to sign the contents of the JWT. Therefore, when the content (including username of user) of the JWT changes, so does the resulting hashed signature. That hash is added and sent WITH the token. The secret key is used to decode the signature and thereby verifying the JWT and its contents (are constructed by provider you expect). You will see evidence of the hash in that google decoder. The hash is labeled signature – Gabe Rainbow Dec 2 '14 at 22:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.