-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
103 lines (84 loc) · 3.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using Octokit;
using System.Collections.Generic;
namespace OAuthConsole
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Usage: clientId clientSecret");
return;
}
string clientId = args[0];
string clientSecret = args[1];
// Creates a redirect URI using an available port on the loopback address.
string redirectURI = string.Format("http://{0}:{1}/signin-github/", "localhost", 5000);
Console.WriteLine("redirect URI: " + redirectURI);
string responseString = string.Format(
@"<html>
<head>
<meta http-equiv='refresh' content='10;url=https://gmaster.io'>
</head>
<body>Please return to the app.</body>
</html>");
GitHubOAuth.Result result = GitHubOAuth.GetToken(
clientId, clientSecret, State.Create(32), redirectURI, responseString).Result;
var repos = ListGitHubRepos.Get(result.Token).Result;
foreach (Repository rep in repos)
{
Console.WriteLine(rep.HtmlUrl);
}
Console.ReadKey();
}
static class ListGitHubRepos
{
internal static Task<IReadOnlyList<Repository>> Get(string token)
{
GitHubClient client = new GitHubClient(new ProductHeaderValue(
"test"));
client.Credentials = new Credentials(token);
return client.Repository.GetAllForCurrent();
}
}
static class State
{
internal static string Create(uint length)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] bytes = new byte[length];
rng.GetBytes(bytes);
return base64urlencodeNoPadding(bytes);
}
static string base64urlencodeNoPadding(byte[] buffer)
{
string base64 = Convert.ToBase64String(buffer);
// Converts base64 to base64url.
base64 = base64.Replace("+", "-");
base64 = base64.Replace("/", "_");
// Strips padding.
base64 = base64.Replace("=", "");
return base64;
}
}
}
static class ConsoleHack
{
// Hack to bring the Console window to front.
// ref: http://stackoverflow.com/a/12066376
[DllImport("kernel32.dll", ExactSpelling = true)]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
internal static void BringConsoleToFront()
{
SetForegroundWindow(GetConsoleWindow());
}
}
}