-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: live admin page authenticates with github #87
base: main
Are you sure you want to change the base?
Changes from all commits
940a8b5
e3f4574
43f81e2
77ca364
aaf2feb
ef8e69b
ae18038
cc272aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"dependencies": { | ||
"@astrojs/check": "^0.5.3", | ||
"@astrojs/mdx": "^2.1.1", | ||
"@astrojs/node": "^8.3.3", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What did you need node for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Astro requires an ssr adapter in order to enable server side rendering : https://docs.astro.build/en/guides/server-side-rendering |
||
"@astrojs/react": "^3.1.0", | ||
"@astrojs/rss": "^4.0.5", | ||
"@astrojs/sitemap": "^3.0.5", | ||
|
@@ -36,11 +37,13 @@ | |
"react-dom": "^18.2.0", | ||
"react-hook-form": "^7.51.3", | ||
"react-select": "^5.8.0", | ||
"simple-oauth2": "2.5.2", | ||
"tailwindcss": "^3.4.1", | ||
"typescript": "^5.3.3" | ||
}, | ||
"devDependencies": { | ||
"@brown-ccv/eslint-config": "^0.3.0", | ||
"@types/simple-oauth2": "^2.5.2", | ||
"eslint": "^8.56.0", | ||
"eslint-plugin-astro": "^0.31.4", | ||
"prettier": "^3.2.5", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as simpleOauthModule from "simple-oauth2" | ||
|
||
export const create = () => | ||
simpleOauthModule.create({ | ||
client: { | ||
id: process.env.OAUTH_CLIENT_ID || "", | ||
secret: process.env.OAUTH_CLIENT_SECRET || "", | ||
}, | ||
auth: { | ||
tokenHost: `https://github.com`, | ||
tokenPath: `/login/oauth/access_token`, | ||
authorizePath: `/login/oauth/authorize`, | ||
}, | ||
}) | ||
|
||
export const renderBody = (status: string, content: object) => ` | ||
<script> | ||
const receiveMessage = (message) => { | ||
window.opener.postMessage( | ||
'authorization:github:${status}:${JSON.stringify(content)}', | ||
message.origin | ||
); | ||
|
||
window.removeEventListener("message", receiveMessage, false); | ||
} | ||
window.addEventListener("message", receiveMessage, false); | ||
|
||
window.opener.postMessage("authorizing:github", "*"); | ||
</script> | ||
` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
import Layout from "../../layouts/Layout.astro" | ||
import { GET } from "./auth" | ||
|
||
const auth = await GET(Astro.url.origin) | ||
--- | ||
|
||
<Layout title="Authenticate" description="" /> | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need this if the thing is calling |
||
|
||
<script> | ||
const receiveMessage = (message: MessageEvent) => { | ||
window.opener.postMessage( | ||
"authorization:github:${status}:${JSON.stringify(content)}", | ||
message.origin | ||
) | ||
window.removeEventListener("message", receiveMessage, false) | ||
} | ||
window.addEventListener("message", receiveMessage, false) | ||
|
||
window.opener.postMessage("authorizing:github", "*") | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import crypto from "crypto" | ||
import { create } from "../../api/_lib/oauth2.ts" | ||
|
||
export const prerender = false | ||
|
||
export const GET = (host: string) => { | ||
const randomString = () => crypto.randomBytes(4).toString(`hex`) | ||
const oauth2 = create() | ||
|
||
const url = oauth2.authorizationCode.authorizeURL({ | ||
redirect_uri: `${host}/api/callback`, | ||
scope: `repo,user`, | ||
state: randomString(), | ||
}) | ||
return Response.redirect(url, 301) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
import Layout from "../../layouts/Layout.astro" | ||
import { GetCallback } from "./callback" | ||
|
||
const callback = GetCallback(Astro.request, Astro.url.origin) | ||
--- | ||
|
||
<Layout title="Callback" description="" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { create, renderBody } from "../../api/_lib/oauth2.ts" | ||
|
||
export const prerender = false | ||
|
||
export const GetCallback = async (req: Request, host: string) => { | ||
const params = new URLSearchParams(req.url) | ||
const code = params.get("code") || "" | ||
const oauth2 = create() | ||
|
||
try { | ||
const accessToken = await oauth2.authorizationCode.getToken({ | ||
code, | ||
redirect_uri: `${host}/api/callback`, | ||
}) | ||
const { token } = oauth2.accessToken.create(accessToken) | ||
return new Response( | ||
renderBody("success", { | ||
token: token.access_token, | ||
provider: "github", | ||
}), | ||
{ status: 200 } | ||
) | ||
} catch (e: any) { | ||
return new Response(renderBody("error", e.message), { status: 200 }) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably why firebase is upset