Skip to content

Instantly share code, notes, and snippets.

@dylan-asos
Last active August 15, 2022 14:14
Show Gist options
  • Save dylan-asos/091f2b8e6a865538f061f7554fc03566 to your computer and use it in GitHub Desktop.
Save dylan-asos/091f2b8e6a865538f061f7554fc03566 to your computer and use it in GitHub Desktop.
Lookup a github user using a SAML/SSO linked email address and get the associated github username/login
public class GraphUserLookup
{
private readonly HttpClient _httpClient;
public GraphUserLookup(string authToken)
{
// TODO - handle tidy up / manage lifetime of this
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("https://api.github.com/graphql");
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {authToken}");
_httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
_httpClient.DefaultRequestHeaders.Add("User-Agent", "YourUserAgent");
}
public async Task<string> GetUserAsync(string login)
{
//TODO - Replace yourorgname with the name of your github org
var query =
"query { organization(login: \"yourorgname\") { samlIdentityProvider { externalIdentities(first: 100, userName:\"" +
login + "\") { edges { node { user { login } samlIdentity { nameId } } } } } } }";
var result = await PostAsync(query);
var edges = result.Data.Organization.SamlIdentityProvider.ExternalIdentities.Edges.FirstOrDefault();
var userLogin = edges?.Node.User?.Login;
return userLogin;
}
private async Task<GraphQlResponse> PostAsync(string query)
{
var request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
var payload = new GraphQlQueryPayload
{
query = query
};
var serialized = JsonConvert.SerializeObject(payload);
using var stringContent = new StringContent(
serialized,
Encoding.UTF8,
"application/json");
request.Content = stringContent;
var response = await _httpClient.SendAsync(request);
var responsePayload = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GraphQlResponse>(responsePayload);
}
}
private class GraphQlQueryPayload
{
[JsonProperty("query")]
public string Query { get; set; }
}
public class GraphQlResponse
{
[JsonProperty("data")] public Data Data { get; set; }
}
public class Data
{
[JsonProperty("organization")] public Organization Organization { get; set; }
}
public class Organization
{
[JsonProperty("samlIdentityProvider")] public SamlIdentityProvider SamlIdentityProvider { get; set; }
}
public class SamlIdentityProvider
{
[JsonProperty("externalIdentities")] public ExternalIdentities ExternalIdentities { get; set; }
}
public class ExternalIdentities
{
[JsonProperty("edges")] public Edge[] Edges { get; set; }
}
public class Edge
{
[JsonProperty("node")] public Node Node { get; set; }
}
public class Node
{
[JsonProperty("user")] public User User { get; set; }
[JsonProperty("samlIdentity")] public SamlIdentity SamlIdentity { get; set; }
}
public class SamlIdentity
{
[JsonProperty("nameId")] public string NameId { get; set; }
}
public class User
{
[JsonProperty("login")] public string Login { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment