Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
[ES6 - JS] Secure Session Management
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="MayoMayn" data-source="post: 404762" data-attributes="member: 71840"><p>Well, I saw some folks on here who wanted to get some knowledge about JavaScript from scratch.</p><p>I wrote two components for my Electron app, and I then decided just to release them as a separate thread so that other people can enjoy this.</p><p></p><p>It uses the CryptoJS module by Google for encrypting and decrypting the local storage items.</p><p><strong>Auth.js</strong></p><p>[PHP]</p><p>import CryptoJS from 'crypto-js'</p><p>//const CryptoJS = require('crypto-js')</p><p>export default class Auth {</p><p></p><p> /**</p><p> * Encrypt a string, object or array</p><p> */</p><p> static encrypt(value) {</p><p> // if the value is an object, stringify it so it can be properly encrypted</p><p> if(typeof value === 'object')</p><p> value = JSON.stringify(value)</p><p> // encrypted the value using aes</p><p> let enc = CryptoJS.AES.encrypt(value, 'devbest') // < secret key</p><p> // incase you want to use the encrypted value it gets returned</p><p> return enc.toString()</p><p> }</p><p></p><p> /**</p><p> * Decrypt a string, object or array</p><p> */</p><p> static decrypt(value) {</p><p> // decrypts the specified value</p><p> let bytes = CryptoJS.AES.decrypt(value, 'devbest'), // < secret key</p><p> bytesToStr = bytes.toString(CryptoJS.enc.Utf8) // since it returns an object, we're only supposed to receive the decrypted part</p><p></p><p> // if the decrypted value is an object, parse it using json</p><p> return (typeof bytesToStr === 'object') ? JSON.parse(bytesToStr) : bytesToStr</p><p> }</p><p></p><p>}</p><p>[/PHP]</p><p></p><p><strong>Session.js</strong></p><p>[PHP]</p><p>import Auth from './auth'</p><p>// const Auth = require('./auth')</p><p></p><p>export default class Session {</p><p></p><p> // writes an object to the local storage</p><p> static write(obj) {</p><p> for(let key in obj) {</p><p> // encrypt the value using the auth class</p><p> obj[key] = Auth.encrypt(obj[key])</p><p> // write the encrypted value to the local storage</p><p> localStorage.setItem(key, obj[key])</p><p> }</p><p> </p><p> // return object incase you want to use it</p><p> return obj</p><p> }</p><p> </p><p> // reads a value from the storage</p><p> static read(key) {</p><p> // decrypt the local storage item using our decrypt method</p><p> return Auth.decrypt(localStorage.getItem(key))</p><p> }</p><p></p><p> // checks if several or one item exists in our local storage</p><p> static exists(arr) {</p><p> for(let key in arr) {</p><p> // checks if it exists</p><p> let exists = (localStorage.getItem(arr[key]) !== null)</p><p></p><p> if(!exists) return false</p><p> }</p><p></p><p> return true</p><p> }</p><p></p><p> // remove item in our local storage</p><p> static remove(arr) {</p><p> for(let key in arr) {</p><p> localStorage.removeItem(arr[key])</p><p> }</p><p> }</p><p></p><p>}</p><p>[/PHP]</p><p></p><p>Simple usage:</p><p>[PHP]</p><p>Session.write({</p><p> username: 'Sentinel',</p><p> bitch: 'Pettyjohn'</p><p>})</p><p></p><p>if(Session.exists(['username', 'bitch']) {</p><p> console.log(Session.read('username'))</p><p> // outputs Sentinel</p><p>}</p><p>[/PHP]</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 404762, member: 71840"] Well, I saw some folks on here who wanted to get some knowledge about JavaScript from scratch. I wrote two components for my Electron app, and I then decided just to release them as a separate thread so that other people can enjoy this. It uses the CryptoJS module by Google for encrypting and decrypting the local storage items. [B]Auth.js[/B] [PHP] import CryptoJS from 'crypto-js' //const CryptoJS = require('crypto-js') export default class Auth { /** * Encrypt a string, object or array */ static encrypt(value) { // if the value is an object, stringify it so it can be properly encrypted if(typeof value === 'object') value = JSON.stringify(value) // encrypted the value using aes let enc = CryptoJS.AES.encrypt(value, 'devbest') // < secret key // incase you want to use the encrypted value it gets returned return enc.toString() } /** * Decrypt a string, object or array */ static decrypt(value) { // decrypts the specified value let bytes = CryptoJS.AES.decrypt(value, 'devbest'), // < secret key bytesToStr = bytes.toString(CryptoJS.enc.Utf8) // since it returns an object, we're only supposed to receive the decrypted part // if the decrypted value is an object, parse it using json return (typeof bytesToStr === 'object') ? JSON.parse(bytesToStr) : bytesToStr } } [/PHP] [B]Session.js[/B] [PHP] import Auth from './auth' // const Auth = require('./auth') export default class Session { // writes an object to the local storage static write(obj) { for(let key in obj) { // encrypt the value using the auth class obj[key] = Auth.encrypt(obj[key]) // write the encrypted value to the local storage localStorage.setItem(key, obj[key]) } // return object incase you want to use it return obj } // reads a value from the storage static read(key) { // decrypt the local storage item using our decrypt method return Auth.decrypt(localStorage.getItem(key)) } // checks if several or one item exists in our local storage static exists(arr) { for(let key in arr) { // checks if it exists let exists = (localStorage.getItem(arr[key]) !== null) if(!exists) return false } return true } // remove item in our local storage static remove(arr) { for(let key in arr) { localStorage.removeItem(arr[key]) } } } [/PHP] Simple usage: [PHP] Session.write({ username: 'Sentinel', bitch: 'Pettyjohn' }) if(Session.exists(['username', 'bitch']) { console.log(Session.read('username')) // outputs Sentinel } [/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[ES6 - JS] Secure Session Management
Top