@@ -51,96 +51,99 @@ bedrock.events.on('bedrock.init', () => {
51
51
52
52
bedrock . events . on ( 'bedrock-express.configure.routes' , app => {
53
53
const cfg = config [ 'profile-http' ] ;
54
+
55
+ // interactions feature is optional, return early if not enabled
56
+ const { interactions} = cfg ;
57
+ if ( ! interactions ?. enabled ) {
58
+ return ;
59
+ }
60
+
54
61
const interactionsPath = '/interactions' ;
55
62
const routes = {
56
63
interactions : interactionsPath ,
57
64
interaction : `${ interactionsPath } /:localInteractionId/:localExchangeId`
58
65
} ;
59
66
60
- // optional interactions feature
61
- const { interactions} = cfg ;
62
- if ( interactions ?. enabled ) {
63
- // create an interaction to exchange VCs
64
- app . post (
65
- routes . interactions ,
66
- ensureAuthenticated ,
67
- validate ( { bodySchema : schemas . createInteraction } ) ,
68
- asyncHandler ( async ( req , res ) => {
69
- const { id : accountId } = req . user . account || { } ;
70
- const { workflowName, exchange : { variables} } = req . body ;
71
-
72
- const workflow = WORKFLOWS_BY_NAME_MAP . get ( workflowName ) ;
73
- if ( ! workflow ) {
74
- throw new BedrockError ( `Workflow "${ workflowName } " not found.` , {
67
+ // create an interaction to exchange VCs
68
+ app . post (
69
+ routes . interactions ,
70
+ ensureAuthenticated ,
71
+ validate ( { bodySchema : schemas . createInteraction } ) ,
72
+ asyncHandler ( async ( req , res ) => {
73
+ const { id : accountId } = req . user . account || { } ;
74
+ const { workflowName, exchange : { variables} } = req . body ;
75
+
76
+ const workflow = WORKFLOWS_BY_NAME_MAP . get ( workflowName ) ;
77
+ if ( ! workflow ) {
78
+ throw new BedrockError ( `Workflow "${ workflowName } " not found.` , {
79
+ name : 'NotFoundError' ,
80
+ details : {
81
+ httpStatusCode : 404 ,
82
+ public : true
83
+ }
84
+ } ) ;
85
+ }
86
+
87
+ // create exchange with given variables
88
+ const exchange = {
89
+ // 15 minute expiry in seconds
90
+ ttl : 60 * 15 ,
91
+ // template variables
92
+ variables : {
93
+ ...variables ,
94
+ accountId
95
+ }
96
+ } ;
97
+ const capability = workflow . zcaps . get ( 'readWriteExchanges' ) ;
98
+ const response = await ZCAP_CLIENT . write ( { json : exchange , capability} ) ;
99
+ const exchangeId = response . headers . get ( 'location' ) ;
100
+ const { localInteractionId} = workflow ;
101
+ // reuse `localExchangeId` in path
102
+ const localExchangeId = exchangeId . slice ( exchangeId . lastIndexOf ( '/' ) ) ;
103
+ const id = `${ config . server . baseUri } /${ routes . interactions } /` +
104
+ `${ localInteractionId } /${ localExchangeId } ` ;
105
+ res . json ( { id, exchangeId} ) ;
106
+ } ) ) ;
107
+
108
+ // gets an interaction by its "id"
109
+ app . get (
110
+ routes . interactionPath ,
111
+ ensureAuthenticated ,
112
+ asyncHandler ( async ( req , res ) => {
113
+ const { id : accountId } = req . user . account || { } ;
114
+ const { localInteractionId, localExchangeId} = req . params ;
115
+
116
+ const workflow = WORKFLOWS_BY_ID_MAP . get ( localInteractionId ) ;
117
+ if ( ! workflow ) {
118
+ throw new BedrockError (
119
+ `Workflow "${ localInteractionId } " not found.` , {
75
120
name : 'NotFoundError' ,
76
121
details : {
77
122
httpStatusCode : 404 ,
78
123
public : true
79
124
}
80
125
} ) ;
81
- }
82
-
83
- // create exchange with given variables
84
- const exchange = {
85
- // 15 minute expiry in seconds
86
- ttl : 60 * 15 ,
87
- // template variables
88
- variables : {
89
- ...variables ,
90
- accountId
91
- }
92
- } ;
93
- const capability = workflow . zcaps . get ( 'readWriteExchanges' ) ;
94
- const response = await ZCAP_CLIENT . write ( { json : exchange , capability} ) ;
95
- const exchangeId = response . headers . get ( 'location' ) ;
96
- const { localInteractionId} = workflow ;
97
- // reuse `localExchangeId` in path
98
- const localExchangeId = exchangeId . slice ( exchangeId . lastIndexOf ( '/' ) ) ;
99
- const id = `${ config . server . baseUri } /${ routes . interactions } /` +
100
- `${ localInteractionId } /${ localExchangeId } ` ;
101
- res . json ( { id, exchangeId} ) ;
102
- } ) ) ;
103
-
104
- // gets an interaction by its "id"
105
- app . get (
106
- routes . interactionPath ,
107
- ensureAuthenticated ,
108
- asyncHandler ( async ( req , res ) => {
109
- const { id : accountId } = req . user . account || { } ;
110
- const { localInteractionId, localExchangeId} = req . params ;
111
-
112
- const workflow = WORKFLOWS_BY_ID_MAP . get ( localInteractionId ) ;
113
- if ( ! workflow ) {
114
- throw new BedrockError (
115
- `Workflow "${ localInteractionId } " not found.` , {
116
- name : 'NotFoundError' ,
117
- details : {
118
- httpStatusCode : 404 ,
119
- public : true
120
- }
121
- } ) ;
122
- }
126
+ }
123
127
124
- // FIXME: use in-memory cache to return exchange state if it was
125
- // polled recently
128
+ // FIXME: use in-memory cache to return exchange state if it was
129
+ // polled recently
126
130
127
- // fetch exchange
128
- const capability = workflow . zcaps . get ( 'readWriteExchanges' ) ;
129
- const response = await ZCAP_CLIENT . read ( {
130
- url : `${ capability . invocationTarget } /${ localExchangeId } ` ,
131
- capability
132
- } ) ;
131
+ // fetch exchange
132
+ const capability = workflow . zcaps . get ( 'readWriteExchanges' ) ;
133
+ const response = await ZCAP_CLIENT . read ( {
134
+ url : `${ capability . invocationTarget } /${ localExchangeId } ` ,
135
+ capability
136
+ } ) ;
133
137
134
- // ensure `accountId` matches exchange variables
135
- const { exchange : { state, variables} } = response ;
136
- if ( variables . accountId !== accountId ) {
137
- throw new BedrockError (
138
- 'The "account" is not authorized.' ,
139
- 'NotAllowedError' ,
140
- { httpStatusCode : 403 , public : true } ) ;
141
- }
138
+ // ensure `accountId` matches exchange variables
139
+ const { exchange : { state, variables} } = response ;
140
+ if ( variables . accountId !== accountId ) {
141
+ throw new BedrockError (
142
+ 'The "account" is not authorized.' ,
143
+ 'NotAllowedError' ,
144
+ { httpStatusCode : 403 , public : true } ) ;
145
+ }
142
146
143
- res . json ( { exchange : { state, variables} } ) ;
144
- } ) ) ;
145
- }
147
+ res . json ( { exchange : { state, variables} } ) ;
148
+ } ) ) ;
146
149
} ) ;
0 commit comments