Created
February 13, 2013 19:00
-
-
Save parsnips/4947150 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Web; | |
namespace CamTokenGetter | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); | |
var authexample = new AuthenticationExample(); | |
authexample.ProcessRequest(); | |
Console.ReadKey(); | |
} | |
} | |
public static class DateTimeHelper | |
{ | |
public static long ToEpochTime(this DateTime dt) | |
{ | |
TimeSpan t = dt - new DateTime(1970, 1, 1); | |
long secondsSinceEpoch = (long)t.TotalSeconds; | |
return secondsSinceEpoch; | |
} | |
} | |
public class AuthenticationExample | |
{ | |
private static readonly string AUDIENCE = "auth.webtrends.com"; | |
private static readonly string SCOPE = "sapi.webtrends.com"; | |
public void ProcessRequest() | |
{ | |
string clientId = "5dc19a573395451cb90330f128807cee.app.webtrends.com"; // set = your client id (client token) | |
string secret = "6f6525b6cc1a4ab285023be1a5c2b5ad"; // set = your client secret | |
int expMinutes = 10; | |
var url = @"https://osauth.webtrends.com/v1/token"; | |
var assertion = buildAssertion(clientId, secret, expMinutes, 35043, 131); | |
var request = new StringDictionary | |
{ | |
{ "client_id", clientId }, | |
{ "client_assertion", HttpUtility.UrlEncode(assertion) } | |
}; | |
string result = HttpPost(url, request); | |
Console.Write(result); | |
} | |
private static string buildAssertion(string clientId, string clientSecret, int expMinutes, int accountId, int userId) | |
{ | |
// Json representation of the header | |
string header = "{\"typ\":\"JWT\", \"alg\":\"HS256\"}"; | |
// Token expiration in minutes | |
string exp = DateTime.UtcNow.AddMinutes(expMinutes).ToEpochTime().ToString(); | |
// Json representation of the claims set | |
string claims = "{\"iss\":\"" + clientId + "\",\"prn\":\"" + clientId + "\",\"aud\":\"" + AUDIENCE + "\",\"exp\":" + exp + ",\"scope\":\"" + SCOPE + "\",\"aid\":" + accountId + ",\"uid\":" + userId + " }"; | |
// Base64Encode the component parts | |
string encodedHeader = Base64Encode(header); | |
string encodedClaims = Base64Encode(claims); | |
// The content to be hashed | |
string message = string.Format("{0}.{1}", encodedHeader, encodedClaims); | |
// Hash the content, signed with our secret to derive the signature | |
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(clientSecret)); | |
byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(message)); | |
string signature = Convert.ToBase64String(hash); | |
return string.Format("{0}.{1}.{2}", encodedHeader, encodedClaims, signature); | |
} | |
private static string Base64Encode(string input) | |
{ | |
return Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(input)); | |
} | |
private static string HttpPost(string url, StringDictionary payload) | |
{ | |
try | |
{ | |
HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest; | |
req.Method = "POST"; | |
req.ContentType = "application/x-www-form-urlencoded"; | |
var data = new StringBuilder(); | |
foreach (string key in payload.Keys) | |
{ | |
data.AppendFormat("{0}={1}&", key, payload[key]); | |
} | |
byte[] formData = UTF8Encoding.UTF8.GetBytes(data.ToString().TrimEnd('&')); | |
req.ContentLength = formData.Length; | |
using (var post = req.GetRequestStream()) | |
{ | |
post.Write(formData, 0, formData.Length); | |
} | |
string result = null; | |
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) | |
{ | |
var reader = new StreamReader(resp.GetResponseStream()); | |
result = reader.ReadToEnd(); | |
} | |
return result; | |
} | |
catch (Exception ex) | |
{ | |
Console.Write(ex.Message); | |
throw; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment