mirror of
https://codeberg.org/keyoxide/doipjs.git
synced 2024-12-22 22:49:28 -07:00
feat: unify fromJSON() for Profile, Persona and Claim
This commit is contained in:
parent
4aba882b1b
commit
cb397e42b7
3 changed files with 105 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue