|
| 1 | +import { App, setGlobalLoggerFactory, WinstonLoggerFactory } from '@solid/community-server'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import { getDefaultCssVariables, instantiateFromConfig } from '../util/ServerUtil'; |
| 4 | + |
| 5 | +const cssPort = 3001; |
| 6 | +const umaPort = 4001; |
| 7 | + |
| 8 | +describe('A server setup', (): void => { |
| 9 | + let umaApp: App; |
| 10 | + let cssApp: App; |
| 11 | + |
| 12 | + beforeAll(async(): Promise<void> => { |
| 13 | + setGlobalLoggerFactory(new WinstonLoggerFactory('off')); |
| 14 | + |
| 15 | + umaApp = await instantiateFromConfig( |
| 16 | + 'urn:uma:default:App', |
| 17 | + path.join(__dirname, '../../packages/uma/config/default.json'), |
| 18 | + { |
| 19 | + 'urn:uma:variables:port': umaPort, |
| 20 | + 'urn:uma:variables:baseUrl': `http://localhost:${umaPort}/uma`, |
| 21 | + 'urn:uma:variables:policyBaseIRI': `http://localhost:${cssPort}/`, |
| 22 | + 'urn:uma:variables:policyDir': path.join(__dirname, '../../packages/uma/config/rules/policy'), |
| 23 | + 'urn:uma:variables:eyePath': 'eye', |
| 24 | + } |
| 25 | + ) as App; |
| 26 | + |
| 27 | + cssApp = await instantiateFromConfig( |
| 28 | + 'urn:solid-server:default:App', |
| 29 | + path.join(__dirname, '../../packages/css/config/default.json'), |
| 30 | + { |
| 31 | + ...getDefaultCssVariables(cssPort), |
| 32 | + 'urn:solid-server:uma:variable:AuthorizationServer': `http://localhost:${umaPort}/`, |
| 33 | + 'urn:solid-server:default:variable:seedConfig': path.join(__dirname, '../../packages/css/config/seed.json'), |
| 34 | + }, |
| 35 | + ) as App; |
| 36 | + |
| 37 | + await Promise.all([ umaApp.start(), cssApp.start() ]); |
| 38 | + }); |
| 39 | + |
| 40 | + afterAll(async(): Promise<void> => { |
| 41 | + await Promise.all([ umaApp.stop(), cssApp.stop() ]); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('using public namespace authorization', (): void => { |
| 45 | + const container = `http://localhost:${cssPort}/alice/public/`; |
| 46 | + const slug = 'resource.txt'; |
| 47 | + const body = 'This is a resource.'; |
| 48 | + |
| 49 | + it('RS: provides immediate read access.', async(): Promise<void> => { |
| 50 | + const publicResource = `http://localhost:${cssPort}/alice/profile/card`; |
| 51 | + |
| 52 | + const publicResponse = await fetch(publicResource); |
| 53 | + |
| 54 | + expect(publicResponse.status).toBe(200); |
| 55 | + expect(publicResponse.headers.get('content-type')).toBe('text/turtle'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('RS: provides immediate create access to the container', async(): Promise<void> => { |
| 59 | + const containerResponse = await fetch(container, { |
| 60 | + method: 'PUT', |
| 61 | + }); |
| 62 | + expect(containerResponse.status).toBe(201); |
| 63 | + expect(containerResponse.headers.get('location')).toBe(container); |
| 64 | + }); |
| 65 | + |
| 66 | + it('RS: provides immediate create access to the contents', async(): Promise<void> => { |
| 67 | + const createResponse = await fetch(container, { |
| 68 | + method: 'POST', |
| 69 | + headers: { slug }, |
| 70 | + body |
| 71 | + }); |
| 72 | + expect(createResponse.status).toBe(201); |
| 73 | + expect(createResponse.headers.get('location')).toBe(`${container}${slug}`); |
| 74 | + }); |
| 75 | + |
| 76 | + it('RS: provides immediate read access to the contents', async(): Promise<void> => { |
| 77 | + const readResponse = await fetch(`${container}${slug}`); |
| 78 | + expect(readResponse.status).toBe(200); |
| 79 | + await expect(readResponse.text()).resolves.toBe(body); |
| 80 | + }); |
| 81 | + |
| 82 | + it('RS: provides immediate delete access to the contents', async(): Promise<void> => { |
| 83 | + const deleteResponse = await fetch(`${container}${slug}`, { |
| 84 | + method: 'DELETE', |
| 85 | + }) |
| 86 | + expect(deleteResponse.status).toBe(205); |
| 87 | + |
| 88 | + const readResponse = await fetch(`${container}${slug}`); |
| 89 | + expect(readResponse.status).toBe(404); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('using ODRL authorization', (): void => { |
| 94 | + const privateResource = `http://localhost:${cssPort}/alice/private/resource.txt`; |
| 95 | + let wwwAuthenticateHeader: string; |
| 96 | + let ticket: string; |
| 97 | + let tokenEndpoint: string; |
| 98 | + let jsonResponse: { access_token: string, token_type: string }; |
| 99 | + |
| 100 | + it('RS: sends a WWW-Authenticate response when access is private.', async(): Promise<void> => { |
| 101 | + const noTokenResponse = await fetch(privateResource, { |
| 102 | + method: 'PUT', |
| 103 | + body: 'Some text ...' , |
| 104 | + }); |
| 105 | + |
| 106 | + expect(noTokenResponse.status).toBe(401); |
| 107 | + wwwAuthenticateHeader = noTokenResponse.headers.get('WWW-Authenticate'); |
| 108 | + expect(typeof wwwAuthenticateHeader).toBe('string'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('AS: returns the token endpoint from the configuration.', async(): Promise<void> => { |
| 112 | + const parsedHeader = Object.fromEntries( |
| 113 | + wwwAuthenticateHeader |
| 114 | + .replace(/^UMA /,'') |
| 115 | + .split(', ') |
| 116 | + .map(param => param.split('=').map(s => s.replace(/"/g,''))) |
| 117 | + ); |
| 118 | + expect(typeof parsedHeader.as_uri).toBe('string'); |
| 119 | + expect(typeof parsedHeader.ticket).toBe('string'); |
| 120 | + ticket = parsedHeader.ticket; |
| 121 | + |
| 122 | + const configurationUrl = parsedHeader.as_uri + '/.well-known/uma2-configuration'; |
| 123 | + const response = await fetch(configurationUrl); |
| 124 | + expect(response.status).toBe(200); |
| 125 | + const configuration = await response.json(); |
| 126 | + expect(typeof configuration.token_endpoint).toBe('string'); |
| 127 | + tokenEndpoint = configuration.token_endpoint; |
| 128 | + }); |
| 129 | + |
| 130 | + it('AS: responds with a token when receiving the ticket.', async(): Promise<void> => { |
| 131 | + const claim_token = 'https://woslabbi.pod.knows.idlab.ugent.be/profile/card#me'; |
| 132 | + |
| 133 | + const content = { |
| 134 | + grant_type: 'urn:ietf:params:oauth:grant-type:uma-ticket', |
| 135 | + ticket, |
| 136 | + claim_token: encodeURIComponent(claim_token), |
| 137 | + claim_token_format: 'urn:solidlab:uma:claims:formats:webid', |
| 138 | + }; |
| 139 | + |
| 140 | + const asRequestResponse = await fetch(tokenEndpoint, { |
| 141 | + method: 'POST', |
| 142 | + headers: { 'content-type': 'application/json' }, |
| 143 | + body: JSON.stringify(content), |
| 144 | + }); |
| 145 | + |
| 146 | + expect(asRequestResponse.status).toBe(200); |
| 147 | + expect(asRequestResponse.headers.get('content-type')).toBe('application/json'); |
| 148 | + jsonResponse = await asRequestResponse.json(); |
| 149 | + expect(typeof jsonResponse.access_token).toBe('string'); |
| 150 | + expect(jsonResponse.token_type).toBe('Bearer'); |
| 151 | + const token = JSON.parse(Buffer.from(jsonResponse.access_token.split('.')[1], 'base64').toString()); |
| 152 | + expect(Array.isArray(token.permissions)).toBe(true); |
| 153 | + expect(token.permissions).toHaveLength(2); |
| 154 | + expect(token.permissions).toContainEqual({ |
| 155 | + resource_id: `http://localhost:${cssPort}/alice/private/resource.txt`, |
| 156 | + resource_scopes: [ 'urn:example:css:modes:append', 'urn:example:css:modes:create' ] |
| 157 | + }); |
| 158 | + expect(token.permissions).toContainEqual({ |
| 159 | + resource_id: `http://localhost:${cssPort}/alice/private/`, |
| 160 | + resource_scopes: [ 'urn:example:css:modes:create' ] |
| 161 | + } |
| 162 | + ); |
| 163 | + }); |
| 164 | + |
| 165 | + it('RS: provides access when receiving a valid token.', async(): Promise<void> => { |
| 166 | + const response = await fetch(privateResource, { |
| 167 | + method: 'PUT', |
| 168 | + headers: { 'Authorization': `${jsonResponse.token_type} ${jsonResponse.access_token}` }, |
| 169 | + body: 'Some text ...' , |
| 170 | + }); |
| 171 | + |
| 172 | + expect(response.status).toBe(201); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments