doipjs/src/persona.js

139 lines
2.8 KiB
JavaScript
Raw Normal View History

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.
*/
/**
* A persona with identity claims
* @class
* @constructor
* @public
* @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
/**
* @param {string} name
* @param {import('./claim.js').Claim[]} claims
*/
constructor (name, claims) {
/**
* Identifier of the persona
* @type {string | null}
* @public
2023-07-03 02:39:23 -06:00
*/
this.identifier = null
2023-07-03 02:39:23 -06:00
/**
* Name to be displayed on the profile page
* @type {string}
* @public
*/
2023-07-03 02:39:23 -06:00
this.name = name
/**
* 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-07-03 02:39:23 -06:00
/**
* List of identity claims
* @type {import('./claim.js').Claim[]}
* @public
*/
2023-07-03 02:39:23 -06:00
this.claims = claims
/**
* Has the persona been revoked
* @type {boolean}
* @public
*/
this.isRevoked = false
}
/**
* @function
* @param {string} identifier
*/
setIdentifier (identifier) {
this.identifier = identifier
}
/**
* @function
* @param {string} description
*/
setDescription (description) {
this.description = description
}
/**
* @function
* @param {string} email
*/
setEmailAddress (email) {
this.email = email
}
/**
* @function
* @param {string} avatarUrl
*/
setAvatarUrl (avatarUrl) {
this.avatarUrl = avatarUrl
}
/**
* @function
* @param {import('./claim.js').Claim} claim
*/
addClaim (claim) {
this.claims.push(claim)
}
/**
* @function
*/
revoke () {
this.isRevoked = true
}
/**
* Get a JSON representation of the Profile object
* @function
* @returns {object}
*/
toJSON () {
return {
identifier: this.identifier,
name: this.name,
email: this.email,
description: this.description,
avatarUrl: this.avatarUrl,
isRevoked: this.isRevoked,
claims: this.claims.map(x => x.toJSON())
}
2023-07-03 02:39:23 -06:00
}
}