Skip to content

Commit

Permalink
fix(hydra-box): missing req param
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jun 20, 2021
1 parent e8c16db commit bef021d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-eggs-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hydra-box-web-access-control": patch
-------------------------------------

Add express `Request` as second parameter to custom patterns
18 changes: 15 additions & 3 deletions packages/hydra-box-web-access-control/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ import error from 'http-errors'
import type { StreamClient } from 'sparql-http-client/StreamClient'
import type * as express from 'express'
import { acl } from '@tpluscode/rdf-ns-builders'
import { check, Check } from 'rdf-web-access-control'
import { check, AdditionalPatterns } from 'rdf-web-access-control'
import { Variable } from '@rdfjs/types'
import type { SparqlTemplateResult } from '@tpluscode/sparql-builder'

interface Option extends Pick<Check, 'additionalPatterns'> {
export interface AclPatterns {
(acl:Variable, req: express.Request): SparqlTemplateResult | string
}

interface Option {
client: StreamClient
additionalPatterns?: AclPatterns | AclPatterns[]
}

function wrapPatterns(patterns: Option['additionalPatterns'] = [], req: express.Request): AdditionalPatterns[] {
const arr = Array.isArray(patterns) ? patterns : [patterns]
return arr.map(func => acl => func(acl, req))
}

export default ({ client, additionalPatterns }: Option): express.RequestHandler => asyncMiddleware(async (req, res, next) => {
Expand Down Expand Up @@ -41,7 +53,7 @@ export default ({ client, additionalPatterns }: Option): express.RequestHandler
accessMode,
client,
agent: req.agent,
additionalPatterns,
additionalPatterns: wrapPatterns(additionalPatterns, req),
})

if (!result) {
Expand Down
4 changes: 2 additions & 2 deletions packages/hydra-box-web-access-control/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('hydra-box-web-access-control', () => {

it('passes right parameters to check', async () => {
// given
const additionalPatterns = () => ''
const additionalPatterns = sinon.stub()
const agent = clownface({ dataset: $rdf.dataset() }).namedNode('')
app.use((req, res, next) => {
req.agent = agent
Expand All @@ -55,7 +55,7 @@ describe('hydra-box-web-access-control', () => {
client,
agent,
term,
additionalPatterns,
additionalPatterns: sinon.match.array,
}))
})

Expand Down
20 changes: 19 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ app.use(hydraBox.middleware(api, {
}))
```

### Per-request authorization restrictions

A function of array of functions can be optionally passed to the middleware. They take an RDF/JS variable, and the current request object as parameters and should return additional SPARQL patterns to filter out ACL authorization resources as desired.

```typescript
import accessControl from 'hydra-box-web-access-control'
import { Variable } from '@rdfjs/types'
import { Request } from 'express'

const middleware = accessControl({
client,
additionalPatterns(acl: Variable, req: Request) {
// ...
}
})
```

See [below](#additional-authorization-restrictions) for a complete example. The only difference is that the `hydra-box-web-access-control` adds the second parameter while the other accepts only one.

## rdf-web-access-control

Expand Down Expand Up @@ -164,7 +182,7 @@ All queries will implicitly add `rdfs:Resource` to the queries types. Given a st

It is possible to restrict considered instances of `acl:Authorization`, for example to select only ACLs valid for given timeframe or by a custom property.

To do that, pass a function to the `check` call, which will return partial SPARQL patterns. It takes an RDF/JS Variable object as input which will match the ACL resources in the query,
To do that, pass a function to the `check` call, which will return partial SPARQL patterns. It takes an RDF/JS Variable object as input which will match the ACL resources in the query.

```typescript
import { Variable } from '@rdfjs/types'
Expand Down

0 comments on commit bef021d

Please sign in to comment.