feat: unify fromJSON() for Profile, Persona and Claim

This commit is contained in:
Yarmo Mackenbach 2023-09-21 14:26:26 +02:00
parent 4aba882b1b
commit cb397e42b7
No known key found for this signature in database
GPG key ID: 3C57D093219103A3
3 changed files with 105 additions and 2 deletions

View file

@ -79,10 +79,11 @@ export class Claim {
/**
* @function
* @param {object} claimObject
* @returns {Claim | Error}
* @example
* const claimAlt = doip.Claim(JSON.stringify(claim));
* doip.Claim.fromJSON(JSON.stringify(claim));
*/
static fromJson (claimObject) {
static fromJSON (claimObject) {
/** @type {Claim} */
let claim
let result

View file

@ -13,6 +13,8 @@ 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.
*/
import { Claim } from './claim.js'
/**
* A persona with identity claims
* @class
@ -72,6 +74,37 @@ export class Persona {
this.isRevoked = false
}
/**
* @function
* @param {object} personaObject
* @param {number} profileVersion
* @returns {Persona | Error}
* @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
}
/**
* @function
* @param {string} identifier
@ -136,3 +169,21 @@ export class Persona {
}
}
}
/**
* @param {object} personaObject
* @returns {Persona | Error}
*/
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
persona.isRevoked = personaObject.isRevoked
return persona
}

View file

@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { PublicKeyFetchMethod, PublicKeyEncoding, PublicKeyType } from './enums.js'
import { Persona } from './persona.js'
/**
* A profile of personas with identity claims
@ -135,6 +136,36 @@ export class Profile {
this.verifiers = []
}
/**
* @function
* @param {object} profileObject
* @returns {Profile | Error}
* @example
* doip.Profile.fromJSON(JSON.stringify(profile));
*/
static fromJSON (profileObject) {
/** @type {Profile} */
let profile
let result
if (typeof profileObject === 'object' && 'profileVersion' in profileObject) {
switch (profileObject.profileVersion) {
case 2:
result = importJsonProfileVersion2(profileObject)
if (result instanceof Error) {
throw result
}
profile = result
break
default:
throw new Error('Invalid profile version')
}
}
return profile
}
/**
* @function
* @param {string} name
@ -171,3 +202,23 @@ export class Profile {
}
}
}
/**
* @param {object} profileObject
* @returns {Profile | Error}
*/
function importJsonProfileVersion2 (profileObject) {
if (!('profileVersion' in profileObject && profileObject.profileVersion === 2)) {
return new Error('Invalid profile')
}
const personas = profileObject.personas.map(x => Persona.fromJSON(x, 2))
const profile = new Profile(profileObject.profileType, profileObject.identifier, personas)
profile.primaryPersonaIndex = profileObject.primaryPersonaIndex
profile.publicKey = profileObject.publicKey
profile.verifiers = profileObject.verifiers
return profile
}