Skip to content

Instantly share code, notes, and snippets.

@spencerkittleson
Created March 29, 2023 17:35
Show Gist options
  • Save spencerkittleson/9eaa0bccc6ff880cb7715c5a3c1e6005 to your computer and use it in GitHub Desktop.
Save spencerkittleson/9eaa0bccc6ff880cb7715c5a3c1e6005 to your computer and use it in GitHub Desktop.
CXOneExpert Server Side Tokens
using System;
using System.IO;
using System.Security.Cryptography;
using System.Net.Http;
// Server API Token key and secret are available from API token management dashboard when Server API Token is generated
var key = "da28b6ec3ea350db524d80099f8c9f40f2fab2f4caf91a8d49d4cb4d659a9785";
var secret = "60a63916a77ef70c0ecf42b134f44fbb9732b395ae33dc6d5165aad2b5668bb4";
// include username prefixed with '='
var user = "=admin";
// ...or include userid
user = "1";
// hash time, key, user with secret
var hash = "";
var epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
using (var hmac = new HMACSHA256(Encoding.ASCII.GetBytes(secret)))
{
var bytes = hmac.ComputeHash(Encoding.ASCII.GetBytes(string.Format("{0}_{1}_{2}", key, epoch, user)));
hash = BitConverter.ToString(bytes).Replace("-", "");
}
var signature = string.Format("tkn_{0}_{1}_{2}_{3}", key, epoch, user, hash);
// send signature as X-Deki-Token HTTP header to MindTouch API (WebRequest is used in this example)
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Deki-Token", signature);
var response = await client.GetAsync("https://authtalk.mindtouch.es/@api/deki/users/current?dream.out.format=json");
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
// Exchange signature token for a user's authtoken for immediate access via a link.
// https://{authtalk.mindtouch.es}/@api/deki/users/authenticate?x-deki-token={signature}
Console.WriteLine($"https://authtalk.mindtouch.es/@api/deki/users/authenticate?x-deki-token={signature}");
Console.WriteLine($"https://authtalk.mindtouch.es/@api/deki/users/authenticate?x-deki-token={signature}&redirect=https%3A%2F%2Fauthtalk.mindtouch.es%2F");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment