calimero-auth-sdk
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -5,2 +5,4 @@ "use strict"; | ||
const js_sha256_1 = require("js-sha256"); | ||
const axios = require("axios"); | ||
const randomstring = require("randomstring"); | ||
//Max valid period for a token would be 30 days | ||
@@ -35,2 +37,5 @@ exports.MAX_CALIMERO_TOKEN_DURATION = 1000 * 60 * 60 * 24 * 30; | ||
isDurationValid() { | ||
if (this.to === null) { | ||
return true; | ||
} | ||
const isOrderRight = this.from < this.to; | ||
@@ -58,3 +63,3 @@ const isDurationWithinBounds = (this.to.getTime() - this.from.getTime()) <= exports.MAX_CALIMERO_TOKEN_DURATION; | ||
blockId: this.walletData.blockId, | ||
publicKey: Buffer.from(this.walletData.publicKey.data).toString('base64'), | ||
publicKey: Buffer.from(this.walletData.publicKey.data).toString("base64"), | ||
keyType: this.walletData.publicKey.keyType, | ||
@@ -70,2 +75,42 @@ }; | ||
exports.CalimeroToken = CalimeroToken; | ||
class CalimeroAuth { | ||
static isSignedIn() { | ||
return localStorage.getItem("calimeroToken") !== null; | ||
} | ||
static signIn(config) { | ||
if (!localStorage.getItem("calimeroSecret")) { | ||
localStorage.setItem("calimeroSecret", randomstring.generate({ length: 128 })); | ||
localStorage.setItem("calimeroSecretHash", js_sha256_1.sha256.update(localStorage.getItem("calimeroSecret")).toString()); | ||
const callbackUrl = encodeURIComponent(window.location.href); | ||
window.location.href = `${config.walletUrl}?message=${localStorage.getItem("calimeroSecretHash")}&callbackUrl=${callbackUrl}`; | ||
} | ||
const search = window.location.search; | ||
const params = new URLSearchParams(search); | ||
if (params.get("message") !== localStorage.getItem("calimeroSecretHash")) { | ||
console.log("Wallet Message is not the same as the calimeroSecretHash"); | ||
return; | ||
} | ||
axios.post(config.authServiceUrl + "/api/v1/authenticate", { | ||
shardId: config.shardId, | ||
accountId: params.get("accountId"), | ||
blockId: params.get("blockId"), | ||
publicKey: params.get("publicKey"), | ||
signature: params.get("signature"), | ||
calimeroSecret: localStorage.getItem("calimeroSecret"), | ||
calimeroSecretHash: localStorage.getItem("calimeroSecretHash") | ||
}).then((res) => { | ||
if (res.status == 200) { | ||
localStorage.setItem("calimeroToken", res.data.secretToken); | ||
} | ||
}).catch((err) => { | ||
console.error(err); | ||
}); | ||
window.location.href = "/"; | ||
} | ||
static signOut() { | ||
localStorage.removeItem("calimeroToken"); | ||
localStorage.removeItem("calimeroSecret"); | ||
localStorage.removeItem("calimeroSecretHash"); | ||
} | ||
} | ||
module.exports = { | ||
@@ -75,3 +120,4 @@ MAX_CALIMERO_TOKEN_DURATION: exports.MAX_CALIMERO_TOKEN_DURATION, | ||
CalimeroToken, | ||
CalimeroTokenData | ||
CalimeroTokenData, | ||
CalimeroAuth, | ||
}; |
{ | ||
"name": "calimero-auth-sdk", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "type": "commonjs", |
42977
144