From c9138a59c15239d05a578373b52bf28a9ce21ee0 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Mon, 21 Aug 2023 15:38:14 -0400 Subject: [PATCH 1/8] [feature] Switch from Ant build to Maven and add GitHub Actions CI --- .github/workflows/ci.yml | 28 +++++ .gitignore | 3 +- VERSION.txt | 1 - build.properties.xml | 12 -- build.xml | 36 ------ collection.xconf | 6 - expath-pkg.xml.tmpl | 6 - index.html | 9 -- pom.xml | 107 ++++++++++++++++++ post-install.xql | 6 - repo.xml.tmpl | 13 --- saml-request-ids/KEEPME | 1 - .../xar-resources-filtered/clean-reqids.xq | 4 +- .../main/xar-resources-filtered/scheduler.xq | 9 +- .../xar-resources/modules}/config-exsaml.xml | 0 .../main/xar-resources/modules}/exsaml.xqm | 6 +- src/main/xar-resources/post-install.xq | 18 +++ xar-assembly.xml | 43 +++++++ 18 files changed, 205 insertions(+), 103 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 VERSION.txt delete mode 100644 build.properties.xml delete mode 100644 build.xml delete mode 100644 collection.xconf delete mode 100644 expath-pkg.xml.tmpl delete mode 100644 index.html create mode 100644 pom.xml delete mode 100644 post-install.xql delete mode 100644 repo.xml.tmpl delete mode 100644 saml-request-ids/KEEPME rename content/clean-reqids.xql => src/main/xar-resources-filtered/clean-reqids.xq (84%) rename content/scheduler.xql => src/main/xar-resources-filtered/scheduler.xq (56%) rename {content => src/main/xar-resources/modules}/config-exsaml.xml (100%) rename {content => src/main/xar-resources/modules}/exsaml.xqm (98%) create mode 100644 src/main/xar-resources/post-install.xq create mode 100644 xar-assembly.xml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3060067 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +name: CI +on: [push, pull_request] +jobs: + build: + name: Build and Test (${{ matrix.os }} / OpenJDK ${{ matrix.jdk }}) + strategy: + fail-fast: true + matrix: + jdk: ['8'] + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: Set up JDK ${{ matrix.jdk }} + uses: actions/setup-java@v3 + with: + java-version: ${{ matrix.jdk }} + distribution: 'temurin' + - name: Cache Maven packages + uses: actions/cache@v3 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + - name: Maven Build + run: mvn clean package -DskipTests + - name: Test + run: mvn verify diff --git a/.gitignore b/.gitignore index 42d15f4..e06bcfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -build/ -.* +target/ !.gitignore diff --git a/VERSION.txt b/VERSION.txt deleted file mode 100644 index 266146b..0000000 --- a/VERSION.txt +++ /dev/null @@ -1 +0,0 @@ -1.6.3 diff --git a/build.properties.xml b/build.properties.xml deleted file mode 100644 index 4ecc28a..0000000 --- a/build.properties.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - existdb-saml - SAML implementation for existdb - 1.6.3 - http://exist-db.org/xquery/exsaml - beta - rwxr-xr-x - https://exist-db.org/ - - diff --git a/build.xml b/build.xml deleted file mode 100644 index 95aede0..0000000 --- a/build.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/collection.xconf b/collection.xconf deleted file mode 100644 index a871791..0000000 --- a/collection.xconf +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/expath-pkg.xml.tmpl b/expath-pkg.xml.tmpl deleted file mode 100644 index a67f499..0000000 --- a/expath-pkg.xml.tmpl +++ /dev/null @@ -1,6 +0,0 @@ - - - @name@ - - - diff --git a/index.html b/index.html deleted file mode 100644 index be398e4..0000000 --- a/index.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - forbidden - - -

forbidden

- - \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..31ab3d5 --- /dev/null +++ b/pom.xml @@ -0,0 +1,107 @@ + + + + 4.0.0 + + + org.exist-db + exist-apps-parent + 1.12.0 + + + + org.exist-db.apps + existdb-saml-xquery + 1.7.0-SNAPSHOT + + + eXist-db SAML XQuery + SAML v2.0 Implementation in XQuery + https://github.com/eXist-db/existdb-saml + + + https://github.com/eXist-db/existdb-saml.git + scm:git:https://github.com/eXist-db/existdb-saml.git + scm:git:https://github.com/eXist-db/existdb-saml.git + + + + UTF-8 + UTF-8 + + + http://exist-db.org/apps/exsaml + + 6.0.1 + + /db/system/repo/${project.artifactId}-${project.version} + + + + + + + src/main/xar-resources + false + + + src/main/xar-resources-filtered + true + + + + + + src/test/resources + false + + + src/test/resources-filtered + true + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + ro.kuberam.maven.plugins + kuberam-expath-plugin + + + create-xar + package + + make-xar + + + xar-assembly.xml + ${package-final-name} + + + + + + org.apache.maven.plugins + maven-gpg-plugin + + true + + + + org.apache.maven.plugins + maven-release-plugin + + forked-path + + true + @{project.version} + + + + + + diff --git a/post-install.xql b/post-install.xql deleted file mode 100644 index 92792cd..0000000 --- a/post-install.xql +++ /dev/null @@ -1,6 +0,0 @@ -xquery version "3.1"; - -(: the target collection into which the app is deployed :) -declare variable $target external; - -sm:chmod(xs:anyURI($target||'/saml-request-ids'), 'rwx------') diff --git a/repo.xml.tmpl b/repo.xml.tmpl deleted file mode 100644 index 07ba920..0000000 --- a/repo.xml.tmpl +++ /dev/null @@ -1,13 +0,0 @@ - - - @description@ - @website@ - @status@ - LGPL 2.1 - true - library - existdb-saml - - post-install.xql - - diff --git a/saml-request-ids/KEEPME b/saml-request-ids/KEEPME deleted file mode 100644 index a1b0291..0000000 --- a/saml-request-ids/KEEPME +++ /dev/null @@ -1 +0,0 @@ -just to keep this dir \ No newline at end of file diff --git a/content/clean-reqids.xql b/src/main/xar-resources-filtered/clean-reqids.xq similarity index 84% rename from content/clean-reqids.xql rename to src/main/xar-resources-filtered/clean-reqids.xq index 21b5de8..39a47f7 100644 --- a/content/clean-reqids.xql +++ b/src/main/xar-resources-filtered/clean-reqids.xq @@ -1,10 +1,10 @@ xquery version "3.1"; -import module namespace exsaml="http://exist-db.org/xquery/exsaml" at "/db/apps/existdb-saml/content/exsaml.xqm"; +import module namespace exsaml = "http://exist-db.org/xquery/exsaml" at "${exist.saml.library.path}/modules/exsaml.xqm"; import module namespace functx = "http://www.functx.com"; declare function local:clean-reqids() { - let $reqid-col := "/db/apps/existdb-saml/saml-request-ids" + let $reqid-col := "${exist.saml.library.path}/saml-request-ids" let $reqids := for $reqid in collection($reqid-col)/reqid let $duration := xs:dateTime(current-dateTime()) - xs:dateTime($reqid) return diff --git a/content/scheduler.xql b/src/main/xar-resources-filtered/scheduler.xq similarity index 56% rename from content/scheduler.xql rename to src/main/xar-resources-filtered/scheduler.xq index 76b40fd..b2368bc 100644 --- a/content/scheduler.xql +++ b/src/main/xar-resources-filtered/scheduler.xq @@ -1,21 +1,18 @@ xquery version "3.1"; -import module namespace scheduler="http://exist-db.org/xquery/scheduler" at "java:org.exist.xquery.modules.scheduler.SchedulerModule"; - - -declare namespace sc="http://exist-db.org/xquery/scheduler"; +import module namespace scheduler = "http://exist-db.org/xquery/scheduler"; declare variable $local:job-name := "clean-up-sso-reqids"; declare variable $local:cron := "0 0 11 * * ? *"; declare function local:start-job() { - scheduler:schedule-xquery-cron-job("/db/apps/existdb-saml/content/clean-reqids.xql", $local:cron, $local:job-name) + scheduler:schedule-xquery-cron-job("${exist.saml.library.path}/clean-reqids.xql", $local:cron, $local:job-name) }; declare function local:show-job() { let $jobs := scheduler:get-scheduled-jobs() return - $jobs//sc:job[@name=$local:job-name] + $jobs//scheduler:job[@name=$local:job-name] }; declare function local:stop-job() { diff --git a/content/config-exsaml.xml b/src/main/xar-resources/modules/config-exsaml.xml similarity index 100% rename from content/config-exsaml.xml rename to src/main/xar-resources/modules/config-exsaml.xml diff --git a/content/exsaml.xqm b/src/main/xar-resources/modules/exsaml.xqm similarity index 98% rename from content/exsaml.xqm rename to src/main/xar-resources/modules/exsaml.xqm index f8884aa..665064a 100644 --- a/content/exsaml.xqm +++ b/src/main/xar-resources/modules/exsaml.xqm @@ -51,7 +51,7 @@ declare %private variable $exsaml:fake-user := data($exsaml:config/fake-idp/@u declare %private variable $exsaml:fake-group := data($exsaml:config/fake-idp/@group); (: SAML specific constants and non-configurable vars :) -declare %private variable $exsaml:saml-coll-reqid := "/db/apps/existdb-saml/saml-request-ids"; +declare %private variable $exsaml:saml-coll-reqid := "${exist.saml.library.path}/saml-request-ids"; declare %private variable $exsaml:saml-version := "2.0"; declare %private variable $exsaml:status-success := "urn:oasis:names:tc:SAML:2.0:status:Success"; (: debugging only to simulate failure in fake-idp :) @@ -130,7 +130,7 @@ declare %private function exsaml:store-authnreqid-as-exsol-user($id as xs:string then ( let $log := exsaml:log("info", "collection " || $exsaml:saml-coll-reqid || " does not exist, attempting to create it") return - xmldb:create-collection("/db/apps/existdb-saml", "saml-request-ids") + xmldb:create-collection(fn:replace($exsaml:saml-coll-reqid, "(.*)/[^/]+", "$1"), fn:replace($exsaml:saml-coll-reqid, ".*/([^/]+)", "$1")) ) else () return @@ -228,7 +228,7 @@ declare function exsaml:process-saml-response-post() { else "" let $pass := exsaml:create-user-password($auth/@nameid) - let $log-in := xmldb:login("/db/apps", $auth/@nameid, $pass, true()) + let $log-in := xmldb:login("/db", $auth/@nameid, $pass, true()) let $log := util:log("info", "login result: " || $log-in || ", " || fn:serialize(sm:id())) (: put SAML token into browser session :) diff --git a/src/main/xar-resources/post-install.xq b/src/main/xar-resources/post-install.xq new file mode 100644 index 0000000..f519d3e --- /dev/null +++ b/src/main/xar-resources/post-install.xq @@ -0,0 +1,18 @@ +xquery version "3.1"; + +import module namespace sm = "http://exist-db.org/xquery/securitymanager"; +import module namespace xmldb = "http://exist-db.org/xquery/xmldb"; + +(: the target collection into which the app is deployed :) +declare variable $target external; + +declare variable $saml-request-ids-collection-name := "saml-request-ids"; +declare variable $saml-request-ids-collection-path := $target || "/" || $saml-request-ids-collection-name; + +let $_ := + if (fn:not(xmldb:collection-available($saml-request-ids-collection-path))) + then + xmldb:create-collection($target, $saml-request-ids-collection-name) + else() +return + sm:chmod(xs:anyURI($saml-request-ids-collection-path), "rwx------") diff --git a/xar-assembly.xml b/xar-assembly.xml new file mode 100644 index 0000000..625b318 --- /dev/null +++ b/xar-assembly.xml @@ -0,0 +1,43 @@ + + + ${package-title} + ${project.organization.name} + ${project.description} + ${project.url} + GNU LGPL 2.1 + true + + library + + stable + + ${project.artifactId} + + + + + ${package-abbrev} + + post-install.xq + + + + + ${project.build.outputDirectory} + + + ${project.basedir} + + README.md + + + + ${project.basedir} + + LICENSE + + + + + From b5892ed8eff55ab7e790841cce5e242a7fca0510 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Fri, 25 Aug 2023 13:33:09 -0500 Subject: [PATCH 2/8] [bugfix] Forward port from - https://github.com/eXist-db/existdb-saml/pull/19 - Create 'exsaml' user during package install --- .../modules => xar-resources-filtered}/exsaml.xqm | 0 src/main/xar-resources/modules/config-exsaml.xml | 2 +- src/main/xar-resources/pre-install.xq | 11 +++++++++++ xar-assembly.xml | 1 + 4 files changed, 13 insertions(+), 1 deletion(-) rename src/main/{xar-resources/modules => xar-resources-filtered}/exsaml.xqm (100%) create mode 100644 src/main/xar-resources/pre-install.xq diff --git a/src/main/xar-resources/modules/exsaml.xqm b/src/main/xar-resources-filtered/exsaml.xqm similarity index 100% rename from src/main/xar-resources/modules/exsaml.xqm rename to src/main/xar-resources-filtered/exsaml.xqm diff --git a/src/main/xar-resources/modules/config-exsaml.xml b/src/main/xar-resources/modules/config-exsaml.xml index ecd6d8d..ed2a34d 100644 --- a/src/main/xar-resources/modules/config-exsaml.xml +++ b/src/main/xar-resources/modules/config-exsaml.xml @@ -52,7 +52,7 @@ - + From 89cdc24b51e68b69ad1fe8bf9943cafbb804d413 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Fri, 25 Aug 2023 14:34:30 -0500 Subject: [PATCH 3/8] [bugfix] Forward port from - https://github.com/eXist-db/existdb-saml/pull/22 - Use correct XDM type for compatibility with eXist-db 6.x.x and without breaking backwards compatibility Closes https://github.com/eXist-db/existdb-saml/issues/2 --- src/main/xar-resources-filtered/exsaml.xqm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/xar-resources-filtered/exsaml.xqm b/src/main/xar-resources-filtered/exsaml.xqm index 665064a..074b206 100644 --- a/src/main/xar-resources-filtered/exsaml.xqm +++ b/src/main/xar-resources-filtered/exsaml.xqm @@ -122,7 +122,7 @@ declare %private function exsaml:build-saml-authnreq() { return $req }; -declare %private function exsaml:store-authnreqid-as-exsol-user($id as xs:string, $instant as xs:string) { +declare %private function exsaml:store-authnreqid-as-exsol-user($id as xs:string, $instant as xs:dateTime) { let $create-collection := if ( not(xmldb:collection-available($exsaml:saml-coll-reqid)) @@ -139,7 +139,7 @@ declare %private function exsaml:store-authnreqid-as-exsol-user($id as xs:string }; (: store issued request ids in a collection, :) -declare %private function exsaml:store-authnreqid($id as xs:string, $instant as xs:string) { +declare %private function exsaml:store-authnreqid($id as xs:string, $instant as xs:dateTime) { let $log := exsaml:log("info", "storing SAML request id: " || $id || ", date: " || $instant) return system:as-user( From ec01352ae76a04878fb5cb0204d97dc6f915731e Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Fri, 25 Aug 2023 14:51:27 -0500 Subject: [PATCH 4/8] fixup! [feature] Switch from Ant build to Maven and add GitHub Actions CI --- src/main/xar-resources-filtered/{ => modules}/exsaml.xqm | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/xar-resources-filtered/{ => modules}/exsaml.xqm (100%) diff --git a/src/main/xar-resources-filtered/exsaml.xqm b/src/main/xar-resources-filtered/modules/exsaml.xqm similarity index 100% rename from src/main/xar-resources-filtered/exsaml.xqm rename to src/main/xar-resources-filtered/modules/exsaml.xqm From 1b4b62395dc8c59d77ef8f60256d01da1098b3cd Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Fri, 25 Aug 2023 14:55:43 -0500 Subject: [PATCH 5/8] [bugfix] Forward port from - https://github.com/eXist-db/existdb-saml/pull/19 - Export the exsaml module as an EXPath Package Module --- pom.xml | 2 +- xar-assembly.xml | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 31ab3d5..7e3f355 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ http://exist-db.org/apps/exsaml - + 6.0.1 /db/system/repo/${project.artifactId}-${project.version} diff --git a/xar-assembly.xml b/xar-assembly.xml index 30b8378..9f35778 100644 --- a/xar-assembly.xml +++ b/xar-assembly.xml @@ -40,5 +40,16 @@ - + + + + + http://exist-db.org/xquery/exsaml + ${project.build.outputDirectory} + + modules/exsaml.xqm + + + + From 19f5d29a2482717545ce6fbc2173ce4aaa1eb2ac Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Fri, 25 Aug 2023 15:23:57 -0500 Subject: [PATCH 6/8] [bugfix] Forward port from - https://github.com/eXist-db/existdb-saml/pull/19 - Make sure the saml-request-ids collection is accessible by the 'exsaml' user --- pom.xml | 1 + src/main/xar-resources/modules/config-exsaml.xml | 2 +- src/main/xar-resources/post-install.xq | 6 +++++- src/main/xar-resources/pre-install.xq | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 7e3f355..88cfc11 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,7 @@ 6.0.1 + exsaml /db/system/repo/${project.artifactId}-${project.version} diff --git a/src/main/xar-resources/modules/config-exsaml.xml b/src/main/xar-resources/modules/config-exsaml.xml index ed2a34d..8e77e72 100644 --- a/src/main/xar-resources/modules/config-exsaml.xml +++ b/src/main/xar-resources/modules/config-exsaml.xml @@ -52,7 +52,7 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + sso-user + + + + + + + + + + diff --git a/src/test/resources/myapp/controller.xq b/src/test/resources/myapp/controller.xq new file mode 100644 index 0000000..b070821 --- /dev/null +++ b/src/test/resources/myapp/controller.xq @@ -0,0 +1,63 @@ +xquery version "3.1"; + +declare namespace exist = "http://exist.sourceforge.net/NS/exist"; + +(: import exsaml module :) +import module namespace exsaml = "http://exist-db.org/xquery/exsaml" at 'xmldb:///db/system/repo/existdb-saml-xquery-1.7.0-SNAPSHOT/modules/exsaml.xqm'; + +declare variable $exist:controller external; +declare variable $exist:path external; + +(: this is required for SAML so that the IDP response can be rendered as a form + that gets auto-submitted by the user's browser, back to the SP (eXist) :) +declare option exist:serialize "method=html media-type=text/html indent=no"; + +(: handle SP endpoint to process SAML response in HTTP POST :) +if ($exist:path = "/SAML2SP") +then + let $log := util:log('info', "SAML2SP: processing SAML response") + let $status := exsaml:process-saml-response-post() + let $log := util:log('debug', "endpoint SAML2SP; status: " || $status/@code) + return + if ($status/@code >= 0) then + (: forward to page that was requested by the user :) + let $debug := util:log("info", "Auth success - code " || $status/@code || " - relaystate: " || $status/@relaystate) + return + + + + else + (: if SAML failed, display an error message for now :) + {string($status/@msg) || ": " || string($status/@data)} + +(: if logout, invalidate SAML token :) +else if ($exist:path = '/logout') +then + let $_ := + if (exsaml:is-enabled()) + then + exsaml:invalidate-saml-token() + else () + return + + + + +(: if no valid token, redirect to SAML auth :) +else if (exsaml:is-enabled() and not(exsaml:check-valid-saml-token())) +then + let $debug := exsaml:log('info', "controller: no valid token, redirect to SAML auth") + let $return-path := "/exist/apps" || $exist:controller || $exist:path + return + + + + + + + +(: We have an existing valid SAML token! :) +else + + + diff --git a/src/test/resources/myapp/index.html b/src/test/resources/myapp/index.html new file mode 100644 index 0000000..c8263e3 --- /dev/null +++ b/src/test/resources/myapp/index.html @@ -0,0 +1,8 @@ + + + My App + + +

Welcome to MyApp

+ + \ No newline at end of file