-
Notifications
You must be signed in to change notification settings - Fork 1
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
19 changed files
with
269 additions
and
49 deletions.
There are no files selected for viewing
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,19 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDHTCCAgWgAwIBAgIJMSxRVGwPXrarMA0GCSqGSIb3DQEBCwUAMCwxKjAoBgNV | ||
BAMTIWRldi11NGptbThnaTYxeXJ6eXJrLnVzLmF1dGgwLmNvbTAeFw0yNDA0MTYx | ||
NjQ0MDBaFw0zNzEyMjQxNjQ0MDBaMCwxKjAoBgNVBAMTIWRldi11NGptbThnaTYx | ||
eXJ6eXJrLnVzLmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC | ||
ggEBAM9Jg2WQCaB1da7jd6h2HhSB1Ujz7pP//0bnzzvXRJe2jXC0K78WWZMx56Je | ||
mDbt02e9jr/AYP/IdPZ6alxQhzRu3LQSe+CKmrNyKQ3qVsHO0LpfpT62F28iyR1+ | ||
9StOv5p6du80WLRuOLOLf52bnfvOZtKiGMkSmQIFks7rlf6+5kttIvC+rC7EEzjX | ||
c8HaHO/hhVQzYbZDXFpee6brMscoqwOGfRB1jfYrn6Qnwn7CTiS4IgNiZwsMfvbu | ||
K7DBpNTipMOPYMK3kQ3J9tQaLBu2qtdVq8y8+2mZn2anOr+9Ge9eLCR367vZVP9M | ||
HniCfKD+MJrEK/4u8P9Bj+90PHMCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAd | ||
BgNVHQ4EFgQUcppxfd/gtygwJpRxZCq8uD7R0UcwDgYDVR0PAQH/BAQDAgKEMA0G | ||
CSqGSIb3DQEBCwUAA4IBAQBJrHzC8FXvJKJ/zweygVQdFsVds68ysclP2jDf2Dcr | ||
wIDomsj0EsaFBoY60xxsf8Te3AU9wgt+1CLARhm34ilSnCmsXNYphcosm6vlMZd/ | ||
WHmU2Zm3tNO3l/soq0VAPaZrYij3JmQe1ag8Ei4RCoQuL2/5CeBZrmOMrmlyMaSZ | ||
rFW0ZGXBMNtwyyHYsKA+ygXj6Sh86TQSnjTzBxopTSp5gO+onddP7GHtB9Pf9mpg | ||
LplnjMsshkxPYzW0Ki3nfNPPJrlNDDBbEgJKlHklItZcUmZvyVics2jVA7s769nM | ||
QTDcmkX2s8+k7aSH033twUJKpzFLCIam0eK689JyVKFC | ||
-----END CERTIFICATE----- |
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 @@ | ||
<template> | ||
<div class="flex md:flex md:flex-grow flex-row-reverse space-x-3 mt-5 mr-4 mb-2"> | ||
<div class=""> | ||
<button | ||
class="hover:text-gray-300 transition font-semibold duration-400 text-white font-bold py-2 px-5 rounded-lg" | ||
style="background-color:#FF0000" @click="logout"> {{ "Log Out" }}</button> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
const logout = async () => { | ||
window.location.href='api/logout' | ||
} | ||
</script> |
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
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
Binary file not shown.
Binary file not shown.
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,12 @@ | ||
{ | ||
"name": "Raspberry", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
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,93 @@ | ||
import { WebSocketServer } from "ws" | ||
import dotenv from "dotenv" | ||
|
||
// Environment variables | ||
dotenv.config({ path: "./.env" }) | ||
const PORT_GM_RASPBERRY: number = parseInt(`${process.env.PORT_GM_RASPBERRY}`) | ||
const PORT_WSS_CONTROLLER_RASPBERRY: number = parseInt(`${process.env.PORT_WSS_CONTROLLER_RASPBERRY}`) | ||
|
||
// Shared variables | ||
let ready: boolean = true | ||
let timer: number = 0 | ||
let score1: number = 0 | ||
let score2: number = 0 | ||
|
||
// FOR GAME MANAGER | ||
const wss_gm = new WebSocketServer({ port: PORT_GM_RASPBERRY}) | ||
|
||
wss_gm.on("listening", () => { | ||
console.log(`WebSocket wss_gm is running on ws://localhost:${PORT_GM_RASPBERRY}`) | ||
}) | ||
|
||
wss_gm.on("error", (error) => { | ||
console.log("WebSocket wss_gm error: " + error) | ||
}) | ||
|
||
wss_gm.on("close", () => { | ||
console.log("WebSocket wss_gm closed") | ||
}) | ||
|
||
wss_gm.on("connection", (ws: any, request) => { | ||
ws.on("message", (data: any) => { | ||
const { type, payload } = JSON.parse(data) | ||
if(type === "CHECK_READY"){ // should already be in | ||
console.log(`Telling GM: ready is ${ready}`) | ||
ws.send(JSON.stringify({ | ||
"type": "IS_READY", | ||
"payload": ready | ||
})) | ||
} | ||
else if(type === "GAME_START"){ | ||
timer = payload["timer"] | ||
const broadcastTimer = setInterval(() => { | ||
if(timer === 0){ | ||
clearInterval(broadcastTimer) | ||
clearInterval(broadcastScore) | ||
ws.send(JSON.stringify({ | ||
"type": "GAME_END", | ||
"payload": {"timer": timer, "score1": score1, "score2": score2} | ||
})) | ||
} | ||
else{ | ||
ws.send(JSON.stringify({ | ||
"type": "TIMER_UPDATE", | ||
"payload": {"timer": timer} | ||
})) | ||
timer-- | ||
} | ||
}, 1000) | ||
const broadcastScore = setInterval(() => { | ||
ws.send(JSON.stringify({ | ||
"type": "SCORE_UPDATE", | ||
"payload": {"score1": score1, "score2": score2} | ||
})) | ||
}, 1000) | ||
} | ||
}) | ||
}) | ||
|
||
// FOR CONTROLLER | ||
const wss_control = new WebSocketServer({ port: PORT_WSS_CONTROLLER_RASPBERRY}) | ||
|
||
wss_control.on("listening", () => { | ||
console.log(`WebSocket wss_control is running on ws://localhost:${PORT_WSS_CONTROLLER_RASPBERRY}`) | ||
}) | ||
|
||
wss_control.on("error", (error) => { | ||
console.log("WebSocket wss_control error: " + error) | ||
}) | ||
|
||
wss_control.on("close", () => { | ||
console.log("WebSocket wss_control closed") | ||
}) | ||
|
||
wss_control.on("connection", (ws: any, request) => { | ||
ws.on("message", (data: any) => { | ||
const { type, payload } = JSON.parse(data) | ||
|
||
if(type === "KEY_INPUT"){ // should already be in | ||
const { keys, playernumber }: {keys: string, playernumber: number} = payload | ||
console.log(`Player ${playernumber} pressed ${keys}`) | ||
} | ||
}) | ||
}) |
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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2020", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"outDir": "./dist" | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
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 {nanoid} from "nanoid" | ||
|
||
const state: {[key: string]: number} = {}; | ||
const genState = () => { const s = nanoid(); state[s] = 1; return s} | ||
const runtime = useRuntimeConfig() | ||
|
||
export const loginRedirectUrl = () => `${runtime.ISSUER}authorize?response_type=id_token&response_mode=form_post&client_id=${runtime.AUTH0_CLIENTID}&scope=openid%20email&redirect_uri=${encodeURIComponent(runtime.BASEURL!+"api/callback")}&nonce=${genState()}` | ||
export const logoutRedirectUrl = (id_token: string) => `${runtime.ISSUER}oidc/logout?id_token_hint=${id_token}&post_logout_redirect_uri=${encodeURIComponent(runtime.BASEURL!+"api/logoutcallback")}&nonce=${genState()}` | ||
|
||
// Number used once | ||
export const verifyNonce = (nonce: string) => { | ||
if (state[nonce]) { | ||
delete state[nonce] | ||
return true | ||
} | ||
return false | ||
} |
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,37 @@ | ||
import jwt from "jsonwebtoken" | ||
import fs from "fs" | ||
import { verifyNonce } from "./auth0" | ||
import { PrismaClient } from "@prisma/client" | ||
import { nanoid } from "nanoid" | ||
|
||
const prisma = new PrismaClient() | ||
|
||
export default defineEventHandler(async event => { | ||
const body = await readBody(event) | ||
const srtoken = body.id_token | ||
const claims: any = jwt.verify(srtoken, fs.readFileSync(process.cwd()+"/cert-dev.pem")) | ||
// check for valid nonce claim | ||
if(claims instanceof Object && "nonce" in claims && verifyNonce(claims["nonce"].toString())){ | ||
const user_id: string = claims["sub"] | ||
const email: string = claims["email"] | ||
const find_user = await prisma.player.findUnique({ | ||
where: { | ||
user_id: user_id | ||
} | ||
}) | ||
// if not in database yet, create new player in database | ||
let username: string | ||
if(find_user){ | ||
username = find_user.username | ||
} | ||
else{ | ||
username = nanoid() | ||
const user = await prisma.player.create({ | ||
data: { user_id: user_id, username: username, email: email}, | ||
}) | ||
} | ||
setCookie(event, "srtoken", srtoken) | ||
setCookie(event, "sruser", username) | ||
} | ||
await sendRedirect(event, "/") | ||
}) |
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,5 @@ | ||
import { loginRedirectUrl } from "./auth0" | ||
|
||
export default defineEventHandler(async event => { | ||
await sendRedirect(event, loginRedirectUrl() || "") | ||
}) |
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 @@ | ||
import { logoutRedirectUrl } from "./auth0" | ||
|
||
export default defineEventHandler(async event => { | ||
const id_token = getCookie(event, "srtoken") | ||
await sendRedirect(event, logoutRedirectUrl(id_token as string) || "") | ||
}) |
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,5 @@ | ||
export default defineEventHandler(async event => { | ||
setCookie(event, "srtoken", "") | ||
setCookie(event, "sruser", "") | ||
await sendRedirect(event, "/") | ||
}) |
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,33 @@ | ||
import {loginRedirectUrl, logoutRedirectUrl} from "../api/auth0" | ||
import jwt from "jsonwebtoken" | ||
import fs from "fs" | ||
const runtime = useRuntimeConfig() | ||
|
||
import { PrismaClient } from "@prisma/client" | ||
const client = new PrismaClient() | ||
|
||
const publicURLs = [] | ||
|
||
export default defineEventHandler(async event => { | ||
const srtoken = getCookie(event, "srtoken") || "" | ||
|
||
if(srtoken){ | ||
|
||
} | ||
|
||
// if (!srtoken) { | ||
// await sendRedirect(event, loginRedirectUrl()); | ||
// } | ||
// else { | ||
// // theoretically logged in | ||
// if (srtoken) { | ||
// try { | ||
// const claims = jwt.verify(srtoken, fs.readFileSync(process.cwd()+"/cert-dev.pem")) | ||
// } catch(e){ | ||
// console.error(e) | ||
// setCookie(event,"srtoken","") | ||
// setCookie(event,"sruser","") | ||
// } | ||
// } | ||
// } | ||
}) |