-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApiClient.js
115 lines (101 loc) · 2.97 KB
/
ApiClient.js
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
import RNFetchBlob from 'rn-fetch-blob'
// THIS CODE IS PROVIDED FOR DEMO PURPOSES ONLY AND SHOULD NOT BE USED IN
// PRODUCTION! YOU SHOULD NEVER EMBED YOUR CREDENTIALS IN A PUBLIC APP RELEASE!
// THESE API CALLS SHOULD ONLY EVER BE MADE FROM YOUR BACK-END SERVER
export const CLAIM_TYPE_ENROL = 'enrol'
export const CLAIM_TYPE_VERIFY = 'verify'
export const ASSURANCE_TYPE_GENUINE_PRESENCE = 'genuine_presence'
export const ASSURANCE_TYPE_LIVENESS = 'liveness'
export const PHOTO_SOURCE_OPTICAL_ID = 'oid'
export const PHOTO_SOURCE_ELECTRONIC_ID = 'eid'
export default class ApiClient {
/* constructor(config) {
this.baseUrl = config.baseUrl.endsWith('/')
? config.baseUrl
: config.baseUrl + '/'
this.apiKey = config.apiKey
this.secret = config.secret
}*/
constructor(url, key, secret) {
this.baseUrl = url
this.apiKey = key
this.secret = secret
}
async getToken(assuranceType, claimType, userId) {
return await fetch(`${this.baseUrl}claim/${claimType}/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
api_key: this.apiKey,
secret: this.secret,
resource: 'com.iproov.sdk',
user_id: userId,
client: 'react-native',
assurance_type: assuranceType
})
})
}
async enrolPhoto(token, image, photoSource) {
const form = [
{ name: 'api_key', data: this.apiKey },
{ name: 'secret', data: this.secret },
{ name: 'rotation', data: '0' },
{ name: 'token', data: token },
{ name: 'source', data: photoSource },
{ name: 'image', filename: 'image.jpeg', type: 'image/jpeg', data: image }
]
return await RNFetchBlob.fetch(
'POST',
`${this.baseUrl}claim/enrol/image`,
{
'Content-Type': 'multipart/form-data'
},
form
)
}
async enrolPhotoAndGetVerifyToken(userId, image, photoSource) {
const response = await this.getToken(
ASSURANCE_TYPE_GENUINE_PRESENCE,
CLAIM_TYPE_ENROL,
userId
)
if (!response.ok) return response
const body = await response.json()
await this.enrolPhoto(body.token, image, photoSource)
return this.getToken(
ASSURANCE_TYPE_GENUINE_PRESENCE,
CLAIM_TYPE_VERIFY,
userId
)
}
async validate(token, userId) {
return await fetch(`${this.baseUrl}claim/verify/validate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
api_key: this.apiKey,
secret: this.secret,
user_id: userId,
token: token,
assurance_type: assuranceType,
ip: '127.0.0.1',
client: 'android'
})
})
}
async invalidate(token, reason) {
return await fetch(`${this.baseUrl}claim/${token}/invalidate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
reason: reason
})
})
}
}