|
| 1 | +import { App } from '@solid/community-server'; |
| 2 | +import { setGlobalLoggerFactory, WinstonLoggerFactory } from 'global-logger-factory'; |
| 3 | +import { createServer, Server } from 'node:http'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { getPorts, instantiateFromConfig } from '../util/ServerUtil'; |
| 6 | +import { getToken, umaFetch } from '../util/UmaUtil'; |
| 7 | + |
| 8 | +const [ rsPort, umaPort ] = getPorts('Partial'); |
| 9 | + |
| 10 | +interface UmaConfig { |
| 11 | + issuer: string; |
| 12 | + permission_endpoint: string; |
| 13 | + resource_registration_endpoint: string; |
| 14 | + token_endpoint: string; |
| 15 | + registration_endpoint: string; |
| 16 | +} |
| 17 | + |
| 18 | +describe('A server with partial results enabled', (): void => { |
| 19 | + const owner = 'http://example.com/alice#me'; |
| 20 | + const user = `http://example.com/bob`; |
| 21 | + const resource = `http://localhost:${rsPort}/alice/data`; |
| 22 | + const readScope = 'http://www.w3.org/ns/odrl/2/read'; |
| 23 | + const writeScope = 'http://www.w3.org/ns/odrl/2/write'; |
| 24 | + let umaApp: App; |
| 25 | + let rsServer: Server; |
| 26 | + let umaConfig: UmaConfig; |
| 27 | + let pat: string; |
| 28 | + |
| 29 | + beforeAll(async(): Promise<void> => { |
| 30 | + setGlobalLoggerFactory(new WinstonLoggerFactory('off')); |
| 31 | + |
| 32 | + umaApp = await instantiateFromConfig( |
| 33 | + 'urn:uma:default:App', |
| 34 | + [ |
| 35 | + path.join(__dirname, '../../packages/uma/config/default.json'), |
| 36 | + path.join(__dirname, '../../packages/uma/config/enable-partial.json'), |
| 37 | + ], |
| 38 | + { |
| 39 | + 'urn:uma:variables:port': umaPort, |
| 40 | + 'urn:uma:variables:baseUrl': `http://localhost:${umaPort}/uma`, |
| 41 | + 'urn:uma:variables:backupFilePath': '', |
| 42 | + } |
| 43 | + ); |
| 44 | + await umaApp.start(); |
| 45 | + |
| 46 | + const configResponse = await fetch(`http://localhost:${umaPort}/uma/.well-known/uma2-configuration`); |
| 47 | + expect(configResponse.status).toBe(200); |
| 48 | + umaConfig = await configResponse.json() as UmaConfig; |
| 49 | + |
| 50 | + const registrationResponse = await fetch(umaConfig.registration_endpoint, { |
| 51 | + method: 'POST', |
| 52 | + headers: { |
| 53 | + authorization: `WebID ${encodeURIComponent(owner)}`, |
| 54 | + 'content-type': 'application/json', |
| 55 | + }, |
| 56 | + body: JSON.stringify({ client_uri: `http://localhost:${rsPort}/` }), |
| 57 | + }); |
| 58 | + expect(registrationResponse.status).toBe(201); |
| 59 | + const { client_id, client_secret } = await registrationResponse.json() as { |
| 60 | + client_id: string, |
| 61 | + client_secret: string, |
| 62 | + }; |
| 63 | + |
| 64 | + const authString = `${encodeURIComponent(client_id)}:${encodeURIComponent(client_secret)}`; |
| 65 | + const credentials = `Basic ${Buffer.from(authString).toString('base64')}`; |
| 66 | + const patResponse = await fetch(umaConfig.token_endpoint, { |
| 67 | + method: 'POST', |
| 68 | + headers: { |
| 69 | + authorization: credentials, |
| 70 | + 'content-type': 'application/x-www-form-urlencoded', |
| 71 | + }, |
| 72 | + body: 'grant_type=client_credentials&scope=uma_protection', |
| 73 | + }); |
| 74 | + expect(patResponse.status).toBe(201); |
| 75 | + const patJson = await patResponse.json() as { access_token: string, token_type: string }; |
| 76 | + pat = `${patJson.token_type} ${patJson.access_token}`; |
| 77 | + |
| 78 | + const registrationBody = { |
| 79 | + name: resource, |
| 80 | + resource_scopes: [ readScope, writeScope ], |
| 81 | + }; |
| 82 | + const resourceRegistrationResponse = await fetch(umaConfig.resource_registration_endpoint, { |
| 83 | + method: 'POST', |
| 84 | + headers: { |
| 85 | + Authorization: pat, |
| 86 | + 'Content-Type': 'application/json', |
| 87 | + Accept: 'application/json', |
| 88 | + }, |
| 89 | + body: JSON.stringify(registrationBody), |
| 90 | + }); |
| 91 | + expect(resourceRegistrationResponse.status).toBe(201); |
| 92 | + |
| 93 | + rsServer = createServer((request, response): void => { |
| 94 | + void (async(): Promise<void> => { |
| 95 | + if (!request.url || !request.url.startsWith('/alice/data')) { |
| 96 | + response.statusCode = 404; |
| 97 | + response.end(); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const auth = request.headers.authorization; |
| 102 | + if (hasScope(auth, resource, readScope)) { |
| 103 | + response.statusCode = 200; |
| 104 | + response.end('protected data'); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + const permissionResponse = await fetch(umaConfig.permission_endpoint, { |
| 109 | + method: 'POST', |
| 110 | + headers: { |
| 111 | + Authorization: pat, |
| 112 | + 'Content-Type': 'application/json', |
| 113 | + Accept: 'application/json', |
| 114 | + }, |
| 115 | + body: JSON.stringify([ |
| 116 | + { |
| 117 | + resource_id: resource, |
| 118 | + resource_scopes: [ readScope ], |
| 119 | + } |
| 120 | + ]), |
| 121 | + }); |
| 122 | + |
| 123 | + if (permissionResponse.status !== 201) { |
| 124 | + response.statusCode = 500; |
| 125 | + response.end(await permissionResponse.text()); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + const { ticket } = await permissionResponse.json() as { ticket: string }; |
| 130 | + response.statusCode = 401; |
| 131 | + response.setHeader('WWW-Authenticate', `UMA realm="solid", as_uri="${umaConfig.issuer}", ticket="${ticket}"`); |
| 132 | + response.end(); |
| 133 | + })().catch((error: unknown): void => { |
| 134 | + response.statusCode = 500; |
| 135 | + response.end(String(error)); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + await new Promise<void>((resolve): void => { |
| 140 | + rsServer.listen(rsPort, resolve); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + afterAll(async(): Promise<void> => { |
| 145 | + const shutdown: Promise<unknown>[] = []; |
| 146 | + if (umaApp) { |
| 147 | + shutdown.push(umaApp.stop()); |
| 148 | + } |
| 149 | + if (rsServer) { |
| 150 | + shutdown.push(new Promise<void>((resolve, reject): void => { |
| 151 | + rsServer.close((error): void => { |
| 152 | + if (error) { |
| 153 | + reject(error); |
| 154 | + return; |
| 155 | + } |
| 156 | + resolve(); |
| 157 | + }); |
| 158 | + })); |
| 159 | + } |
| 160 | + await Promise.all(shutdown); |
| 161 | + }); |
| 162 | + |
| 163 | + it('can create a policy for the protected resource.', async(): Promise<void> => { |
| 164 | + const policy = ` |
| 165 | + @prefix ex: <http://example.org/> . |
| 166 | + @prefix odrl: <http://www.w3.org/ns/odrl/2/> . |
| 167 | + |
| 168 | + ex:policy a odrl:Agreement ; |
| 169 | + odrl:uid ex:policy ; |
| 170 | + odrl:permission ex:ownerPermission, ex:userPermission . |
| 171 | + |
| 172 | + ex:ownerPermission a odrl:Permission ; |
| 173 | + odrl:assignee <${owner}> ; |
| 174 | + odrl:assigner <${owner}> ; |
| 175 | + odrl:action odrl:create, odrl:modify ; |
| 176 | + odrl:target <http://localhost:${rsPort}/alice/> , |
| 177 | + <${resource}> . |
| 178 | +
|
| 179 | + ex:userPermission a odrl:Permission ; |
| 180 | + odrl:assignee <${user}> ; |
| 181 | + odrl:assigner <${owner}> ; |
| 182 | + odrl:action odrl:read ; |
| 183 | + odrl:target <${resource}> .`; |
| 184 | + |
| 185 | + const url = `http://localhost:${umaPort}/uma/policies`; |
| 186 | + let response = await fetch(url, { |
| 187 | + method: 'POST', |
| 188 | + headers: { authorization: `WebID ${encodeURIComponent(owner)}`, 'content-type': 'text/turtle' }, |
| 189 | + body: policy, |
| 190 | + }); |
| 191 | + expect(response.status).toBe(201); |
| 192 | + |
| 193 | + response = await umaFetch(resource, {}, user); |
| 194 | + expect(response.status).toBe(200); |
| 195 | + }); |
| 196 | + |
| 197 | + it('returns partial=true when not all requested scopes are granted.', async(): Promise<void> => { |
| 198 | + const permissionResponse = await fetch(umaConfig.permission_endpoint, { |
| 199 | + method: 'POST', |
| 200 | + headers: { |
| 201 | + Authorization: pat, |
| 202 | + 'Content-Type': 'application/json', |
| 203 | + Accept: 'application/json', |
| 204 | + }, |
| 205 | + body: JSON.stringify([ |
| 206 | + { |
| 207 | + resource_id: resource, |
| 208 | + resource_scopes: [ readScope, writeScope ], |
| 209 | + } |
| 210 | + ]), |
| 211 | + }); |
| 212 | + expect(permissionResponse.status).toBe(201); |
| 213 | + const { ticket } = await permissionResponse.json() as { ticket: string }; |
| 214 | + |
| 215 | + const token = await getToken(ticket, umaConfig.token_endpoint, user); |
| 216 | + |
| 217 | + // Verify partial flag is present |
| 218 | + expect((token as unknown as { partial?: boolean }).partial).toBe(true); |
| 219 | + |
| 220 | + // Verify the token contains the allowed scope |
| 221 | + const jwtPayload = JSON.parse(Buffer.from(token.access_token.split('.')[1], 'base64').toString()); |
| 222 | + expect(Array.isArray(jwtPayload.permissions)).toBe(true); |
| 223 | + expect(jwtPayload.permissions).toContainEqual({ |
| 224 | + resource_id: resource, |
| 225 | + resource_scopes: [ readScope ] |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + it('can access a protected resource with partial results.', async(): Promise<void> => { |
| 230 | + const response = await umaFetch(resource, {}, user); |
| 231 | + expect(response.status).toBe(200); |
| 232 | + }); |
| 233 | +}); |
| 234 | + |
| 235 | +function hasScope(authHeader: string | undefined, resource: string, scope: string): boolean { |
| 236 | + if (!authHeader?.startsWith('Bearer ')) { |
| 237 | + return false; |
| 238 | + } |
| 239 | + |
| 240 | + try { |
| 241 | + const token = authHeader.slice('Bearer '.length); |
| 242 | + const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()) as { |
| 243 | + permissions?: { resource_id: string, resource_scopes: string[] }[] |
| 244 | + }; |
| 245 | + return Array.isArray(payload.permissions) |
| 246 | + && payload.permissions.some((permission): boolean => |
| 247 | + permission.resource_id === resource && permission.resource_scopes.includes(scope) |
| 248 | + ); |
| 249 | + } catch { |
| 250 | + return false; |
| 251 | + } |
| 252 | +} |
0 commit comments