2023-07-03 02:39:23 -06:00
|
|
|
/*
|
|
|
|
Copyright 2023 Yarmo Mackenbach
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2023-09-21 06:26:26 -06:00
|
|
|
import { Claim } from './claim.js'
|
|
|
|
|
2023-07-03 02:39:23 -06:00
|
|
|
/**
|
|
|
|
* @class
|
2024-01-27 10:00:55 -07:00
|
|
|
* @classdesc A persona with identity claims
|
2023-07-03 02:39:23 -06:00
|
|
|
* @example
|
|
|
|
* const claim = Claim('https://alice.tld', '123');
|
|
|
|
* const pers = Persona('Alice', 'About Alice', [claim]);
|
|
|
|
*/
|
2023-07-08 00:17:13 -06:00
|
|
|
export class Persona {
|
2023-07-03 02:39:23 -06:00
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {string} name - Name of the persona
|
2024-01-29 06:05:17 -07:00
|
|
|
* @param {Array<Claim>} claims - Claims of the persona
|
2023-07-09 03:42:21 -06:00
|
|
|
*/
|
|
|
|
constructor (name, claims) {
|
|
|
|
/**
|
|
|
|
* Identifier of the persona
|
|
|
|
* @type {string | null}
|
|
|
|
* @public
|
2023-07-03 02:39:23 -06:00
|
|
|
*/
|
2023-07-09 03:42:21 -06:00
|
|
|
this.identifier = null
|
2023-07-03 02:39:23 -06:00
|
|
|
/**
|
2023-07-09 03:42:21 -06:00
|
|
|
* Name to be displayed on the profile page
|
|
|
|
* @type {string}
|
|
|
|
* @public
|
|
|
|
*/
|
2023-07-03 02:39:23 -06:00
|
|
|
this.name = name
|
|
|
|
/**
|
2023-07-09 03:42:21 -06:00
|
|
|
* Email address of the persona
|
|
|
|
* @type {string | null}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.email = null
|
|
|
|
/**
|
|
|
|
* Description to be displayed on the profile page
|
|
|
|
* @type {string | null}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.description = null
|
|
|
|
/**
|
|
|
|
* URL to an avatar image
|
|
|
|
* @type {string | null}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.avatarUrl = null
|
2023-10-05 03:11:25 -06:00
|
|
|
/**
|
|
|
|
* Theme color
|
|
|
|
* @type {string | null}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.themeColor = null
|
2023-07-03 02:39:23 -06:00
|
|
|
/**
|
2023-07-09 03:42:21 -06:00
|
|
|
* List of identity claims
|
2024-01-29 06:05:17 -07:00
|
|
|
* @type {Array<Claim>}
|
2023-07-09 03:42:21 -06:00
|
|
|
* @public
|
|
|
|
*/
|
2023-07-03 02:39:23 -06:00
|
|
|
this.claims = claims
|
2023-07-09 03:42:21 -06:00
|
|
|
/**
|
|
|
|
* Has the persona been revoked
|
|
|
|
* @type {boolean}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
this.isRevoked = false
|
|
|
|
}
|
|
|
|
|
2023-09-21 06:26:26 -06:00
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Parse a JSON object and convert it into a persona
|
2023-09-21 06:26:26 -06:00
|
|
|
* @function
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {object} personaObject - JSON representation of a persona
|
|
|
|
* @param {number} profileVersion - Version of the Profile containing the persona
|
|
|
|
* @returns {Persona | Error} Parsed persona
|
2023-09-21 06:26:26 -06:00
|
|
|
* @example
|
|
|
|
* doip.Persona.fromJSON(JSON.stringify(persona), 2);
|
|
|
|
*/
|
|
|
|
static fromJSON (personaObject, profileVersion) {
|
|
|
|
/** @type {Persona} */
|
|
|
|
let persona
|
|
|
|
let result
|
|
|
|
|
|
|
|
if (typeof personaObject === 'object' && profileVersion) {
|
|
|
|
switch (profileVersion) {
|
|
|
|
case 2:
|
|
|
|
result = importJsonPersonaVersion2(personaObject)
|
|
|
|
if (result instanceof Error) {
|
|
|
|
throw result
|
|
|
|
}
|
|
|
|
persona = result
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Invalid persona version')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return persona
|
|
|
|
}
|
|
|
|
|
2023-07-09 03:42:21 -06:00
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Set the persona's identifier
|
2023-07-09 03:42:21 -06:00
|
|
|
* @function
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {string} identifier - Identifier of the persona
|
2023-07-09 03:42:21 -06:00
|
|
|
*/
|
|
|
|
setIdentifier (identifier) {
|
|
|
|
this.identifier = identifier
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Set the persona's description
|
2023-07-09 03:42:21 -06:00
|
|
|
* @function
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {string} description - Description of the persona
|
2023-07-09 03:42:21 -06:00
|
|
|
*/
|
|
|
|
setDescription (description) {
|
|
|
|
this.description = description
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Set the persona's email address
|
2023-07-09 03:42:21 -06:00
|
|
|
* @function
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {string} email - Email address of the persona
|
2023-07-09 03:42:21 -06:00
|
|
|
*/
|
|
|
|
setEmailAddress (email) {
|
|
|
|
this.email = email
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Set the URL to the persona's avatar
|
2023-07-09 03:42:21 -06:00
|
|
|
* @function
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {string} avatarUrl - URL to the persona's avatar
|
2023-07-09 03:42:21 -06:00
|
|
|
*/
|
|
|
|
setAvatarUrl (avatarUrl) {
|
|
|
|
this.avatarUrl = avatarUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Add a claim
|
2023-07-09 03:42:21 -06:00
|
|
|
* @function
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {Claim} claim - Claim to add
|
2023-07-09 03:42:21 -06:00
|
|
|
*/
|
|
|
|
addClaim (claim) {
|
|
|
|
this.claims.push(claim)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Revoke the persona
|
2023-07-09 03:42:21 -06:00
|
|
|
* @function
|
|
|
|
*/
|
|
|
|
revoke () {
|
|
|
|
this.isRevoked = true
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-27 10:00:55 -07:00
|
|
|
* Get a JSON representation of the persona
|
2023-07-09 03:42:21 -06:00
|
|
|
* @function
|
2024-01-27 10:00:55 -07:00
|
|
|
* @returns {object} JSON representation of the persona
|
2023-07-09 03:42:21 -06:00
|
|
|
*/
|
|
|
|
toJSON () {
|
|
|
|
return {
|
|
|
|
identifier: this.identifier,
|
|
|
|
name: this.name,
|
|
|
|
email: this.email,
|
|
|
|
description: this.description,
|
|
|
|
avatarUrl: this.avatarUrl,
|
2023-10-05 03:11:25 -06:00
|
|
|
themeColor: this.themeColor,
|
2023-07-09 03:42:21 -06:00
|
|
|
isRevoked: this.isRevoked,
|
|
|
|
claims: this.claims.map(x => x.toJSON())
|
|
|
|
}
|
2023-07-03 02:39:23 -06:00
|
|
|
}
|
|
|
|
}
|
2023-09-21 06:26:26 -06:00
|
|
|
|
|
|
|
/**
|
2024-01-27 11:36:19 -07:00
|
|
|
* @ignore
|
2024-01-27 10:00:55 -07:00
|
|
|
* @param {object} personaObject - JSON representation of a persona
|
|
|
|
* @returns {Persona | Error} Parsed persona
|
2023-09-21 06:26:26 -06:00
|
|
|
*/
|
|
|
|
function importJsonPersonaVersion2 (personaObject) {
|
|
|
|
const claims = personaObject.claims.map(x => Claim.fromJSON(x))
|
|
|
|
|
|
|
|
const persona = new Persona(personaObject.name, claims)
|
|
|
|
|
|
|
|
persona.identifier = personaObject.identifier
|
|
|
|
persona.email = personaObject.email
|
|
|
|
persona.description = personaObject.description
|
|
|
|
persona.avatarUrl = personaObject.avatarUrl
|
2023-10-05 03:11:25 -06:00
|
|
|
persona.themeColor = personaObject.avatarUrl
|
2023-09-21 06:26:26 -06:00
|
|
|
persona.isRevoked = personaObject.isRevoked
|
|
|
|
|
|
|
|
return persona
|
|
|
|
}
|