-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
263 additions
and
251 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
exports.up = DB => { | ||
DB.sql(` | ||
create table if not exists sessions ( | ||
uid uuid NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(), | ||
user_id integer REFERENCES users(id) not null, | ||
expiry timestamp without time zone DEFAULT now() + interval '1 year' | ||
); | ||
`); | ||
}; | ||
|
||
exports.down = DB => { | ||
DB.sql(` | ||
drop table if exists sessions; | ||
`); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const oauth = 'https://github.com/login/oauth'; | ||
export const baseurl = process.env.BASEURL; | ||
export const secure = baseurl.startsWith('https:'); | ||
|
||
export const client_id = process.env.GITHUB_CLIENT_ID; | ||
export const client_secret = process.env.GITHUB_CLIENT_SECRET; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import send from '@polka/send'; | ||
import devalue from 'devalue'; | ||
import * as cookie from 'cookie'; | ||
import * as httpie from 'httpie'; | ||
import { parse, stringify } from 'querystring'; | ||
import { sanitize_user, create_user, create_session } from '../../utils/auth'; | ||
import { oauth, secure, client_id, client_secret } from './_config.js'; | ||
|
||
export async function get(req, res) { | ||
try { | ||
// Trade "code" for "access_token" | ||
const r1 = await httpie.post(`${oauth}/access_token?` + stringify({ | ||
code: req.query.code, | ||
client_id, | ||
client_secret, | ||
})); | ||
|
||
// Now fetch User details | ||
const { access_token } = parse(r1.data); | ||
const r2 = await httpie.get('https://api.github.com/user', { | ||
headers: { | ||
'User-Agent': 'svelte.dev', | ||
Authorization: `token ${access_token}` | ||
} | ||
}); | ||
|
||
const user = await create_user(r2.data, access_token); | ||
const session = await create_session(user); | ||
|
||
res.writeHead(200, { | ||
'Set-Cookie': cookie.serialize('sid', session.uid, { | ||
maxAge: 31536000, | ||
path: '/', | ||
httpOnly: true, | ||
secure | ||
}), | ||
'Content-Type': 'text/html; charset=utf-8' | ||
}); | ||
|
||
res.end(` | ||
<script> | ||
window.opener.postMessage({ | ||
user: ${devalue(sanitize_user(user))} | ||
}, window.location.origin); | ||
</script> | ||
`); | ||
} catch (err) { | ||
console.error('GET /auth/callback', err); | ||
send(res, 500, err.data, { | ||
'Content-Type': err.headers['content-type'], | ||
'Content-Length': err.headers['content-length'] | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import send from '@polka/send'; | ||
import { stringify } from 'querystring'; | ||
import { oauth, baseurl, client_id } from './_config.js'; | ||
|
||
export const get = client_id | ||
? (req, res) => { | ||
const Location = `${oauth}/authorize?` + stringify({ | ||
scope: 'read:user', | ||
client_id, | ||
redirect_uri: `${baseurl}/auth/callback`, | ||
}); | ||
|
||
send(res, 302, Location, { Location }); | ||
} | ||
: (req, res) => { | ||
send(res, 500, ` | ||
<body style="font-family: sans-serif; background: rgb(255,215,215); border: 2px solid red; margin: 0; padding: 1em;"> | ||
<h1>Missing .env file</h1> | ||
<p>In order to use GitHub authentication, you will need to <a target="_blank" href="https://github.com/settings/developers">register an OAuth application</a> and create a local .env file:</p> | ||
<pre>GITHUB_CLIENT_ID=[YOUR_APP_ID]\nGITHUB_CLIENT_SECRET=[YOUR_APP_SECRET]\nBASEURL=http://localhost:3000</pre> | ||
<p>The <code>BASEURL</code> variable should match the callback URL specified for your app.</p> | ||
<p>See also <a target="_blank" href="https://github.com/sveltejs/svelte/tree/master/site#repl-github-integration">here</a></p> | ||
</body> | ||
`, { | ||
'Content-Type': 'text/html; charset=utf-8' | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import send from '@polka/send'; | ||
import * as cookie from 'cookie'; | ||
import { secure } from './_config.js'; | ||
import { delete_session } from '../../utils/auth.js'; | ||
|
||
export async function get(req, res) { | ||
await delete_session(req.cookies.sid); | ||
|
||
send(res, 200, '', { | ||
'Set-Cookie': cookie.serialize('sid', '', { | ||
maxAge: -1, | ||
path: '/', | ||
httpOnly: true, | ||
secure | ||
}) | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
15 changes: 12 additions & 3 deletions
15
site/src/routes/repl/[id]/_components/AppControls/UserMenu.svelte
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
Oops, something went wrong.