2022-07-03 10:25:12 +00:00
|
|
|
import axios, { AxiosResponse, Method } from "axios";
|
2022-07-01 01:27:18 +00:00
|
|
|
import qs from "qs";
|
|
|
|
import { getCookie, removeCookie } from "typescript-cookie";
|
|
|
|
|
|
|
|
export interface Session {
|
|
|
|
user:string
|
|
|
|
xsrf:string
|
|
|
|
csrf:string
|
2022-07-03 10:25:12 +00:00
|
|
|
authed_request:(verb:Method,url:string,data:any)=>Promise<any>
|
2022-07-01 01:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-03 10:25:12 +00:00
|
|
|
export const SITE_ROOT = "/lifeto/"
|
|
|
|
export const API_ROOT = "api/lifeto/"
|
2022-07-01 01:27:18 +00:00
|
|
|
|
2022-07-03 10:25:12 +00:00
|
|
|
const login_endpoint = (name:string)=>{
|
|
|
|
return SITE_ROOT + name
|
2022-07-01 01:27:18 +00:00
|
|
|
}
|
2022-07-03 10:25:12 +00:00
|
|
|
const api_endpoint = (name:string)=>{
|
|
|
|
return SITE_ROOT+API_ROOT + name
|
|
|
|
}
|
|
|
|
|
2022-07-01 01:27:18 +00:00
|
|
|
|
|
|
|
export class LoginHelper {
|
|
|
|
user:string
|
|
|
|
pass:string
|
|
|
|
csrf?:string
|
|
|
|
constructor(user:string, pass:string){
|
|
|
|
this.user = user;
|
|
|
|
this.pass = pass;
|
|
|
|
}
|
|
|
|
login = async ():Promise<TokenSession> =>{
|
|
|
|
removeCookie("XSRF-TOKEN")
|
|
|
|
await sleep(1000)
|
2022-07-03 10:25:12 +00:00
|
|
|
return axios.get(login_endpoint("login"),{withCredentials:false})
|
2022-07-01 01:27:18 +00:00
|
|
|
.then(async (x)=>{
|
|
|
|
console.log(x)
|
|
|
|
if(x.data){
|
|
|
|
try{
|
|
|
|
this.csrf = x.data.split("csrf-token")[1].split('\">')[0].replace("\" content=\"",'')
|
|
|
|
}catch(e){
|
|
|
|
}
|
|
|
|
}
|
2022-07-03 10:25:12 +00:00
|
|
|
return axios.post(login_endpoint("login"),{
|
2022-07-01 01:27:18 +00:00
|
|
|
login:this.user,
|
|
|
|
password:this.pass,
|
|
|
|
redirectTo:"lifeto"
|
|
|
|
},{withCredentials:false})
|
|
|
|
.then(async (x)=>{
|
|
|
|
await sleep(100)
|
|
|
|
let xsrf= getCookie("XSRF-TOKEN")
|
2022-07-03 10:25:12 +00:00
|
|
|
return new TokenSession(this.user,this.csrf!, xsrf!)
|
2022-07-01 01:27:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const sleep= async(ms:number)=> {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
|
|
}
|
2022-07-03 10:25:12 +00:00
|
|
|
|
|
|
|
export class TokenSession implements Session {
|
2022-07-01 01:27:18 +00:00
|
|
|
csrf:string
|
|
|
|
xsrf:string
|
|
|
|
user:string
|
2022-07-03 10:25:12 +00:00
|
|
|
constructor(name:string, csrf:string, xsrf: string){
|
2022-07-01 01:27:18 +00:00
|
|
|
this.user = name
|
|
|
|
this.csrf = csrf
|
|
|
|
this.xsrf = xsrf;
|
|
|
|
}
|
|
|
|
|
2022-07-03 10:25:12 +00:00
|
|
|
authed_request = async (verb:string,url:string,data:any):Promise<AxiosResponse> => {
|
2022-07-01 01:27:18 +00:00
|
|
|
let promise
|
2022-07-03 10:25:12 +00:00
|
|
|
switch (verb.toLowerCase()){
|
2022-07-01 01:27:18 +00:00
|
|
|
case "post":
|
2022-07-03 10:25:12 +00:00
|
|
|
promise = axios.post(api_endpoint(url),data,this.genHeaders())
|
2022-07-01 01:27:18 +00:00
|
|
|
break;
|
|
|
|
case "postraw":
|
|
|
|
const querystring = qs.stringify(data)
|
2022-07-03 10:25:12 +00:00
|
|
|
promise = axios.post(api_endpoint(url),querystring,this.genHeaders())
|
2022-07-01 01:27:18 +00:00
|
|
|
break;
|
|
|
|
case "get":
|
|
|
|
default:
|
2022-07-03 10:25:12 +00:00
|
|
|
promise = axios.get(api_endpoint(url),this.genHeaders())
|
2022-07-01 01:27:18 +00:00
|
|
|
}
|
|
|
|
return promise.then(x=>{
|
|
|
|
if(x.data){
|
|
|
|
try{
|
|
|
|
this.xsrf = x.data.split("xsrf-token")[1].split('\">')[0].replace("\" content=\"",'')
|
|
|
|
}catch(e){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(x.headers['set-cookie']){
|
|
|
|
const cookies = x.headers['set-cookie'].map((y)=>{
|
|
|
|
return y.split("=")[1].split(";")[0];
|
|
|
|
})
|
|
|
|
this.xsrf = cookies[0]
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
})
|
|
|
|
}
|
|
|
|
genHeaders = ()=>{
|
|
|
|
const out = {
|
|
|
|
headers:{
|
2022-07-03 10:25:12 +00:00
|
|
|
Accept: "application/json",
|
2022-07-01 01:27:18 +00:00
|
|
|
"Update-Insecure-Requests": 1,
|
|
|
|
},
|
|
|
|
withCredentials:true
|
|
|
|
}
|
|
|
|
if(this.xsrf){
|
|
|
|
(out.headers as any)["X-XSRF-TOKEN"] = this.xsrf.replace("%3D","=")
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|