Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 43b63f0

Browse files
Missing changes
1 parent a609e7a commit 43b63f0

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

octorun/bin/octorun-token

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../src/bin/app-token.js');

octorun/src/bin/app-token.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
var commander = require('commander');
3+
var package = require('../../package.json');
4+
var output = require('../output');
5+
var config = require("../configuration");
6+
var querystring = require('querystring');
7+
8+
commander
9+
.version(package.version)
10+
.option('-h, --host <host>')
11+
.parse(process.argv);
12+
13+
var host = commander.host;
14+
var port = 443;
15+
var scheme = 'https';
16+
17+
var valid = host && config.clientId && config.clientSecret && config.token;
18+
if (valid) {
19+
var https = require(scheme);
20+
21+
var postData = querystring.stringify({
22+
client_id: config.clientId,
23+
client_secret: config.clientSecret,
24+
code: config.token
25+
});
26+
27+
var options = {
28+
protocol: scheme + ':',
29+
hostname: host,
30+
port: port,
31+
path: '/login/oauth/access_token',
32+
method: 'POST',
33+
headers: {
34+
'Content-Type': 'application/x-www-form-urlencoded',
35+
'Content-Length': postData.length
36+
}
37+
};
38+
39+
var req = https.request(options, function (res) {
40+
var success = res.statusCode == 200;
41+
42+
if(!success) {
43+
output.error(res.statusCode);
44+
} else {
45+
res.on('data', function (d) {
46+
output.custom("success", d, true);
47+
});
48+
49+
res.on('end', function (d) {
50+
process.exit();
51+
});
52+
}
53+
});
54+
55+
req.on('error', function (error) {
56+
output.error(error);
57+
});
58+
59+
req.write(postData);
60+
61+
req.end();
62+
}
63+
else {
64+
commander.help();
65+
process.exit(-1);
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Net;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using System.Web;
6+
using GitHub.Logging;
7+
8+
namespace GitHub.Unity
9+
{
10+
public interface IOAuthCallbackListener
11+
{
12+
void Listen(string state, CancellationToken cancel, Action<string> codeCallback);
13+
}
14+
15+
public class OAuthCallbackListener : IOAuthCallbackListener
16+
{
17+
private static readonly ILogging logger = LogHelper.GetLogger<OAuthCallbackListener>();
18+
19+
const int CallbackPort = 42424;
20+
21+
static readonly string CallbackUrl = $"http://localhost:{CallbackPort}/";
22+
private string state;
23+
private CancellationToken cancel;
24+
private Action<string> codeCallback;
25+
private HttpListener httpListener;
26+
27+
public void Listen(string state, CancellationToken cancel, Action<string> codeCallback)
28+
{
29+
this.state = state;
30+
this.cancel = cancel;
31+
this.codeCallback = codeCallback;
32+
33+
httpListener = new HttpListener();
34+
httpListener.Prefixes.Add(CallbackUrl);
35+
httpListener.Start();
36+
Task.Factory.StartNew(Start, cancel);
37+
}
38+
39+
private void Start()
40+
{
41+
try
42+
{
43+
using (httpListener)
44+
{
45+
using (cancel.Register(httpListener.Stop))
46+
{
47+
while (true)
48+
{
49+
var context = httpListener.GetContext();
50+
var queryParts = HttpUtility.ParseQueryString(context.Request.Url.Query);
51+
52+
if (queryParts["state"] == state)
53+
{
54+
context.Response.Close();
55+
codeCallback(queryParts["code"]);
56+
}
57+
}
58+
}
59+
}
60+
}
61+
catch (Exception ex)
62+
{
63+
logger.Warning(ex, "OAuthCallbackListener Error");
64+
}
65+
finally
66+
{
67+
httpListener = null;
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)