Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Last active June 4, 2018 16:03
Show Gist options
  • Save bookercodes/822f6bb7bd7c0d4ebf9b28aec8e9d755 to your computer and use it in GitHub Desktop.
Save bookercodes/822f6bb7bd7c0d4ebf9b28aec8e9d755 to your computer and use it in GitHub Desktop.
// Install-Package jose-rt
using System;
using System.Text;
using System.Collections.Generic;
using Jose;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var token = GenerateToken("Charlie", "+uhKzPz1I0dYnoToAhkj+DCR51exCybpJ8MlI6LjdQI=", "072d42ca-bcc6-4b19-bfe3-926a1e7d04db", "2a5bc257-0f82-4dd1-83fc-9ab039186a13");
Console.WriteLine(token);
}
public static long ToTimestamp(DateTime time)
{
var timeSpan = (time - new DateTime(1970, 1, 1, 0, 0, 0));
return (long)timeSpan.TotalSeconds;
}
private static string GenerateToken(string userId, string secretKey, string instance, string publicKey) {
var exp = ToTimestamp(DateTime.UtcNow.AddMinutes(120));
var iat = ToTimestamp(DateTime.UtcNow);
var payload = new Dictionary<string, object>()
{
{ "sub", userId },
{ "exp", exp },
{ "iat", iat },
{ "instance", instance },
{ "su", true },
{ "iss", "api_keys/" + publicKey }
};
//
byte[] encodedKey = Encoding.ASCII.GetBytes(secretKey);
return Jose.JWT.Encode(payload, encodedKey, JwsAlgorithm.HS256);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment