@@ -7,9 +7,14 @@ import { tcrypto, utils } from '@tanker/crypto';
7
7
import { createIdentity } from '@tanker/identity' ;
8
8
import { uuid } from '@tanker/test-utils' ;
9
9
10
- import { AuthenticatedRequester } from './AuthenticatedRequester ' ;
10
+ import { requestTrustchaind , requestAdmindWithAuth } from './request ' ;
11
11
import { oidcSettings , storageSettings } from './config' ;
12
12
13
+ function toUnpaddedSafeBase64 ( str : Uint8Array ) : string {
14
+ const b64 = utils . toSafeBase64 ( str ) ;
15
+ return b64 . substring ( 0 , b64 . indexOf ( '=' ) ) ;
16
+ }
17
+
13
18
function makeRootBlock ( appKeyPair : Object ) {
14
19
const rootBlock = {
15
20
trustchain_id : new Uint8Array ( 0 ) ,
@@ -25,13 +30,11 @@ function makeRootBlock(appKeyPair: Object) {
25
30
}
26
31
27
32
export class AppHelper {
28
- _requester : AuthenticatedRequester ;
29
33
appId : Uint8Array ;
30
34
appKeyPair: Object ;
31
35
authToken: string ;
32
36
33
- constructor ( requester : AuthenticatedRequester , appId : Uint8Array , appKeyPair : Object , authToken : string ) {
34
- this . _requester = requester ;
37
+ constructor ( appId : Uint8Array , appKeyPair : Object , authToken : string ) {
35
38
this . appId = appId ;
36
39
this . appKeyPair = appKeyPair ;
37
40
this . authToken = authToken ;
@@ -40,37 +43,38 @@ export class AppHelper {
40
43
static async newApp ( ) : Promise < AppHelper > {
41
44
const appKeyPair = tcrypto . makeSignKeyPair ( ) ;
42
45
const rootBlock = makeRootBlock ( appKeyPair ) ;
43
- const message = {
46
+ const body = {
44
47
root_block : utils . toBase64 ( serializeBlock ( rootBlock ) ) ,
45
48
name : `functest-${ uuid . v4 ( ) } ` ,
46
- is_test : true ,
47
49
private_signature_key : utils . toBase64 ( appKeyPair . privateKey ) ,
48
50
} ;
49
- const requester = await AuthenticatedRequester . open ( ) ;
50
- const createResponse = await requester . send ( 'create trustchain' , message ) ;
51
- const authToken = createResponse . auth_token ;
51
+ const createResponse = await requestAdmindWithAuth ( { method : 'POST' , path : '/apps' , body } ) ;
52
+ const authToken = createResponse . app . auth_token ;
52
53
const appId = rootBlock . trustchain_id ;
53
- return new AppHelper ( requester , appId , appKeyPair , authToken ) ;
54
+ return new AppHelper ( appId , appKeyPair , authToken ) ;
55
+ }
56
+
57
+ async _update ( body : Object ) : Promise < Object > {
58
+ await requestAdmindWithAuth ( {
59
+ method : 'PATCH' ,
60
+ path : `/apps/${ toUnpaddedSafeBase64 ( this . appId ) } ` ,
61
+ body,
62
+ } ) ;
54
63
}
55
64
56
65
async setOIDC ( ) {
57
- await this . _requester . send ( 'update trustchain' , {
58
- id : utils . toBase64 ( this . appId ) ,
66
+ await this . _update ( {
59
67
oidc_provider : 'google' ,
60
68
oidc_client_id : oidcSettings . googleAuth . clientId ,
61
69
} ) ;
62
70
}
63
71
64
72
async unsetOIDC ( ) {
65
- await this . _requester . send ( 'update trustchain' , {
66
- id : utils . toBase64 ( this . appId ) ,
67
- oidc_provider : 'none' ,
68
- } ) ;
73
+ await this . _update ( { oidc_provider : 'none' } ) ;
69
74
}
70
75
71
76
async setS3 ( ) {
72
- await this . _requester . send ( 'update trustchain' , {
73
- id : utils . toBase64 ( this . appId ) ,
77
+ await this . _update ( {
74
78
storage_provider : 's3' ,
75
79
storage_bucket_name : storageSettings . s3 . bucketName ,
76
80
storage_bucket_region : storageSettings . s3 . bucketRegion ,
@@ -80,10 +84,7 @@ export class AppHelper {
80
84
}
81
85
82
86
async unsetS3 ( ) {
83
- await this . _requester . send ( 'update trustchain' , {
84
- id : utils . toBase64 ( this . appId ) ,
85
- storage_provider : 'none' ,
86
- } ) ;
87
+ await this . _update ( { storage_provider : 'none' } ) ;
87
88
}
88
89
89
90
generateIdentity ( userId ? : string ) : Promise < b64string > {
@@ -92,11 +93,12 @@ export class AppHelper {
92
93
}
93
94
94
95
async getVerificationCode ( email : string ) : Promise < string > {
95
- const msg = {
96
- trustchain_id : utils . toBase64 ( this . appId ) ,
96
+ const body = {
97
+ app_id : utils . toBase64 ( this . appId ) ,
97
98
email,
99
+ auth_token : this . authToken ,
98
100
} ;
99
- const answer = await this . _requester . send ( 'get verification code', msg ) ;
101
+ const answer = await requestTrustchaind ( { method : 'POST' , path : '/verification/email/ code', body } ) ;
100
102
if ( ! answer . verification_code ) {
101
103
throw new Error ( 'Invalid response' ) ;
102
104
}
@@ -113,6 +115,9 @@ export class AppHelper {
113
115
}
114
116
115
117
async cleanup ( ) : Promise < void > {
116
- await this . _requester . send ( 'delete trustchain' , { id : utils . toBase64 ( this . appId ) } ) ;
118
+ await requestAdmindWithAuth ( {
119
+ method : 'DELETE' ,
120
+ path : `/apps/${ toUnpaddedSafeBase64 ( this . appId ) } `
121
+ } ) ;
117
122
}
118
123
}
0 commit comments