/
identity.ts
207 lines (191 loc) · 7.8 KB
/
identity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { BaseClient } from './base';
import { ClientConfig } from '../models/clientConfig';
import {
VerifiableCredential,
RevokeVerificationBody,
VerifyJwtBody,
User,
UserType,
CredentialTypes,
VerifiableCredentialInternal,
IdentityDocument,
IdentityKeys,
UserSearchResponse
} from '@iota/is-shared-modules';
import { SearchCriteria } from '../models/searchCriteria';
export class IdentityClient extends BaseClient {
private baseUrl: string;
constructor(config: ClientConfig) {
super(config);
this.baseUrl = this.useGatewayUrl ? this.isGatewayUrl!! : this.ssiBridgeUrl!!;
this.baseUrl = this.baseUrl + `/api/${config.apiVersionSsiBridge}`
}
/**
* Create a new decentralized digital identity (DID). Identity DID document is signed and published to the ledger (IOTA Tangle). A digital identity can represent an individual, an organization or an object. The privateAuthKey controlling the identity is returned. It is recommended to securely (encrypt) store the privateAuthKey locally, since it is not stored on the APIs Bridge.
* @param username
* @param claimType defaults to UserType.Person
* @param claim
* @param hidden
* @returns
*/
async create(
username?: string,
claimType = UserType.Person,
claim?: any,
hidden?: boolean
): Promise<IdentityKeys> {
return await this.post(`${this.baseUrl}/identities/create`, {
username,
hidden,
claim: {
...claim,
type: claimType
}
});
}
/**
* Search for identities in the system and returns a list of existing identities.
* @param username
* @returns
*/
async search({
type,
username,
creator,
registrationDate,
asc,
limit,
index
}: SearchCriteria): Promise<UserSearchResponse[]> {
const param = registrationDate != undefined ? { 'registration-date': registrationDate } : {};
return await this.get(`${this.baseUrl}/identities/search`, {
type,
username,
creator,
...param,
asc,
limit,
index
});
}
/**
* Get information (including attached credentials) about a specific identity using the identity-id (DID identifier).
* @param id
* @returns
*/
async find(id: string): Promise<User> {
return await this.get(`${this.baseUrl}/identities/identity/${id}`, {});
}
/**
* Register an existing identity into the Bridge. This can be used if the identity already exists or it was only created locally. Registering an identity in the Bridge makes it possible to search for it by using some of the identity attributes, i.e., the username.
* @param identity
* @returns
*/
async add(identity: User): Promise<null> {
return this.post(`${this.baseUrl}/identities/identity`, identity);
}
/**
* Update claim of a registered identity.
* @param identity
* @returns
*/
async update(identity: User): Promise<null> {
return this.put(`${this.baseUrl}/identities/identity`, identity);
}
/**
* Removes an identity from the Bridge. An identity can only delete itself and is not able to delete other identities. Administrators are able to remove other identities. The identity cannot be removed from the immutable IOTA Tangle but only at the Bridge. Also the identity credentials will remain and the identity is still able to interact with other bridges.
* @param id
* @param revokeCredentials
* @returns Null
*/
async remove(id: string, revokeCredentials: boolean = false): Promise<null> {
return this.delete(`${this.baseUrl}/identities/identity/${id}`, {
'revoke-credentials': revokeCredentials
});
}
/**
* Get the latest version of an identity document (DID) from the IOTA Tangle.
* @param id
* @returns
*/
async latestDocument(id: string): Promise<{ document: IdentityDocument; messageId: string }> {
return await this.get(`${this.baseUrl}/verification/latest-document/${id}`);
}
/**
* Adds Trusted Root identity identifiers (DIDs). Trusted roots are DIDs of identities which are trusted by the Bridge. This identity DIDs can be DIDs of other organizations. By adding them to the list Trusted Roots their Verifiable Credentials (VCs) are automatically trusted when checking at the Bridge.
* @param trustedAuthority
* @returns
*/
async addTrustedAuthority(trustedRootId: string): Promise<null> {
return await this.post(`${this.baseUrl}/verification/trusted-roots`, { trustedRootId });
}
/**
* Returns a list of Trusted Root identity identifiers (DIDs). Trusted roots are DIDs of identities which are trusted by the Bridge. This identity DIDs can be DIDs of other organizations. By adding them to the list Trusted Roots their Verifiable Credentials (VCs) are automatically trusted when checking at the Bridge.
* @returns
*/
async getTrustedAuthorities(): Promise<string[]> {
return await this.get(`${this.baseUrl}/verification/trusted-roots`);
}
/**
* Remove Trusted Root identity identifiers (DIDs). Trusted roots are DIDs of identities which are trusted by the Bridge. This identity DIDs can be DIDs of other organizations. By adding them to the list Trusted Roots their Verifiable Credentials (VCs) are automatically trusted when checking at the Bridge.
* @param trustedAuthorityId
* @returns
*/
async removeTrustedAuthority(trustedAuthorityId: string): Promise<null> {
return await this.delete(
`${this.baseUrl}/verification/trusted-roots/${trustedAuthorityId}`,
{}
);
}
/**
* Verify the authenticity of an identity (of an individual, organization or object) and issue a credential stating the identity verification status. Only previously verified identities (based on a network of trust) with assigned privileges can verify other identities. Having a verified identity provides the opportunity for other identities to identify and verify a the entity they interact to.
* @param initiatorVC
* @param targetDid
* @param claim
* @returns
*/
async createCredential(
initiatorVC: VerifiableCredentialInternal | undefined,
targetDid: string,
credentialType: CredentialTypes | string,
claimType: UserType,
claim?: any
): Promise<VerifiableCredential> {
let body = {
subject: {
id: targetDid,
credentialType,
claim: {
type: claimType,
...claim
}
},
initiatorVC: initiatorVC
};
return await this.post(`${this.baseUrl}/verification/create-credential`, body);
}
/**
* Check the verifiable credential of an identity. Validates the signed verifiable credential against the Issuer information stored onto the IOTA Tangle and checks if the issuer identity (DID) contained in the credential is from a trusted root.
* @param credential
* @returns
*/
async checkCredential(credential: VerifiableCredential): Promise<{ isVerified: boolean }> {
return await this.post(`${this.baseUrl}/verification/check-credential`, credential);
}
/**
* Revoke one specific verifiable credential of an identity. In the case of individual and organization identities the reason could be that the user has left the organization. Only organization admins (with verified identities) or the identity owner itself can do that.
* @param credential
* @returns
*/
async revokeCredential(credential: RevokeVerificationBody): Promise<null> {
return await this.post(`${this.baseUrl}/verification/revoke-credential`, credential);
}
/**
* Check the verifiable credential of an identity. Validates the signed verifiable credential against the Issuer information stored onto the IOTA Tangle and checks if the issuer identity (DID) contained in the credential is from a trusted root.
* @param credential
* @returns
*/
async verifyJwt(jwt: VerifyJwtBody): Promise<{ isValid: boolean; error?: string }> {
return await this.post(`${this.baseUrl}/authentication/verify-jwt`, jwt);
}
}