Skip to content

Commit

Permalink
🚑 PWの上書きが可能な状態を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
r74tech committed Oct 30, 2023
1 parent 64d125d commit 4e3742e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/css/wikidot.css
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,11 @@ code, .code {
.page-source {
white-space: pre-wrap;
}


.errors {
color: #cc0f35;
background: #feecf0;
margin: 0.5em;
font-size: 1.2rem;
}
6 changes: 5 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ <h2>作成履歴</h2>
<input type="button" name="share/share update" id="actionarea-share" class="btn btn-primary" value="Share/Share update">
<input type="button" name="history" id="actionarea-history" class="btn btn-primary" value="History">
</div>
<!-- <input type="text" id="password" placeholder="password"> -->
<input type="text" id="password" placeholder="password">
<div class="errors">
<div id="messages"></div>
</div>
<input type="hidden" id="password-encripted">
<details>
<summary id="actionarea-advancedsettings">Advanced Settings</summary>
<h2 id="actionarea-advancedsettingstitle">Edit Sidebar</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/script/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// api.ts
import { postDataToGAS, getDataFromGAS, getHistoryFromGAS } from './helper';
import { getDataFromGAS, getHistoryFromGAS } from './helper';

export const fetchSharedData = async (shortid: string) => {
try {
Expand Down
91 changes: 75 additions & 16 deletions src/script/eventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { debounce } from 'ts-debounce';


import {
generateShortId, getOrCreateUserShortId, getCurrentPageShortId, encryptSha256, encryptAES
generateShortId, getOrCreateUserShortId, getCurrentPageShortId, encryptSha256, setCookie, getCookie
} from './utils';


Expand Down Expand Up @@ -74,25 +74,45 @@ export const handleDOMContentLoaded = async () => {
const shortid = pathParts[2]; // shortId を取得

// console.log(shortid);
// const enchash = localStorage.getItem(`FtmlPWHash[${shortid}]`);
const enchash = getCookie(shortid);

document.body.style.display = "none";
let password, hash;

password = prompt("パスワードを入力してください");
document.body.style.display = "";

hash = encryptSha256(password);
if (!enchash) {
document.body.style.display = "none";
password = prompt("パスワードを入力してください");
document.body.style.display = "";

hash = encryptSha256(password);
} else {
hash = enchash;
// document.getElementById("password")をdisabledにする
const Elementpassword = document.getElementById("password");
const ElementpasswordEncripted = document.getElementById("password-encripted");
if (Elementpassword) {
Elementpassword.setAttribute("disabled", "disabled");
Elementpassword.setAttribute("placeholder", "パスワード設定済");
}
// ElementpasswordEncriptedにhashを入れる
if (ElementpasswordEncripted) {
ElementpasswordEncripted.setAttribute("value", hash);
}
}

try {
const data = await getDataPWFromGAS(shortid, hash); // 適切な関数名に修正
if (data.error) {
displayLocalStorageData(`FtmlStorage[${shortid}]`)
// displayLocalStorageData(`FtmlStorage[${shortid}]`)
displayData({ title: "PASSWORD ERROR", source: "パスワードが間違っています" });
} else {
displayDataPW(data.data, password);
displayData(data.data);
// localStorage.setItem(`FtmlPWHash[${shortid}]`, hash);
setCookie(shortid, hash);
}
} catch (error) {
console.error('Error fetching data:', error);
displayLocalStorageData(`FtmlStorage[${shortid}]`)
// displayLocalStorageData(`FtmlStorage[${shortid}]`)
}


Expand Down Expand Up @@ -211,30 +231,69 @@ const handleEditsaveButtonClick = async () => {

// 共有ボタンを押したときの処理
const handleShareButtonClick = async () => {
const shortId = getCurrentPageShortId() || generateShortId();
let shortId = getCurrentPageShortId() || generateShortId();

const url = new URL(window.location.href);
const pathname = url.pathname;
const pathParts = pathname.split('/').filter(part => part);

const Elpassword = document.getElementById("password");
const ElementpasswordEncripted = document.getElementById("password-encripted");

let hash = encryptSha256(Elpassword.value);

// Elpasswordがdisabledになっている場合は、ElementpasswordEncriptedの値を使う
if (Elpassword && Elpassword.hasAttribute("disabled") && ElementpasswordEncripted) {
hash = ElementpasswordEncripted.getAttribute("value");
}

const dataToSend = {
shortid: shortId,
title: edittitleField.value,
source: `'${editpageField.value}`, // Add a newline at the end of the source
createdby: getOrCreateUserShortId(),
};
let isPassword = false;
// #password がある場合はパスワードを送信
const password = document.getElementById("password");
if (password.value) {

// share/pw/ がある場合はパスワードを必ず送信
if (pathParts.length === 3 && pathParts[0] === 'share' && pathParts[1] === "pw") {
isPassword = true;
dataToSend["password"] = encryptSha256(password.value);
dataToSend["password"] = hash;
dataToSend["pw"] = "true";
dataToSend["title"] = encryptAES(dataToSend["title"], password.value);
dataToSend["source"] = encryptAES(dataToSend["source"], password.value);
}

// share/ の場合は、パスワードがあればshortIdを変更して送信
else if (pathParts.length === 2 && pathParts[0] === 'share') {
if (Elpassword.value) {
isPassword = true;
shortId = generateShortId();
dataToSend["password"] = hash;
dataToSend["pw"] = "true";
}
}
else {
// #password がある場合はパスワードを送信
if (Elpassword.value) {
isPassword = true;
dataToSend["password"] = hash;
dataToSend["pw"] = "true";
}
}




console.debug('Sending data to GAS:', dataToSend);

try {
const response = await postDataToGAS(dataToSend);
if (response.error) {
console.error('Error sending data to GAS:', response.error);
const errorElement = document.querySelector("#messages");
if (errorElement) {
errorElement.innerHTML = response.error;
errorElement.style.padding = "1em";
}
}
else if (isPassword) {
window.location.href = `/share/pw/${shortId}`;
Expand Down
2 changes: 1 addition & 1 deletion src/script/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ftml } from './worker';
import { setTextContentForElement } from './utils';

const GAS_API_URL = "https://script.google.com/macros/s/AKfycbyUuBsrBvyy_QdstSaOYK6RuPl3LonBpayL_c-KsnUza5TFG5kuBdQ_J91Y7GBJyNmEMQ/exec"
const GAS_API_URL = "https://script.google.com/macros/s/AKfycbxFUwsKJtnPNIwfUqg4BGwaT8N1E9l7REkEza1VFmAssXGlvWyhWKs4-GR-Mp0UWzMygg/exec"



Expand Down
27 changes: 26 additions & 1 deletion src/script/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,29 @@ export function encryptAES(data: string, key: string) {

export function decryptAES(data: string, key: string) {
return crypto.AES.decrypt(data, key).toString(crypto.enc.Utf8);
}
}


export function setCookie(shortid: string, hash: string, daysToExpire: number = 7) {
var date = new Date();
date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000));
var expires = "expires=" + date.toUTCString();
document.cookie = "FtmlPWHash_" + shortid + "=" + hash + ";" + expires + ";path=/;Secure;";
}

export function getCookie(shortid: string): string | null {
const name = "FtmlPWHash_" + shortid + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return null;
}

0 comments on commit 4e3742e

Please sign in to comment.