Create a gist now

Instantly share code, notes, and snippets.

@murbano83 /remote_auth.cs Secret
Last active Feb 23, 2016

Embed
What would you like to do?
SSO Anónimo
using System.Security.Cryptography;
using System;
using System.Collections;
using System.Text;
public class RemoteAuth
{
public static void Main(){
// Definimos un hash con los parametros necesarios
Hashtable hash = new Hashtable();
hash.Add("external_id",EXTERNAL_ID);
hash.Add("account_id",ACCOUNT_ID);
hash.Add("token",TOKEN);
// Obtenemos los parametos para el request (external_id,account_id,hash,sha256,timestamp)
Hashtable postdata = RemoteAuth.remote_auth(hash);
foreach (string key in postdata.Keys) {
System.Console.Write(key);
System.Console.Write(": ");
System.Console.WriteLine(postdata[key]);
}
System.Console.WriteLine("https://vlex.com/session/remote_auth?");
foreach (string key in postdata.Keys) {
System.Console.Write(key);
System.Console.Write("=");
System.Console.Write(postdata[key]);
System.Console.Write("&");
}
}
public static Hashtable remote_auth(Hashtable ht){
SHA256 sha = new SHA256Managed();
string timestamp = RemoteAuth.GetCurrentMilli();
ht.Add("timestamp",timestamp.ToString());
string[] keys = new string[] {"external_id", "account_id", "token","timestamp"};
string buffer = string.Empty;
for(int i=0;i<keys.Length;i++){
buffer += ht[keys[i]];
}
byte[] dataBytes = Encoding.Default.GetBytes(buffer);
byte[] cryString = sha.ComputeHash(dataBytes);
string cryStringHexa = string.Empty;
foreach (byte x in cryString) {
cryStringHexa += string.Format("{0:x2}", x);
}
ht.Add("hash",cryStringHexa);
ht.Add("sha256","true");
ht.Remove("token");
return ht;
}
public static string GetCurrentMilli(){
DateTime now = DateTime.Now;
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = now.ToUniversalTime() - origin;
return Math.Floor(diff.TotalSeconds).ToString();
}
}
<?php
/**
* Returns a URL that grants access to vLex without explicitly signing in
*
* @since Unknown
*
* @param array $data :
* Containing
* ["external_id"]: Your internal ID of the user.
* ["account_id"]: The corporate account id of the company.
* ["token"] The corporate account token of the company
* @return string : The URL granting access to vLex or NULL when errors occur
*/
function remote_auth($data) {
// Set default remote authorization path
$REMOTE_AUTH_URL = "http://vlex.com/session/remote_auth";
// Sort $data checking that required parameters are present
$keys = array("external_id", "account_id", "token");
foreach($keys as $key) {
if(!isset($data[$key])) throw new Exception("Missing parameter: $key");
$values[$key] = $data[$key];
}
// Add timestamp
$values["timestamp"] = time();
// Add hash from the concatenated data
$values["hash"] = md5(implode($values));
// delete token
unset($values["token"]);
// Generate the query and return the URL
$query = http_build_query($values); // name=yourname&amp;email=...
return "$REMOTE_AUTH_URL?$query";
}
/* Example
$my_data = array("account_id" => "1234", "token" => "TOKEN", "external_id" => "id1234");
$single_sing_on = remote_auth($my_data);
header("Location: ".$single_sing_on);
*/
?>
require 'rubygems'
require 'digest/md5' # For the hexdigest function
require 'active_support' # For the .to_query function
module Vlex
class RemoteAuth
class << self
# Set default remote authorization path
REMOTE_AUTH_URL = "http://vlex.com/session/remote_auth"
def remote_auth(params)
# Add timestamp
params = params.clone # so that the delete at the end doesn't update the params hash
params[:timestamp] = Time.now.to_i
# Check that required parameters are present and concatenate them
seed = ""
keys = [:external_id, :account_id, :token, :timestamp]
keys.each do |key|
raise ArgumentError.new "Missing parameter :#{key}" if params[key].nil?
seed << params[key].to_s
end
# Add hash from the concatenated data
params[:hash] = Digest::MD5.hexdigest(seed)
params.delete(:token)
# Generate the query and return the URL
"#{REMOTE_AUTH_URL}?#{params.to_query}"
end
end
end
end
# Example:
# data = {:account_id => "1234", :external_id => "tester", :token => "TOKEN"}
# puts Vlex::RemoteAuth::remote_auth data
Imports System.Security.Cryptography
Imports System.Collections
Imports System.Text
Public Class RemoteAuth
Public Shared Sub Main()
' Definimos un hash con los parametros necesarios
Dim hash As New Hashtable()
hash.Add("external_id", EXTERNAL_ID)
hash.Add("account_id", ACCOUNT_ID)
hash.Add("token", TOKEN)
' Obtenemos los parametos para el request (external_id,account_id,hash,sha256,timestamp)
Dim postdata As Hashtable = RemoteAuth.remote_auth(hash)
For Each key As String In postdata.Keys
System.Console.Write(key)
System.Console.Write(": ")
System.Console.WriteLine(postdata(key))
Next
System.Console.WriteLine("https://vlex.com/session/remote_auth?")
For Each key As String In postdata.Keys
System.Console.Write(key)
System.Console.Write("=")
System.Console.Write(postdata(key))
System.Console.Write("&")
Next
End Sub
Public Shared Function remote_auth(ht As Hashtable) As Hashtable
Dim sha As SHA256 = New SHA256Managed()
Dim timestamp As String = RemoteAuth.GetCurrentMilli()
ht.Add("timestamp", timestamp.ToString())
Dim keys As String() = New String() {"external_id", "account_id", "token", "timestamp"}
Dim buffer As String = String.Empty
For i As Integer = 0 To keys.Length - 1
buffer += ht(keys(i))
Next
Dim dataBytes As Byte() = Encoding.[Default].GetBytes(buffer)
Dim cryString As Byte() = sha.ComputeHash(dataBytes)
Dim cryStringHexa As String = String.Empty
For Each x As Byte In cryString
cryStringHexa += String.Format("{0:x2}", x)
Next
ht.Add("hash", cryStringHexa)
ht.Add("sha256", "true")
ht.Remove("token")
Return ht
End Function
Public Shared Function GetCurrentMilli() As String
Dim now As DateTime = DateTime.Now
Dim origin As New DateTime(1970, 1, 1, 0, 0, 0, _
0)
Dim diff As TimeSpan = now.ToUniversalTime() - origin
Return Math.Floor(diff.TotalSeconds).ToString()
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment