forked from OpenServer/OpenAuth_views
Running prettier
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import App from './App.svelte';
|
||||
import App from "./App.svelte";
|
||||
|
||||
var app = new App({
|
||||
target: document.getElementById("content")
|
||||
target: document.getElementById("content"),
|
||||
});
|
||||
|
||||
export default app;
|
||||
export default app;
|
||||
|
173
src/Login/api.ts
173
src/Login/api.ts
@ -1,9 +1,6 @@
|
||||
import request from "../request";
|
||||
import sha from "../sha512";
|
||||
import {
|
||||
setCookie,
|
||||
getCookie
|
||||
} from "../cookie"
|
||||
import { setCookie, getCookie } from "../cookie";
|
||||
|
||||
export interface TwoFactor {
|
||||
id: string;
|
||||
@ -15,7 +12,7 @@ export enum TFATypes {
|
||||
OTC,
|
||||
BACKUP_CODE,
|
||||
U2F,
|
||||
APP_ALLOW
|
||||
APP_ALLOW,
|
||||
}
|
||||
|
||||
// const Api = {
|
||||
@ -41,8 +38,9 @@ export interface IToken {
|
||||
}
|
||||
|
||||
function makeid(length) {
|
||||
var result = '';
|
||||
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
var result = "";
|
||||
var characters =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
var charactersLength = characters.length;
|
||||
for (var i = 0; i < length; i++) {
|
||||
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
||||
@ -62,98 +60,123 @@ export default class Api {
|
||||
return this.username || getCookie("username");
|
||||
}
|
||||
|
||||
static async setUsername(username: string): Promise<{ error: string | undefined }> {
|
||||
return request("/api/user/login", {
|
||||
type: "username",
|
||||
username
|
||||
}, "POST").then(res => {
|
||||
this.salt = res.salt;
|
||||
this.username = username;
|
||||
return {
|
||||
error: undefined
|
||||
}
|
||||
}).catch(err => {
|
||||
let error = err.message;
|
||||
return { error }
|
||||
})
|
||||
static async setUsername(
|
||||
username: string
|
||||
): Promise<{ error: string | undefined }> {
|
||||
return request(
|
||||
"/api/user/login",
|
||||
{
|
||||
type: "username",
|
||||
username,
|
||||
},
|
||||
"POST"
|
||||
)
|
||||
.then((res) => {
|
||||
this.salt = res.salt;
|
||||
this.username = username;
|
||||
return {
|
||||
error: undefined,
|
||||
};
|
||||
})
|
||||
.catch((err) => {
|
||||
let error = err.message;
|
||||
return { error };
|
||||
});
|
||||
}
|
||||
|
||||
static async setPassword(password: string): Promise<{ error: string | undefined, twofactor?: any }> {
|
||||
static async setPassword(
|
||||
password: string
|
||||
): Promise<{ error: string | undefined; twofactor?: any }> {
|
||||
const date = new Date().valueOf();
|
||||
let pw = sha(sha(this.salt + password) + date.toString());
|
||||
return request("/api/user/login", {
|
||||
type: "password"
|
||||
}, "POST", {
|
||||
username: this.username,
|
||||
password: pw,
|
||||
date
|
||||
}
|
||||
).then(({
|
||||
login,
|
||||
special,
|
||||
tfa
|
||||
}) => {
|
||||
|
||||
this.login = login;
|
||||
this.special = special;
|
||||
|
||||
if (tfa && Array.isArray(tfa) && tfa.length > 0)
|
||||
this.twofactor = tfa;
|
||||
else
|
||||
this.twofactor = undefined;
|
||||
|
||||
|
||||
return {
|
||||
error: undefined
|
||||
return request(
|
||||
"/api/user/login",
|
||||
{
|
||||
type: "password",
|
||||
},
|
||||
"POST",
|
||||
{
|
||||
username: this.username,
|
||||
password: pw,
|
||||
date,
|
||||
}
|
||||
}).catch(err => {
|
||||
let error = err.message;
|
||||
return { error }
|
||||
})
|
||||
)
|
||||
.then(({ login, special, tfa }) => {
|
||||
this.login = login;
|
||||
this.special = special;
|
||||
|
||||
if (tfa && Array.isArray(tfa) && tfa.length > 0)
|
||||
this.twofactor = tfa;
|
||||
else this.twofactor = undefined;
|
||||
|
||||
return {
|
||||
error: undefined,
|
||||
};
|
||||
})
|
||||
.catch((err) => {
|
||||
let error = err.message;
|
||||
return { error };
|
||||
});
|
||||
}
|
||||
|
||||
static gettok() {
|
||||
return {
|
||||
login: this.login.token,
|
||||
special: this.special.token
|
||||
}
|
||||
special: this.special.token,
|
||||
};
|
||||
}
|
||||
|
||||
static async sendBackup(id: string, code: string) {
|
||||
return request("/api/user/twofactor/backup", this.gettok(), "PUT", { code, id }).then(({ login_exp, special_exp }) => {
|
||||
this.login.expires = login_exp;
|
||||
this.special.expires = special_exp;
|
||||
return {};
|
||||
}).catch(err => ({ error: err.message }));
|
||||
return request("/api/user/twofactor/backup", this.gettok(), "PUT", {
|
||||
code,
|
||||
id,
|
||||
})
|
||||
.then(({ login_exp, special_exp }) => {
|
||||
this.login.expires = login_exp;
|
||||
this.special.expires = special_exp;
|
||||
return {};
|
||||
})
|
||||
.catch((err) => ({ error: err.message }));
|
||||
}
|
||||
|
||||
static async sendOTC(id: string, code: string) {
|
||||
return request("/api/user/twofactor/otc", this.gettok(), "PUT", { code, id }).then(({ login_exp, special_exp }) => {
|
||||
this.login.expires = login_exp;
|
||||
this.special.expires = special_exp;
|
||||
return {};
|
||||
}).catch(error => ({ error: error.message }))
|
||||
return request("/api/user/twofactor/otc", this.gettok(), "PUT", {
|
||||
code,
|
||||
id,
|
||||
})
|
||||
.then(({ login_exp, special_exp }) => {
|
||||
this.login.expires = login_exp;
|
||||
this.special.expires = special_exp;
|
||||
return {};
|
||||
})
|
||||
.catch((error) => ({ error: error.message }));
|
||||
}
|
||||
|
||||
static finish() {
|
||||
let d = new Date()
|
||||
d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000)); //Keep the username 30 days
|
||||
let d = new Date();
|
||||
d.setTime(d.getTime() + 30 * 24 * 60 * 60 * 1000); //Keep the username 30 days
|
||||
setCookie("username", this.username, d.toUTCString());
|
||||
|
||||
setCookie("login", this.login.token, new Date(this.login.expires).toUTCString());
|
||||
setCookie("special", this.special.token, new Date(this.special.expires).toUTCString());
|
||||
setCookie(
|
||||
"login",
|
||||
this.login.token,
|
||||
new Date(this.login.expires).toUTCString()
|
||||
);
|
||||
setCookie(
|
||||
"special",
|
||||
this.special.token,
|
||||
new Date(this.special.expires).toUTCString()
|
||||
);
|
||||
|
||||
let url = new URL(window.location.href);
|
||||
let state = url.searchParams.get("state")
|
||||
let red = "/"
|
||||
let state = url.searchParams.get("state");
|
||||
let red = "/";
|
||||
|
||||
if (state) {
|
||||
let base64 = url.searchParams.get("base64")
|
||||
if (base64)
|
||||
red = atob(state)
|
||||
else
|
||||
red = state
|
||||
let base64 = url.searchParams.get("base64");
|
||||
if (base64) red = atob(state);
|
||||
else red = state;
|
||||
}
|
||||
setTimeout(() => window.location.href = red, 200);
|
||||
setTimeout(() => (window.location.href = red), 200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
import App from './App.svelte';
|
||||
|
||||
import App from "./App.svelte";
|
||||
|
||||
var app = new App({
|
||||
target: document.getElementById("content")
|
||||
target: document.getElementById("content"),
|
||||
});
|
||||
|
||||
export default app;
|
||||
export default app;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import App from './App.svelte';
|
||||
import App from "./App.svelte";
|
||||
|
||||
var app = new App({
|
||||
target: document.getElementById("content")
|
||||
target: document.getElementById("content"),
|
||||
});
|
||||
|
||||
export default app;
|
||||
export default app;
|
||||
|
3199
src/cleave.js
3199
src/cleave.js
File diff suppressed because it is too large
Load Diff
@ -1,15 +1,15 @@
|
||||
export function setCookie(cname: string, cvalue: string, exdate: string) {
|
||||
const expires = exdate ? `;expires=${exdate}` : "";
|
||||
document.cookie = `${cname}=${cvalue}${expires};path=/;`
|
||||
document.cookie = `${cname}=${cvalue}${expires};path=/;`;
|
||||
}
|
||||
|
||||
export function getCookie(cname: string) {
|
||||
const name = cname + "=";
|
||||
const dc = decodeURIComponent(document.cookie);
|
||||
const ca = dc.split(';');
|
||||
const ca = dc.split(";");
|
||||
for (let i = 0; i < ca.length; i++) {
|
||||
let c = ca[i];
|
||||
while (c.charAt(0) == ' ') {
|
||||
while (c.charAt(0) == " ") {
|
||||
c = c.substring(1);
|
||||
}
|
||||
if (c.indexOf(name) == 0) {
|
||||
@ -17,4 +17,4 @@ export function getCookie(cname: string) {
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
const elements = new WeakSet();
|
||||
|
||||
function check() {
|
||||
document.querySelectorAll(".floating>input").forEach(e => {
|
||||
document.querySelectorAll(".floating>input").forEach((e) => {
|
||||
if (elements.has(e)) return;
|
||||
elements.add(e);
|
||||
|
||||
@ -10,26 +10,25 @@
|
||||
console.log("Check State");
|
||||
if (e.value !== "") {
|
||||
if (e.classList.contains("used")) return;
|
||||
e.classList.add("used")
|
||||
e.classList.add("used");
|
||||
} else {
|
||||
if (e.classList.contains("used")) e.classList.remove("used")
|
||||
if (e.classList.contains("used")) e.classList.remove("used");
|
||||
}
|
||||
}
|
||||
|
||||
e.addEventListener("change", () => checkState())
|
||||
checkState()
|
||||
})
|
||||
};
|
||||
e.addEventListener("change", () => checkState());
|
||||
checkState();
|
||||
});
|
||||
}
|
||||
|
||||
const observer = new MutationObserver((mutations) => {
|
||||
check();
|
||||
});
|
||||
|
||||
|
||||
// Start observing the target node for configured mutations
|
||||
observer.observe(window.document, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
subtree: true,
|
||||
});
|
||||
check();
|
||||
})()
|
||||
})();
|
||||
|
@ -4,7 +4,14 @@ import { getCookie } from "./cookie";
|
||||
// const baseURL = "http://localhost:3000";
|
||||
const baseURL = "";
|
||||
|
||||
export default async function request(endpoint: string, parameters: { [key: string]: string } = {}, method: "GET" | "POST" | "DELETE" | "PUT" = "GET", body?: any, authInParam = false, redirect = false) {
|
||||
export default async function request(
|
||||
endpoint: string,
|
||||
parameters: { [key: string]: string } = {},
|
||||
method: "GET" | "POST" | "DELETE" | "PUT" = "GET",
|
||||
body?: any,
|
||||
authInParam = false,
|
||||
redirect = false
|
||||
) {
|
||||
let pairs = [];
|
||||
|
||||
if (authInParam) {
|
||||
@ -26,19 +33,23 @@ export default async function request(endpoint: string, parameters: { [key: stri
|
||||
body: JSON.stringify(body),
|
||||
credentials: "same-origin",
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
"content-type": "application/json",
|
||||
},
|
||||
}).then(e => {
|
||||
if (e.status !== 200) throw new Error(e.statusText)
|
||||
return e.json()
|
||||
}).then(data => {
|
||||
if (data.error) {
|
||||
if (redirect && data.additional && data.additional.auth) {
|
||||
let state = btoa(window.location.pathname + window.location.hash);
|
||||
window.location.href = `/login?state=${state}&base64=true`;
|
||||
}
|
||||
return Promise.reject(new Error(data.error))
|
||||
}
|
||||
return data;
|
||||
})
|
||||
}
|
||||
.then((e) => {
|
||||
if (e.status !== 200) throw new Error(e.statusText);
|
||||
return e.json();
|
||||
})
|
||||
.then((data) => {
|
||||
if (data.error) {
|
||||
if (redirect && data.additional && data.additional.auth) {
|
||||
let state = btoa(
|
||||
window.location.pathname + window.location.hash
|
||||
);
|
||||
window.location.href = `/login?state=${state}&base64=true`;
|
||||
}
|
||||
return Promise.reject(new Error(data.error));
|
||||
}
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
485
src/sha512.js
485
src/sha512.js
File diff suppressed because one or more lines are too long
@ -3,4 +3,4 @@
|
||||
"module": "esnext",
|
||||
"sourceMap": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user