state-tokens
Version:
Classes and type definitions for state transition tokens for libraries that manage tokens as objects in sets and tables.
237 lines (190 loc) • 6.83 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: transition_token.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: transition_token.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>
/**
* Formalize the definition of a transition object.
* A Transition Object is a record that pairs a transition token with a session token
* and also that provides basic information about server side and client side operations
* necessary to coordinate a transition of state between the two.
*/
class TransitionObject {
constructor(type) {
//
this.token ="nothing"
this.session_token = undefined
this.secondary_action = false
this.type = type
//
this.elements = {}
this.amelioritive_action = false
this.extras = false
}
/**
* Returns an object without class information containing a minimal set of top level fields refering to current values.
* If the *extras* parameter is supplied, this method assumes that it is an array of field names.
* The extra field will be added to the object produced by this method.
* @param {string[]} [extras]
*
* @returns {object}
*/
to_object() {
let self = this
let obj = {}
for ( let fld of [ "token", "secondary_action", "session_token", "type" ] ) {
obj[fld] = self[fld]
}
if ( this.extras && Array.isArray(this.extras) ) {
for ( let fld of extras ) {
obj[fld] = self[fld]
}
}
}
/**
* Fix the token of this object.
* @param {string} token
*/
set_token(token) {
this.token = token
}
/**
* Set the session id with the expectation that this assertion declares that the token belong to a session.
* @param {string} sess_id
*/
set_session(sess_id) {
this.session_token = sess_id
}
/**
* If this token has been assigned to a session, then erase the session identifier and replace it with
* a message of abandonment, 'sess+gone'.
* @param {string} token
*/
clear_session() {
this.session_token = 'sess+gone'
}
/**
* Extend the elements object with more fields and info. These may overwrite existing fields.
* @param {object} obj
*/
add_to_elements(obj) {
this.elements = Object.assign(this.elements,obj)
}
/**
* The fields returned from `to_object` are by default just a few provided by the base classs.
* For **TransitionObject** these are just the following: "token", "secondary_action", "session_token", "type"
* Use this method to add more field to be included in the `to_object` report.
* @param {string} fld
*/
add_extra_field(fld) {
if ( !this.extras ) this.extras = []
if ( this.extras.indexOf(fld) < 0 ) {
this.extras.push(fld)
}
}
/**
* Call `add_extra_field` for each member of the `fld_list` parameter.
* @param {string[]} fld_list
*/
add_extra_fields(fld_list) {
for ( let fld of fld_list ) {
this.add_extra_field(fld)
}
}
}
/**
* Add fields for login transtion objects
*
* Adds these fields: `_t_u_key`,`user_key`, `strategy'
*
* The strategy defaults to `ucwid` and distributed ID *ucwid* is a kind of [cwid](https://www.npmjs.com/package/cwid).
* This indicates to some login processor that the particular kind of ID will be used to identify a user.
* Applications may use other strategies or several. This type merely provides the placeholder.
*
* In [copious-transition-applications](), the field `_t_u_key` is used to identify which JSON field of some object,
* such as a web app POST parameters will be contain the information to be stored in the `user_key`, which itself may eventually
* be a key into some DB. The key, `_db_session_key` provides a similar function and is often the same as `_t_u_key`.
*
* @extends TransitionObject
*
*/
class LoginTransitionObject extends TransitionObject {
constructor(type) {
super(type)
this.extras = [ '_t_u_key','user_key', 'strategy' ]
this._t_u_key = false
this.user_key = false
this.strategy = 'ucwid'
this._db_session_key = false
}
}
/**
* Add fields for logout transtion objects
*
* @extends TransitionObject
*/
class LogoutTransitionObject extends TransitionObject {
constructor(type) {
super(type)
this.extras = []
}
}
/**
* Add fields for registration transtion objects
*
* @extends TransitionObject
*/
class RegistrationTransitionObject extends TransitionObject {
constructor(type) {
super(type)
this.extras = []
}
}
/**
* Add fields for process transtion objects - the true transitions
*
* The **ProcessTransitionObject** defaults to `secondary_action` set to true.
* A field `asset_key` extend the base object and will be named by the transition parameter.
* The type of the transition object is `transition`.
*
* @extends TransitionObject
*/
class ProcessTransitionObject extends TransitionObject {
constructor(transition) {
super('transition')
this.secondary_action = true
this.asset_key = transition
this.extras = ['asset_key']
}
}
module.exports = {
TransitionObject, LoginTransitionObject, LogoutTransitionObject, RegistrationTransitionObject, ProcessTransitionObject
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="LoginTransitionObject.html">LoginTransitionObject</a></li><li><a href="LogoutTransitionObject.html">LogoutTransitionObject</a></li><li><a href="ProcessTransitionObject.html">ProcessTransitionObject</a></li><li><a href="RegistrationTransitionObject.html">RegistrationTransitionObject</a></li><li><a href="TransitionObject.html">TransitionObject</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Thu Jul 13 2023 16:36:02 GMT-0700 (Pacific Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>