Skip to content

Commit 6e9fd94

Browse files
committed
fix: last fixes to flow
Signed-off-by: Wouter Termont <wouter.termont@ugent.be>
1 parent bc4528d commit 6e9fd94

3 files changed

Lines changed: 40 additions & 58 deletions

File tree

demo/data/ruben/settings/policies/policy0.ttl

Lines changed: 0 additions & 10 deletions
This file was deleted.

demo/flow.ts

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ async function main() {
6868

6969
log(`Now, having discovered both the location of the UMA server and of the desired data, an agent can request the former for access to the latter.`);
7070

71+
const accessRequest = {
72+
// grant_type: 'urn:ietf:params:oauth:grant-type:uma-ticket',
73+
// ticket,
74+
claim_token: encodeURIComponent(terms.agents.vendor),
75+
claim_token_format: 'urn:solidlab:uma:claims:formats:webid',
76+
permissions: [{
77+
resource_id: terms.views.age,
78+
resource_scopes: [ terms.scopes.read ],
79+
}]
80+
};
81+
82+
const accessDeniedResponse = await fetch(tokenEndpoint, {
83+
method: "POST",
84+
headers: { "content-type": "application/json" },
85+
body: JSON.stringify(accessRequest),
86+
});
87+
88+
// if (accessDeniedResponse.status !== 403) { log('Access request succeeded without policy...'); throw 0; }
89+
90+
log(`Without a policy allowing the access, the access is denied.`);
91+
log(`However, the UMA server enables multiple flows in which such a policy can be added, for example by notifying the resource owner. (This is out-of-scope for this demo.)`);
92+
7193
log(`...`);
7294

7395
log(`Having been notified in some way of the access request, Ruben could go to his Authz Companion app, and add a policy allowing the requested access.`);
@@ -84,57 +106,27 @@ async function main() {
84106
});
85107

86108
if (policyCreationResponse.status !== 201) { log('Adding a policy did not succeed...'); throw 0; }
87-
109+
88110
log(`Now that the policy has been set, and the agent has possibly been notified in some way, the agent can try the access request again.`);
89-
90-
const content = {
91-
// grant_type: 'urn:ietf:params:oauth:grant-type:uma-ticket',
92-
claim_token: encodeURIComponent(terms.agents.vendor),
93-
claim_token_format: 'urn:solidlab:uma:claims:formats:webid',
94-
// ticket,
95-
permissions: [{
96-
resource_id: terms.views.age,
97-
resource_scopes: [ terms.scopes.read ],
98-
}]
99-
};
100-
101-
console.log(`=== Requesting token at ${tokenEndpoint} with ticket body:\n`);
102-
console.log(content);
103-
console.log('');
104-
105-
const asRequestResponse = await fetch(tokenEndpoint, {
106-
method: "POST",
107-
headers: { "content-type":"application/json" },
108-
body: JSON.stringify(content),
109-
})
110-
111-
// For debugging:
112-
// console.log("Authorization Server response:", await asRequestResponse.text());
113-
// throw 'stop'
114-
115-
const asResponse = await asRequestResponse.json();
116-
117-
const decodedToken = parseJwt(asResponse.access_token);
118-
119-
console.log(`= Status: ${asRequestResponse.status}\n`);
120-
console.log(`= Body (decoded):\n`);
121-
console.log({ ...asResponse, access_token: asResponse.access_token.slice(0,10).concat('...') });
122-
console.log('\n');
123-
124-
for (const permission of decodedToken.permissions) {
125-
console.log(`Permissioned scopes for resource ${permission.resource_id}:`, permission.resource_scopes)
126-
}
127111

128-
console.log(`=== Trying to create private resource <${terms.views.age}> WITH access token.\n`);
112+
const accessGrantedResponse = await fetch(tokenEndpoint, {
113+
method: "POST",
114+
headers: { "content-type": "application/json" },
115+
body: JSON.stringify(accessRequest),
116+
});
129117

130-
const tokenResponse = await fetch(terms.views.age, {
131-
headers: { 'Authorization': `${asResponse.token_type} ${asResponse.access_token}` }
118+
if (accessGrantedResponse.status !== 200) { log('Access request failed despite policy...'); throw 0; }
119+
120+
log(`Based on the policy, the UMA server returns the agent an access token with the requested permissions.`);
121+
122+
const tokenParams = await accessGrantedResponse.json();
123+
const accessWithTokenResponse = await fetch(terms.views.age, {
124+
headers: { 'Authorization': `${tokenParams.token_type} ${tokenParams.access_token}` }
132125
});
133126

134-
console.log(`= Status: ${tokenResponse.status}\n`);
135-
console.log(`= Body:\n`);
136-
console.log(`= Body: ${await tokenResponse.text()}`);
137-
console.log(`\n`);
127+
if (accessWithTokenResponse.status !== 200) { log('Access with token failed...'); throw 0; }
128+
129+
log(`The agent can then use this access token at the Resource Server to perform the desired action.`);
138130
}
139131

140132
main();

packages/uma/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
"build:ts": "yarn run -T tsc",
5757
"build:components": "yarn run -T componentsjs-generator -r sai-uma -s src -c dist/components -i .componentsignore --lenient",
5858
"test": "yarn run -T jest --coverage",
59-
"start": "ts-node bin/main.ts",
60-
"demo": "ts-node bin/demo.ts"
59+
"start": "yarn run -T ts-node bin/main.ts",
60+
"demo": "yarn run -T ts-node bin/demo.ts"
6161
},
6262
"dependencies": {
6363
"@httpland/authorization-parser": "^1.1.0",

0 commit comments

Comments
 (0)