tucmc-auth
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -62,1 +62,7 @@ # Changelog | ||
## [0.1.1] - 2020-08-20 | ||
## Added | ||
- Minified css for plainJs version | ||
- Check client token before sending any request to server |
import React, { useContext, useState, useEffect } from 'react'; | ||
import FingerprintJS from '@fingerprintjs/fingerprintjs'; | ||
import * as crypto from 'crypto'; | ||
@@ -369,2 +370,8 @@ /*! ***************************************************************************** | ||
var checkToken = function () { | ||
var hostname = window.location.origin; | ||
var hashed = crypto.createHash("SHA256").update(hostname).digest("base64"); | ||
return token.includes(hashed); | ||
}; | ||
var signin = function () { | ||
@@ -377,2 +384,7 @@ if (loading) return; | ||
if (!checkToken()) { | ||
console.log("invalid_client_token"); | ||
return; | ||
} | ||
var data = window.localStorage.getItem("data"); | ||
@@ -379,0 +391,0 @@ if (data) return; |
@@ -71,6 +71,25 @@ function init() { | ||
signin() { | ||
async _checkToken() { | ||
const hostname = window.location.origin | ||
const arrayBuffer = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(hostname)) | ||
function _arrayBufferToBase64( buffer ) { | ||
let binary = '' | ||
let bytes = new Uint8Array( buffer ) | ||
let len = bytes.byteLength | ||
for (let i = 0; i < len; i++) { | ||
binary += String.fromCharCode( bytes[ i ] ) | ||
} | ||
return window.btoa( binary ) | ||
} | ||
return this.TOKEN.includes(_arrayBufferToBase64(arrayBuffer)) | ||
} | ||
async signin() { | ||
const data = window.localStorage.getItem("data") | ||
if (data) return | ||
if (this.prevPop) this.prevPop.close() | ||
if (!await this._checkToken()) {console.log("invalid_client_token"); return;} | ||
this._setLoading() | ||
@@ -77,0 +96,0 @@ |
@@ -1,1 +0,1 @@ | ||
function init(){FingerprintJS.load().then(e=>e.get()).then(e=>{window.fingerPrint=e.visitorId})}class TUCMCAuth{constructor(e){this.TOKEN=e,this.prevPop=null,window.addEventListener("load",()=>{document.getElementById("login_with_TUCMC").addEventListener("click",()=>{this.signin()})})}signout(){window.localStorage.setItem("data",""),window.location.reload()}_setLoading(){document.getElementById("login_with_TUCMC").innerHTML='<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; shape-rendering: auto;width: 40px; height: 26px" viewBox="0 0 100 80" preserveAspectRatio="xMidYMid">\n <circle cx="84" cy="50" r="10" fill="#ffffff">\n <animate attributeName="r" repeatCount="indefinite" dur="0.7142857142857142s" calcMode="spline" keyTimes="0;1" values="10;0"\n keySplines="0 0.5 0.5 1" begin="0s"/>\n <animate attributeName="fill" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="discrete" keyTimes="0;0.25;0.5;0.75;1"\n values="#ffffff;#ffffff;#ffffff;#ffffff;#ffffff" begin="0s"/>\n </circle>\n <circle cx="16" cy="50" r="10" fill="#ffffff">\n <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"/>\n <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"/>\n </circle>\n <circle cx="50" cy="50" r="10" fill="#ffffff">\n <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.7142857142857142s"/>\n <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.7142857142857142s"/>\n </circle>\n <circle cx="84" cy="50" r="10" fill="#ffffff">\n <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.4285714285714284s"/>\n <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.4285714285714284s"/>\n </circle>\n <circle cx="16" cy="50" r="10" fill="#ffffff">\n <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-2.142857142857143s"/>\n <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-2.142857142857143s"/>\n </circle>\n </svg>',document.getElementById("login_with_TUCMC").style.padding="0.15rem 4.71rem",setTimeout(()=>{this._resetLoading()},18e4)}_resetLoading(){document.getElementById("login_with_TUCMC").innerHTML="Login with TUCMC",document.getElementById("login_with_TUCMC").style.padding="0.5rem 2rem"}signin(){if(window.localStorage.getItem("data"))return;this.prevPop&&this.prevPop.close(),this._setLoading();const e=window.open("https://account.triamudom.club/auth","_blank","width=492,height=740");this.prevPop=e;const t=setInterval(()=>{this.prevPop.closed&&(!async function(e,t){const n=await fetch("https://account.triamudom.club/api/token",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"fetchAuthToken",authToken:window.sessionStorage.getItem("authToken"),reqToken:e,fp:fingerPrint})}),i=await n.json();i.status&&(window.sessionStorage.setItem("authToken",""),window.localStorage.setItem("data",JSON.stringify(i.data.data)),window.location.reload()),t()}(this.TOKEN,this._resetLoading),clearInterval(t))},500);(async function(e){const t=await fetch("https://account.triamudom.club/api/token",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"genAuthToken",reqToken:e,fp:fingerPrint})});return await t.json()})(this.TOKEN).then(t=>{t.status&&(window.sessionStorage.setItem("authToken",t.data.authToken),e.location.replace(`https://account.triamudom.club/auth?authToken=${t.data.authToken}`))})}user(){const e=window.localStorage.getItem("data");if(e){return JSON.parse(e).data}return null}} | ||
function init(){const fp=FingerprintJS.load();fp.then(fp=>fp.get()).then(result=>{window.fingerPrint=result.visitorId})}class TUCMCAuth{constructor(TOKEN){this.TOKEN=TOKEN;this.prevPop=null;window.addEventListener("load",()=>{document.getElementById("login_with_TUCMC").addEventListener("click",()=>{this.signin()})})}signout(){window.localStorage.setItem("data","");window.location.reload()}_setLoading(){document.getElementById("login_with_TUCMC").innerHTML='<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; shape-rendering: auto;width: 40px; height: 26px" viewBox="0 0 100 80" preserveAspectRatio="xMidYMid">\n'+' <circle cx="84" cy="50" r="10" fill="#ffffff">\n'+' <animate attributeName="r" repeatCount="indefinite" dur="0.7142857142857142s" calcMode="spline" keyTimes="0;1" values="10;0"\n'+' keySplines="0 0.5 0.5 1" begin="0s"/>\n'+' <animate attributeName="fill" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="discrete" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="#ffffff;#ffffff;#ffffff;#ffffff;#ffffff" begin="0s"/>\n'+" </circle>\n"+' <circle cx="16" cy="50" r="10" fill="#ffffff">\n'+' <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"/>\n'+' <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="0s"/>\n'+" </circle>\n"+' <circle cx="50" cy="50" r="10" fill="#ffffff">\n'+' <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.7142857142857142s"/>\n'+' <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-0.7142857142857142s"/>\n'+" </circle>\n"+' <circle cx="84" cy="50" r="10" fill="#ffffff">\n'+' <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.4285714285714284s"/>\n'+' <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-1.4285714285714284s"/>\n'+" </circle>\n"+' <circle cx="16" cy="50" r="10" fill="#ffffff">\n'+' <animate attributeName="r" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="0;0;10;10;10" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-2.142857142857143s"/>\n'+' <animate attributeName="cx" repeatCount="indefinite" dur="2.8571428571428568s" calcMode="spline" keyTimes="0;0.25;0.5;0.75;1"\n'+' values="16;16;16;50;84" keySplines="0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1;0 0.5 0.5 1" begin="-2.142857142857143s"/>\n'+" </circle>\n"+" </svg>";document.getElementById("login_with_TUCMC").style.padding="0.15rem 4.71rem";setTimeout(()=>{this._resetLoading()},3*60*1e3)}_resetLoading(){document.getElementById("login_with_TUCMC").innerHTML="Login with TUCMC";document.getElementById("login_with_TUCMC").style.padding="0.5rem 2rem"}async _checkToken(){const hostname=window.location.origin;const arrayBuffer=await crypto.subtle.digest("SHA-256",(new TextEncoder).encode(hostname));function _arrayBufferToBase64(buffer){let binary="";let bytes=new Uint8Array(buffer);let len=bytes.byteLength;for(let i=0;i<len;i++){binary+=String.fromCharCode(bytes[i])}return window.btoa(binary)}return this.TOKEN.includes(_arrayBufferToBase64(arrayBuffer))}async signin(){const data=window.localStorage.getItem("data");if(data)return;if(this.prevPop)this.prevPop.close();if(!await this._checkToken()){console.log("invalid_client_token");return}this._setLoading();const wid=window.open("https://account.triamudom.club/auth","_blank","width=492,height=740");this.prevPop=wid;async function _fetchToken(token,resetLoading){const res=await fetch(`https://account.triamudom.club/api/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"fetchAuthToken",authToken:window.sessionStorage.getItem("authToken"),reqToken:token,fp:fingerPrint})});const jsonResult=await res.json();if(jsonResult.status){window.sessionStorage.setItem("authToken","");window.localStorage.setItem("data",JSON.stringify(jsonResult.data.data));window.location.reload()}resetLoading()}const inter=setInterval(()=>{if(this.prevPop.closed){_fetchToken(this.TOKEN,this._resetLoading);clearInterval(inter)}},500);async function _genToken(token){const res=await fetch(`https://account.triamudom.club/api/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({action:"genAuthToken",reqToken:token,fp:fingerPrint})});return await res.json()}_genToken(this.TOKEN).then(jsonResult=>{if(jsonResult.status){window.sessionStorage.setItem("authToken",jsonResult.data.authToken);wid.location.replace(`https://account.triamudom.club/auth?authToken=${jsonResult.data.authToken}`)}})}user(){const data=window.localStorage.getItem("data");if(data){const parsed=JSON.parse(data);return parsed.data}else{return null}}} |
{ | ||
"name": "tucmc-auth", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"module": "dist/index.es.js", | ||
@@ -9,6 +9,5 @@ "types": "dist/index.d.ts", | ||
], | ||
"description": "", | ||
"description": "Easily secure your website with authentication system.", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"publish": "npm publish" | ||
"build": "rollup -c" | ||
}, | ||
@@ -15,0 +14,0 @@ "keywords": [], |
@@ -50,3 +50,3 @@ # TUCMC Auth API | ||
``` | ||
CSS: https://cdn.jsdelivr.net/npm/tucmc-auth@latest/dist/script/auth-style.css | ||
CSS: https://cdn.jsdelivr.net/npm/tucmc-auth@latest/dist/script/auth-style.min.css | ||
Script: https://cdn.jsdelivr.net/npm/tucmc-auth@latest/dist/script/auth-lib.min.js | ||
@@ -58,3 +58,3 @@ ``` | ||
```html | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tucmc-auth@latest/dist/script/auth-style.css"/> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tucmc-auth@latest/dist/script/auth-style.min.css"/> | ||
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/tucmc-auth@latest/dist/script/auth-lib.min.js"></script> | ||
@@ -61,0 +61,0 @@ <script async src="//cdn.jsdelivr.net/npm/@fingerprintjs/fingerprintjs@3/dist/fp.min.js" onload="init()"></script> |
Sorry, the diff of this file is not supported yet
31691
9
543