aboutsummaryrefslogtreecommitdiffhomepage
path: root/auth.js
diff options
context:
space:
mode:
Diffstat (limited to 'auth.js')
-rw-r--r--auth.js126
1 files changed, 126 insertions, 0 deletions
diff --git a/auth.js b/auth.js
new file mode 100644
index 0000000..c29823f
--- /dev/null
+++ b/auth.js
@@ -0,0 +1,126 @@
1const simpleOauthModule = require('simple-oauth2');
2const randomstring = require('randomstring');
3const Secrets = require('./lib/secrets');
4
5const secrets = new Secrets({
6 GIT_HOSTNAME: 'https://github.com',
7 OAUTH_TOKEN_PATH: '/login/oauth/access_token',
8 OAUTH_AUTHORIZE_PATH: '/login/oauth/authorize',
9 OAUTH_CLIENT_ID: 'foo',
10 OAUTH_CLIENT_SECRET: 'bar',
11 REDIRECT_URL: 'https://www.control-alt-del.org/oauth/callback',
12 OAUTH_SCOPES: 'repo,user',
13});
14
15
16function getScript(mess, content) {
17 return `<html><body><script>
18 (function() {
19 function receiveMessage(e) {
20 console.log("receiveMessage %o", e)
21 window.opener.postMessage(
22 'authorization:github:${mess}:${JSON.stringify(content)}',
23 e.origin
24 )
25 window.removeEventListener("message",receiveMessage,false);
26 }
27 window.addEventListener("message", receiveMessage, false)
28 console.log("Sending message: %o", "github")
29 window.opener.postMessage("authorizing:github", "*")
30 })()
31 </script></body></html>`;
32}
33
34module.exports.auth = (e, ctx, cb) => secrets.init()
35 .then(() => {
36 const oauth2 = simpleOauthModule.create({
37 client: {
38 id: secrets.OAUTH_CLIENT_ID,
39 secret: secrets.OAUTH_CLIENT_SECRET,
40 },
41 auth: {
42 tokenHost: secrets.GIT_HOSTNAME,
43 tokenPath: secrets.OAUTH_TOKEN_PATH,
44 authorizePath: secrets.OAUTH_AUTHORIZE_PATH,
45 },
46 });
47
48 // Authorization uri definition
49 const authorizationUri = oauth2.authorizationCode.authorizeURL({
50 redirect_uri: secrets.REDIRECT_URL,
51 scope: secrets.OAUTH_SCOPES,
52 state: randomstring.generate(32),
53 });
54
55 cb(null, {
56 statusCode: 302,
57 headers: {
58 Location: authorizationUri,
59 },
60 });
61 });
62
63module.exports.callback = (e, ctx, cb) => {
64 let oauth2;
65 secrets.init()
66 .then(() => {
67 oauth2 = simpleOauthModule.create({
68 client: {
69 id: secrets.OAUTH_CLIENT_ID,
70 secret: secrets.OAUTH_CLIENT_SECRET,
71 },
72 auth: {
73 tokenHost: secrets.GIT_HOSTNAME,
74 tokenPath: secrets.OAUTH_TOKEN_PATH,
75 authorizePath: secrets.OAUTH_AUTHORIZE_PATH,
76 },
77 });
78
79 const options = {
80 code: e.queryStringParameters.code,
81 };
82 return oauth2.authorizationCode.getToken(options);
83 })
84 .then((result) => {
85 const token = oauth2.accessToken.create(result);
86 cb(
87 null,
88 {
89 statusCode: 200,
90 headers: {
91 'Content-Type': 'text/html',
92 },
93 body: getScript('success', {
94 token: token.token.access_token,
95 provider: 'github',
96 }),
97 },
98 );
99 })
100 .catch((err) => {
101 cb(null, {
102 statusCode: 200,
103 headers: {
104 'Content-Type': 'text/html',
105 },
106 body: getScript('error', err),
107 });
108 });
109};
110
111module.exports.success = (e, ctx, cb) => cb(
112 null,
113 {
114 statusCode: 204,
115 body: '',
116 },
117);
118
119module.exports.default = (e, ctx, cb) => {
120 cb(null, {
121 statusCode: 302,
122 headers: {
123 Location: '/auth',
124 },
125 });
126};