Instantly share code, notes, and snippets.
-
Star
1
(1)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save nikosft/8e0d7eefe504f5b647d95170c320c9de to your computer and use it in GitHub Desktop.
Generate a client assertion and retrieve an access token from iSHARE satellite
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 Microsoft.IdentityModel.Tokens; | |
using System.IdentityModel.Tokens.Jwt; | |
using System.Security.Cryptography; | |
/* | |
* Extract from a p12 file the public key in X509 format | |
* openssl pkcs12 -in i4trust.p12 -out certificate.cer -nokeys | |
* Remove first and last line | |
*/ | |
var clientCertificate = @" | |
MIIEoTCCAomgAwIBAgIILLrbm3Y3rOkwDQYJKoZIhvcNAQELBQAwSDEZMBcGA1UE | |
AwwQaVNIQVJFVGVzdENBX1RMUzENMAsGA1UECwwEVGVzdDEPMA0GA1UECgwGaVNI | |
QVJFMQswCQYDVQQGEwJOTDAeFw0yMjA0MDUwNzQxMjRaFw0yMzA3MjIxNTE0MTNa | |
MGIxFTATBgNVBAMMDEFCQyBUcnVja2luZzEcMBoGA1UEBRMTRVUuRU9SSS5OTDAw | |
MDAwMDAwMTENMAsGA1UECwwEVGVzdDEPMA0GA1UECgwGaVNIQVJFMQswCQYDVQQG | |
EwJOTDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOKEmHHT7CkVesLw | |
4uFmo5isXZLqTVtEbntnHOZ3zplEEVcB0Y1U3eZdc4VkNAqnbZi/gYJ+Sfeca6wu | |
6La+1SktruENoaktlm1sMbtMOCPPYe0PaBvh+ksIdjFm96xOaz/y3PVmYNjBqh/H | |
zsiW2/+7Px8raGYYfJVVtGBumKvvhdUIqQsi1lik7YeFYh69EkqyCTSyFvSCsuvQ | |
VAV/DFRyhTJlLXWvBc3GpVWhU3IsHWRo+hVeIHt4y8i9ewfzg4wDzu8BM3ZPq79N | |
5D6h03Jdb9kzoNhzHtcDyjrHNWiTbGfwYed4FIh2hGptgUPE4dtftaVvhr8LcQjp | |
nodhTfUCAwEAAaN1MHMwDAYDVR0TAQH/BAIwADAfBgNVHSMEGDAWgBQWPOcg6daU | |
7x/lovG4/L7q3yw3gTATBgNVHSUEDDAKBggrBgEFBQcDATAdBgNVHQ4EFgQULSfL | |
etp7x0q6bBpA8gvzZhswQN0wDgYDVR0PAQH/BAQDAgWgMA0GCSqGSIb3DQEBCwUA | |
A4ICAQBsuXGZTVWDRONqRbacc0YDnEBoSTQ4U1C/z3ojptvO2aqXK1HEel8b71zH | |
vkV8atQKcBBA3cGPXBmgRjBfmffZ/7ENdlXp6uVXv7b0eVhX0/HKFGpKi0Hn7vZ4 | |
jUkLLopuJ0IgLQDFC2ECCADUuaNlga7Zk7AZ6hDq6M+PH4olxmG1E+KGp5cPywgC | |
zwzFBtxmqpcTl8MiuARdlZ9iTOcm+b6pve0uBYrE7G+ESh8NtPjeLXp2KJC+fYcE | |
/j2GW9HRASvRMsycWBx3Vt1m2m8EwZPCWH5gprfPs84YGb6LCMRSWAVqwqTOwxRM | |
kjIYYHWaYtZMYbVikW23sVFY/PMdi5WY+RQuA2VToyCfbGWPt+6zSKujp23kkbq4 | |
CA9EdJc+thjF7ig3l9iVn/7+ve4xSwOcympm2pBHPqqemLaxd+QYztKRzWtha+Yg | |
iU0YMJzLFVQLOSCywnNJ810bQ122A8yus/zXaEn43nkxq3pRyiCM0PTUoANFK1CJ | |
c7hzFS1WF7Xl5CQxGGOZt8rEacOI8fmxwar7s6g6yMVbCbsABfi7KiQQleInpCD7 | |
RBgwT6mjQlQaG/RGZvRPGtpxLiAoXTm1bDtyo0jK/7RUc4eaQoeASEr2F7SGx6R0L | |
TQKyQQrwxAZYWBI1yVTwG0egYSCadP5xTkGUwU63pLuamN6rg== | |
"; | |
/* | |
* Extract from a p12 file the private key in PKCS#1 format | |
* openssl pkcs12 -in i4trust.p12 -nodes -nocerts | openssl rsa -out private.key | |
* Remove first and last line | |
*/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8Bx44S+pVQGDL2cKAMtD8zZJt/UEjepfgUQih5 | |
4cHTUZXTK4VHDUmyuFbyNgiIe2dtCLDJmqotWvkCgYEAj05Uj1lYjiK/1qeLobBM | |
TXtbUAWke14b2sl3DzIT6v4DOwjVTHNMUqedwvskrevjpZZBpNCxqRpkJL2L5lru | |
qSZ+lrHW4U/sICg55ZZG4IYbUMziRBtF4rNlf/jQS3/natkbHYGPkgHvI663WvJ7 | |
c+pohRGQclsscGkg6Bm+yK0CgYBvZ7l6GTDcaURaS7YM73njIgwXKKFeC1WWpnWv | |
c29gpUO+Od0+FB3mzviWKtW9d7yjY4prwmOpbh+3zZ+M44Xjw+95JqOEliYo6N7O | |
GnqRLScSvo+PClX4Z8afgDjlp3AvnY2FLY78XoDBSb1y7VyETJkXebVd5weijmlg | |
khkOMQKBgQC7LsIPdIENWgki83cLiNOQtnHER5dKE7cMflmT+cQXDGC0/6ejQDvt | |
S7zG77zGko+6HChX8qFJC/drzCurC6P8GGe5Cv5TzbC8qAfk+93MOKs1FpxlIV6S | |
qvvTz/JV6V5pJCAOhTau4ssyTmyZwNMfBlt1xD+KaMPhYZsWdntmbA== | |
"; | |
var clientEORI = "EU.EORI.NL000000001"; | |
var satelliteID = "EU.EORI.NL000000000"; | |
var satelliteURL = "https://scheme.isharetest.net"; | |
//HEADER fields | |
var x5c = new String[] {clientCertificate.Replace("\n", String.Empty).Replace("\r", String.Empty) }; | |
RSA rsa = RSA.Create(); | |
rsa.ImportRSAPrivateKey(Convert.FromBase64String(clientPrivateKey), out _); | |
var jwtHeader = new JwtHeader( | |
new SigningCredentials( | |
key: new RsaSecurityKey(rsa), | |
algorithm: SecurityAlgorithms.RsaSha256)); | |
jwtHeader.Add("x5c", x5c); | |
//PAYLOAD fields | |
var iss = clientEORI; | |
var sub = clientEORI; | |
var aud = satelliteID; | |
var jti = System.Guid.NewGuid(); | |
var iat = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); | |
var exp = DateTimeOffset.UtcNow.AddSeconds(30).ToUnixTimeSeconds(); | |
var payload = new JwtPayload(); | |
payload.Add("jti", jti); | |
payload.Add("aud", aud); | |
payload.Add("iat", iat); | |
payload.Add("exp", exp); | |
payload.Add("iss", iss); | |
payload.Add("sub", sub); | |
//Create the signed token | |
var jwtToken = new JwtSecurityToken(jwtHeader, payload); | |
var jwtTokenHandler = new JwtSecurityTokenHandler(); | |
var clientAssertion = jwtTokenHandler.WriteToken(jwtToken); | |
Console.WriteLine(clientAssertion); | |
//Request access token | |
var requestBody = new FormUrlEncodedContent(new Dictionary<string, string> | |
{ | |
{ "grant_type", "client_credentials" }, | |
{ "scope", "iSHARE" }, | |
{ "client_id", clientEORI }, | |
{ "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" }, | |
{ "client_assertion", clientAssertion } | |
}); | |
var httpClient = new HttpClient(); | |
var response = await httpClient.PostAsync(satelliteURL + "/connect/token", requestBody); | |
var content = await response.Content.ReadAsStringAsync(); | |
Console.WriteLine(content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment